woocommercefieldfrontenddatasheet

Get a product custom field value in Woocommerce single product pages


Could anyone assist in helping me get the below code to function correctly. I have taken bits of code from various other posts to create it.

I need to create a custom field (Datasheet) and be able to post a link to a PDF file. It should then appear just before the meta data on each product page.

Current code is as follows:

add_action( 'woocommerce_product_options_general_product_data', 
'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;

echo '<div class="options_group">';

woocommerce_wp_text_input( array( // Text Field type
    'id'          => '_datasheet_url', 
    'label'       => __( 'Datasheet', 'woocommerce' ), 
    'placeholder' => 'http://',
    'desc_tip'    => 'true',
    'description' => __( 'Datasheet URL here.', 'woocommerce' ) 
) );

echo '</div>';
}

add_action( 'woocommerce_process_product_meta', 
'woo_save_custom_general_fields' );
function woo_save_custom_general_fields( $post_id ){

$datasheet_field= $_POST['_datasheet_url'];
if( !empty( $datasheet_field ) )
    update_post_meta( $post_id, '_datasheet_url', esc_attr( $datasheet_field 
) );
}

add_action('woocommerce_product_meta_start', 
'woo_display_custom_general_fields_values', 5);
function woo_display_custom_general_fields_values() {
global $product;

$url = get_post_meta( $post->ID, 'Datasheet', true );

     echo '<img src="http://www.freemansolutions.co.uk/wp-content/uploads/pdf- 
icon.png"> <a href="'.$url.'">Datasheet</a>';
}  

Solution

  • Update: Replaced with WooCommerce recent hooks and WC_Product methods

    The main error is $url = get_post_meta( $post->ID, 'Datasheet', true ); as $post is not defined, then $post->ID throw an error and you can get the URL custom field value.

    Try the following instead:

    add_action( 'woocommerce_product_options_general_product_data', 'add_datasheet_url_custom_field' );
    function add_datasheet_url_custom_field() {
        echo '<div class="options_group">';
    
        woocommerce_wp_text_input( array(
            'id'          => '_datasheet_url',
            'label'       => __('Datasheet URL', 'woocommerce'),
            'placeholder' => 'https://',
            'desc_tip'    => 'true',
            'description' => __('Set the "Datasheet" URL here.', 'woocommerce'),
            'data_type'   => 'url', // Added the correct data type
        ) );
    
        echo '</div>';
    }
    
    add_action( 'woocommerce_admin_process_product_object', 'save_datasheet_url_custom_field' );
    function save_datasheet_url_custom_field( $product ){
        if( isset( $_POST['_datasheet_url'] ) ) {
            $product->update_meta_data('_datasheet_url', sanitize_url( $_POST['_datasheet_url'] ) );
        }
    }
    
    add_action('woocommerce_product_meta_start', 'use_datasheet_url_custom_field', 5 );
    function use_datasheet_url_custom_field() {
        global $product;
        
        if( $datasheet_url = $product->get_meta( '_datasheet_url' ) ){
            printf( '<img src="%s" /> <a href="%s">%s</a>', home_url( '/wp-content/uploads/pdf-icon.png' ), $datasheet_url, esc_html__('Datasheet', 'woocommerce') );
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin).