【发布时间】:2022-01-03 16:58:36
【问题描述】:
我想为类别 (A) 添加费用,但仅当订单中有其他类别(B、C、D 等)的产品时才计算。
但如果只订购 A 类产品,则不适用该税。
在我的代码中,这两种情况都会增加费用。您能指导我找到更好的解决方案吗?
我在我的网站上添加此代码:
add_action( 'woocommerce_cart_calculate_fees','custom_pcat_fee', 20, 1 );
function custom_pcat_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Set HERE your categories (can be term IDs, slugs or names) in a coma separated array
$categories = array('396');
$fee_amount = 0;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
if( has_term( $categories, 'product_cat', $cart_item['product_id']) )
$fee_amount = 20;
}
// Adding the fee
if ( $fee_amount > 0 ){
// Last argument is related to enable tax (true or false)
WC()->cart->add_fee( __( "Taxa livrare ROPET", "woocommerce" ), $fee_amount, false );
}
}
【问题讨论】:
标签: php wordpress woocommerce hook-woocommerce fee