首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Woocommerce结帐中为来自产品类别的特定虚拟产品启用发货地址

在Woocommerce结帐中为来自产品类别的特定虚拟产品启用发货地址
EN

Stack Overflow用户
提问于 2018-12-18 22:18:15
回答 1查看 1.3K关注 0票数 3

我有一个类别门,用于在虚拟产品上显示发货。基本上,我有产品,我不想收取运费,我有一个类别,称为礼品.但我还是想要个送货地址。问题是,当我使用我构建的类别筛选器时,它不会按顺序保存地址.如果我只是用..。

代码语言:javascript
复制
add_filter( 'woocommerce_cart_needs_shipping_address', '__return_true', 50 );

效果很好..。

但当我把门放在上面时..。它不能拯救价值..。这是大门..。

代码语言:javascript
复制
//gifts filter
function HDM_gift_shipping() {
// set our flag to be false until we find a product in that category
$cat_check = false;

// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    $product = $cart_item['data'];

   // if cat matches gift return true
    if ( has_term( 'gift', 'product_cat', $product->id ) ) {
        $cat_check = true;
        // break because we only need one "true" to matter here
        break;
    }
}

// if a product in the cart is in our category, do something
if ( $cat_check ) {
    add_filter( 'woocommerce_cart_needs_shipping_address', '__return_true', 50 );
}

}
add_action('woocommerce_before_checkout_billing_form', 'HDM_gift_shipping', 100);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-18 22:37:03

代码中有一些错误。要做到这一点,最好将代码直接设置在woocommerce_cart_needs_shipping_address过滤器钩子中:

代码语言:javascript
复制
add_filter( 'woocommerce_cart_needs_shipping_address', 'custom_cart_needs_shipping_address', 50, 1 );
function custom_cart_needs_shipping_address( $needs_shipping_address ) {
    // Loop though cat items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( has_term( array('gift'), 'product_cat', $cart_item['product_id'] ) ) {
            // Force enable shipping address for virtual "gift" products
            return true; 
        }
    }
    return $needs_shipping_address;
}

代码在您的活动子主题(或活动主题)的function.php文件中。测试和工作。

在购物车( cart )中,要处理Woocommerce 自定义分类法(如产品类别或标签),在使用 WordPress条件函数时,需要使用$cart_item['product_id']而不是不适用于产品变体的$cart_item['data']->get_id()

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53841930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档