我需要帮助在产品前端添加自定义数据,就像这样:
如果 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 单个产品和购物车页面中输出自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽