Output a custom field value in WooCommerce single product and cart pages(在 WooCommerce 单个产品和购物车页面中输出自定义字段值)
问题描述
我需要帮助在产品前端添加自定义数据,就像这样:
如果 PD # 我只是添加并且我希望将其添加到产品的前端,则此图像是示例:
刚刚在 WooCommerce 产品页面常规设置选项卡中添加了自定义字段,在以下帮助下:
此代码已在 WooCommerce 版本 3+ 上测试并有效.
<小时>您还可以在名称下方的购物车项目中输出您的产品自定义字段,使用 woocommerce_cart_item_name
过滤器钩子这样:
//在购物车项目中显示产品自定义字段add_filter('woocommerce_cart_item_name', 'add_cf_after_cart_item_name', 10, 3);函数 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 );;如果( !empty( $pd_number ) )$name_html .= '<br><span class="pd-number">PD # '.$pd_number .'</span>';返回 $name_html;}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
此代码已在 WooCommerce 版本 3+ 上测试并有效.
<小时>这是我使用的代码,他在评论中提供了链接,仅供测试:
//显示产品设置字段add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');函数 woo_add_custom_general_fields() {woocommerce_wp_text_input(数组('id' =>'_pd_number','标签' =>__( 'PD 号', 'woocommerce' ),'占位符' =>'PD# XXXXXX','desc_tip' =>'真的','说明' =>__( '输入产品的 PD 编号', 'woocommerce' )));}//保存产品设置字段add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');函数 woo_add_custom_general_fields_save( $post_id ){$pd_number = $_POST['_pd_number'];如果( !empty( $pd_number ) )update_post_meta( $post_id, '_pd_number', esc_attr( $pd_number ) );}
相关答案:覆盖外部添加到购物车"的产品 URL产品按钮
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:
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
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.
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.
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
这篇关于在 WooCommerce 单个产品和购物车页面中输出自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 WooCommerce 单个产品和购物车页面中输出自定义
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01