【问题标题】:Add additional product information on Woocommerce checkout page在 Woocommerce 结帐页面上添加其他产品信息
【发布时间】:2017-09-13 09:25:15
【问题描述】:

在 WooCommerce 中,我有一个自定义产品字段 “时间”,我想在结帐时输出该字段的值,特别是在产品详细信息中,在产品名称下,使其如下所示: Event time: (value from wcv_custom_product_field)

我试过放置:

add_filter( 'woocommerce_get_item_data', 'wc_checkout_producttime', 10, 2 );

function wc_checkout_producttime( $other_data, $cart_item )
{
    $_product = $cart_item['data'];

    $other_data[] = array( 'name' =>  'wcv_custom_product_field', 'value' => $_product->get_wcv_custom_product_field() );
return $other_data;
}

但我在结帐时看到空白页。

我做错了什么,我该如何解决这个问题?

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce checkout product


    【解决方案1】:

    这是一个挂在 woocommerce_get_item_data 过滤器挂钩中的自定义函数,它将在购物车和结帐项目中显示您的产品自定义字段:

    add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );
    function display_custom_product_field_data( $cart_data, $cart_item ) {
    
        // Define HERE your product custom field meta key  <==   <==   <==   <==   <==
        $meta_key = 'wcv_custom_product_field';
    
        $product_id = $cart_item['product_id'];
    
        $meta_value = get_post_meta( $product_id, $meta_key, true );
    
        if( !empty( $cart_data ) )
            $custom_items = $cart_data;
    
        if( !empty($meta_value) ) {
            $custom_items[] = array(
                'key'       => __('Event time', 'woocommerce'),
                'value'     => $meta_value,
                'display'   => $meta_value,
            );
        }
        return $custom_items;
    }
    

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

    此代码已经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2020-02-22
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 2022-01-20
      • 1970-01-01
      • 2019-12-13
      • 2014-04-24
      相关资源
      最近更新 更多