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

  • 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' => 'http://',
            'desc_tip'    => 'true',
            'description' => __('Set the "Datasheet" URL here.', 'woocommerce'),
        ) );
    
        echo '</div>';
    }
    
    add_action( 'woocommerce_process_product_meta', 'save_datasheet_url_custom_field', 12, 1 );
    function save_datasheet_url_custom_field( $post_id ){
        if( isset( $_POST['_datasheet_url'] ) )
            update_post_meta( $post_id, '_datasheet_url', sanitize_text_field( $_POST['_datasheet_url'] ) );
    }
    
    add_action('woocommerce_product_meta_start', 'use_datasheet_url_custom_field', 5 );
    function use_datasheet_url_custom_field() {
        $pdf_url = get_post_meta( get_the_id(), '_datasheet_url', true );
        if( ! empty( $pdf_url ) ){
            $icon_pdf = home_url( '/wp-content/uploads/pdf-icon.png' );
            echo '<img src="'.$icon_pdf.'" /> <a href="'.$pdf_url.'">' . __('Datasheet', 'woocommerce') . '</a>';
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.