在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:
如果我只是添加PD#,并且希望将其添加到产品的前端,则此图像为示例:
This image is a sample if PD # i just add and i want it to add in the front end of the product:
只需完成以下操作,即可在WooCommerce产品页面的常规设置"选项卡中添加自定义字段:
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_before_add_to_cart_button
操作将您的产品'_pd_number'
自定义字段添加到购物车之前的简单产品页面中用这种方式钩住:
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>';
}
代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.
此代码已在WooCommerce 3+及更高版本上进行了测试.
This code is tested on WooCommerce version 3+ and works.
您还可以通过以下方式使用 woocommerce_cart_item_name
过滤器挂钩在名称下方的购物车项目中输出您的产品自定义字段:
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;
}
代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.
此代码已在WooCommerce 3+及更高版本上进行了测试.
This code is tested on WooCommerce version 3+ and works.
这是我从他那里使用的代码,它在注释中提供了仅用于测试的链接:
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 ) );
}
相关答案:覆盖外部产品网址为添加到购物车"产品按钮
这篇关于在WooCommerce单一产品和购物车页面中输出自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!