Output a custom field value in WooCommerce single

2019-01-15 22:19发布

I need help to add the custom data in the front end of the product just like this:

See Image

This image is a sample if PD # i just add and i want it to add in the front end of the product:

See image

Just done adding a the custom field in WooCommerce product pages general setting tab, with the help of: How to add a Custom Fields in Woocommerce 3.1 thanks @Ankur Bhadania.

Thanks

2条回答
Summer. ? 凉城
2楼-- · 2019-01-15 22:57

For simple products your product '_pd_number' custom field in simple product pages before add-to-cart, using woocommerce_before_add_to_cart_button action hook this way:

add_action( 'woocommerce_before_add_to_cart_button', 'add_cf_before_addtocart_in_single_products', 1, 0 );
function add_cf_before_addtocart_in_single_products()
{
     global $product;

     $pd_number = get_post_meta( $product->get_id(), '_pd_number', true );
     if( !empty( $pd_number ) )
        echo '<div class="pd-number">PD # '. $pd_number .'</div><br>';

}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

enter image description here

This code is tested on WooCommerce version 3+ and works.


You can also output your product custom field in the cart items below the name, using woocommerce_cart_item_name filter hook this way:

// Displaying the product custom field in the Cart items
add_filter( 'woocommerce_cart_item_name', 'add_cf_after_cart_item_name', 10, 3 );
function add_cf_after_cart_item_name( $name_html, $cart_item, $cart_item_key )
{
    $product_id = $cart_item['product_id'];
    if( $cart_item['variation_id'] > 0 )
        $product_id = $cart_item['variation_id'];

     $pd_number = get_post_meta( $product_id, '_pd_number', true );;
     if( !empty( $pd_number ) )
        $name_html .= '<br><span class="pd-number">PD # '.$pd_number .'</span>';

     return $name_html;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested on WooCommerce version 3+ and works.

enter image description here


Here the code I have use from he provided link in comments just for testing:

// Display Product settings Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
    woocommerce_wp_text_input( array(
        'id'          => '_pd_number',
        'label'       => __( 'PD Number', 'woocommerce' ),
        'placeholder' => 'PD# XXXXXX',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the PD Number of Product', 'woocommerce' )
    ));
}

// Save Product settings Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
    $pd_number = $_POST['_pd_number'];
    if( !empty( $pd_number ) )
        update_post_meta( $post_id, '_pd_number', esc_attr( $pd_number ) );
}

Related answer: Override External Product URL to "Add to Cart" product button

查看更多
地球回转人心会变
3楼-- · 2019-01-15 23:16

For display custom product meta in signal product page used 2 hook. used any of one as per your requirement.

1) display before WooCommerce product meta. used woocommerce_product_meta_start

add_action( 'woocommerce_product_meta_start', 'woo_add_custom_general_fields_extra_info' );
function woo_add_custom_general_fields_extra_info()
{
     global $woocommerce, $post;
     echo "PD # ".get_post_meta( $post->ID, '_text_field', true );

}

2) display after WooCommerce product meta. used woocommerce_product_meta_end

add_action( 'woocommerce_product_meta_end', 'woo_add_custom_general_fields_extra_info' );
function woo_add_custom_general_fields_extra_info()
{
     global $woocommerce, $post;
     echo "PD # ".get_post_meta( $post->ID, '_text_field', true );

}
查看更多
登录 后发表回答