【问题标题】:Output a custom field value in WooCommerce single product and cart pages在 WooCommerce 单一产品和购物车页面中输出自定义字段值
【发布时间】:2017-07-11 05:49:47
【问题描述】:

我需要帮助在产品前端添加自定义数据,就像这样:

如果 PD # 我只是添加并且我希望它添加到产品的前端,则此图像是示例:

刚刚在 WooCommerce 产品页面常规设置选项卡中添加了一个自定义字段,借助:How to add a Custom Fields in Woocommerce 3.1 感谢@Ankur Bhadania。

谢谢

【问题讨论】:

  • Ankur 的回答有效吗?
  • @VasimVanzara 是的,它已经工作了,我只想在前端添加自定义字段数据,查看图片了解更多信息,
  • @VasimVanzara 我只是使用示例图像,案例尚未解决。我不知道如何在前端添加那个 PD 号码。

标签: php wordpress woocommerce custom-fields product


【解决方案1】:

对于简单的产品,您的产品 '_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>';

}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码在 WooCommerce 版本 3+ 上经过测试并且可以正常工作。


您还可以使用 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;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码在 WooCommerce 版本 3+ 上经过测试并且可以正常工作。


这是我使用的代码,他在 cmets 中提供了链接,仅用于测试:

// 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 ) );
}

相关回答:Override External Product URL to "Add to Cart" product button

【讨论】:

    【解决方案2】:

    为了在信号产品页面中显示自定义产品元数据,使用了 2 个挂钩。根据您的要求使用任何一种。

    1) 在 WooCommerce 产品元数据之前显示。用过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) 在 WooCommerce 产品元数据之后显示。使用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 );
    
    }
    

    【讨论】:

    • 谢谢你的帮助,我还是要把这个放在主题的functions.php中吗?
    • 是的,把它放在你的function.php中
    • 我得到了空白页。当我添加 woocommerce_product_meta_end.
    • 无法在 /home/xxxxxx/public_html/wp-content/ 中重新声明 woo_add_custom_general_fields_extra_info()(之前在 /home/xxxxx/public_html/wp-content/themes/xxxx/functions.php:137 中声明)主题/xxxx/functions.php 第 148 行
    • 没用过这个都只用过一个。 woocommerce_product_meta_startwoocommerce_product_meta_end
    猜你喜欢
    • 2014-04-20
    • 1970-01-01
    • 2018-07-16
    • 2016-01-14
    • 2018-01-07
    • 1970-01-01
    • 2018-08-28
    • 2019-07-21
    • 2017-11-03
    相关资源
    最近更新 更多