Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从Woocommerce签出中的产品类别为特定虚拟产品启用发货地址_Php_Wordpress_Woocommerce_Field_Checkout - Fatal编程技术网

Php 从Woocommerce签出中的产品类别为特定虚拟产品启用发货地址

Php 从Woocommerce签出中的产品类别为特定虚拟产品启用发货地址,php,wordpress,woocommerce,field,checkout,Php,Wordpress,Woocommerce,Field,Checkout,我有一个分类门,用于显示虚拟产品的发货情况。基本上我有我不想收取运费的产品,我有一个叫做礼品的类别。。。但我还是要一个送货地址。问题是,当我使用我建立的分类过滤器时,它不会按顺序保存地址。。。如果我只是用 add_filter( 'woocommerce_cart_needs_shipping_address', '__return_true', 50 ); 它工作得很好 但是当我把大门关上的时候。。。它不会保存值。。。这是大门 //gifts filter function HDM_gift

我有一个分类门,用于显示虚拟产品的发货情况。基本上我有我不想收取运费的产品,我有一个叫做礼品的类别。。。但我还是要一个送货地址。问题是,当我使用我建立的分类过滤器时,它不会按顺序保存地址。。。如果我只是用

add_filter( 'woocommerce_cart_needs_shipping_address', '__return_true', 50 );
它工作得很好

但是当我把大门关上的时候。。。它不会保存值。。。这是大门

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

您的代码中有一些错误。要使其正常工作,您最好直接在
woocommerce\u cart\u needs\u shipping\u address
过滤器挂钩中设置代码,方法如下:

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文件。测试和工作

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


再一次抓住这个答案。听起来你好像知道你的东西!最后一个问题,假设我想把它限制在一个标签而不是一个类别。我会用“产品标签”替换“产品标签”吗?@MattNath在代码中用“产品标签”替换分类法中的“产品标签”,我想是的。再次感谢你!