沃梦达 / 编程问答 / php问题 / 正文

在 WooCommerce 单个产品和购物车页面中输出自定义

Output a custom field value in WooCommerce single product and cart pages(在 WooCommerce 单个产品和购物车页面中输出自定义字段值)

本文介绍了在 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 单个产品和购物车页面中输出自定义

基础教程推荐