【发布时间】:2018-08-20 19:48:13
【问题描述】:
受this answer code 的启发,我们目前正在使用一些自定义代码,当数量等于6 时会添加$2.50 美元费用。
但是,当同一类别中的两个产品每个都有6的数量时,我们希望它增加$2.50费用。
它几乎可以工作,但是当同一类别中有两个产品并且其中一个的数量为 12 时,代码 sn-p 而不是保留 @987654327 @ 到 $2.50,将其更改为 $7.50。
因此,我们需要找到一种方法来更好地定位单个产品及其各自的数量或用途或方程式,并在找到数量为 12 的产品实例时从中得到 -5。
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('649');
$fee_amount = 0;
$cat_count = 0;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
if( has_term( $categories, 'product_cat', $cart_item['product_id']))
$quantity = $cart_item['quantity'];
$cat_count += $cart_item['quantity'];
}
if ($quantity == 6){
$fee_amount = (2.5 * ($cat_count/6));
;}
// Adding the fee
if ( $fee_amount > 0 ){
// Last argument is related to enable tax (true or false)
WC()->cart->add_fee( __( "Find-it Mixed Case", "woocommerce" ), $fee_amount, false );
}
}
【问题讨论】:
标签: php wordpress woocommerce cart fee