简体   繁体   English

在WooCommerce单一产品和购物车页面中输出自定义字段值

[英]Output a custom field value in WooCommerce single product and cart pages

I need help to add the custom data in the front end of the product just like this: 我需要帮助将自定义数据添加到产品的前端,如下所示:

见图片

This image is a sample if PD # i just add and i want it to add in the front end of the product: 如果我只是添加PD#,并且希望将其添加到产品的前端,则此图像为示例:

看图片

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. 刚刚在WooCommerce产品页面的“常规设置”选项卡中添加了自定义字段 ,并获得了以下帮助: 如何在Woocommerce 3.1中添加自定义字段感谢@Ankur Bhadania。

Thanks 谢谢

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: 对于简单产品,在添加到购物车之前,请在简单产品页面上的产品'_pd_number'自定义字段中,使用woocommerce_before_add_to_cart_button操作挂钩:

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. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

在此处输入图片说明

This code is tested on WooCommerce version 3+ and works. 此代码已在WooCommerce版本3+上经过测试,并且可以运行。


You can also output your product custom field in the cart items below the name, using woocommerce_cart_item_name filter hook this way: 您还可以使用woocommerce_cart_item_name过滤器钩子,在名称下方的购物车项目中输出产品定制字段:

// 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. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

This code is tested on WooCommerce version 3+ and works. 此代码已在WooCommerce版本3+上经过测试,并且可以运行。

在此处输入图片说明


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 相关答案:将外部产品URL覆盖为“添加到购物车”产品按钮

For display custom product meta in signal product page used 2 hook. 为了在信号产品页面中显示自定义产品元数据,请使用2个钩子。 used any of one as per your requirement. 根据您的要求使用了任何一种。

1) display before WooCommerce product meta. 1)在WooCommerce产品meta之前显示。 used woocommerce_product_meta_start 二手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. 2)在WooCommerce产品meta之后显示。 used woocommerce_product_meta_end 二手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 );

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过钩子在 WooCommerce 单个产品页面中显示自定义字段值 - Display custom field value in WooCommerce single product pages via hook 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 显示自定义字段吹产品标题Woocommerce产品单页 - Display custom field blow product title Woocommerce product single pages 在购物车和结帐的 WooCommerce 产品名称中附加自定义字段值 - Append custom field value in WooCommerce product name on cart and checkout 在WooCommerce购物车和结帐表中显示产品自定义字段值 - Display a product custom field value in WooCommerce cart and checkout table 使用产品自定义字段值覆盖 woocommerce 购物车项目价格 - Override woocommerce cart item price with product custom field value 仅在Woocommerce单一产品页面中显示特定自定义字段值的文本 - Show text only for a specific custom field value in Woocommerce single product pages 在 Woocommerce 单个产品页面的简短描述下显示自定义字段 - Display a custom field under short description in Woocommerce single product pages 在 WooCommerce 档案页面中的产品标题下方添加自定义字段值 - Add a custom field value below product title in WooCommerce archives pages 在 WooCommerce 管理订单页面中保留并显示产品自定义字段值 - Keep and display product custom field value in WooCommerce Admin Order pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM