我有一个类别门,用于在虚拟产品上显示发货。基本上,我有产品,我不想收取运费,我有一个类别,称为礼品.但我还是想要个送货地址。问题是,当我使用我构建的类别筛选器时,它不会按顺序保存地址.如果我只是用..。
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);发布于 2018-12-18 22:37:03
代码中有一些错误。要做到这一点,最好将代码直接设置在woocommerce_cart_needs_shipping_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文件中。测试和工作。
在购物车( cart )中,要处理Woocommerce 自定义分类法(如产品类别或标签),在使用 WordPress条件函数时,需要使用
$cart_item['product_id']而不是不适用于产品变体的$cart_item['data']->get_id()。
https://stackoverflow.com/questions/53841930
复制相似问题