【问题标题】:ACF, post object in woocommerce CartACF,在 woocommerce 购物车中发布对象
【发布时间】:2018-12-03 23:15:21
【问题描述】:

我希望这是一个愚蠢的问题,但我目前找不到聪明的解决方案。

我的产品页面中有一个自定义字段 - 这是一个关系字段 - 我想为我的购物车页面中的每个产品显示我的 RF 字段。

更具体地说:我有一个与品牌相关的产品。该品牌有一个“运输时间”字段。

目前我的功能部分工作,从某种意义上说,它显示请求值,但仅适用于购物车中的第一项。 这是我在function.php中的代码

add_filter( 'woocommerce_get_item_data', 'wc_add_shipping_to_cart', 10, 2 );
function wc_add_shipping_to_cart( $cart_data, $cart_item ) 
{ 

$custom_shipping = array();

if( !empty( $cart_data ) )
    $custom_shipping = $cart_data;

// Get the product ID
$product_id = $cart_item['product_id'];
$custom_field_value = get_post_meta( $product_id, 'brand_select', true );
$display_brand_shipping = get_field('shipping_time_brand', $custom_field_value); 

if( $custom_field_value = get_post_meta( $product_id, 'brand_select', true ) )
    $custom_shipping[] = array(
        'name'      => __( 'Shipping', 'woocommerce' ),
        'value'     => $display_brand_shipping,
        'display'   => $display_brand_shipping,
    );

return $custom_shipping; }

你能帮我吗?

【问题讨论】:

  • 产品与品牌的关系如何?品牌是自定义分类法还是产品属性?这个品牌的分类是什么?是否为产品设置了品牌(就像产品类别或产品标签一样)?
  • 'brand' 是自定义帖子类型,我通过关系字段将产品与其关联
  • 抱歉,我帮不上忙,因为它太习惯了,无法猜测什么不起作用。
  • 呃,您需要什么样的信息?我有一个名为“品牌”的 cpt,其中有一些字段(运输时间、网站等)。我有一些产品通过关系字段与品牌相关联。鉴于这种关系,我想在购物车页面上显示相关品牌的字段

标签: woocommerce advanced-custom-fields


【解决方案1】:

您的代码不可测试,因为无法重现与产品自定义字段和 ACF 字段相关的问题,但可以通过这种方式简化:

add_filter( 'woocommerce_get_item_data', 'wc_add_shipping_to_cart', 10, 2 );
function wc_add_shipping_to_cart( $cart_item_data, $cart_item ) 
{ 
    if( $brand_select = get_post_meta( $cart_item['product_id'], 'brand_select', true ) ) {
        if( $shipping_time = get_field('shipping_time_brand', $brand_select ) ) {
            $cart_item_data[] = array(
                'name'      => __( 'Shipping', 'woocommerce' ),
                'value'     => $shipping_time,
            );
        }
    }
    return $cart_item_data;
}

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

【讨论】:

  • 谢谢,这也有效。不幸的是,我的问题仍然存在。
猜你喜欢
  • 1970-01-01
  • 2018-08-29
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
相关资源
最近更新 更多