问题: Add_action代码确定购物车中是否存在“食品”类别,并应用送货费用。工作完美,但在收费之前,需要客户检查餐饮订单是否需要送货。如果为true,则添加费用。
目标是确定购物车中是否有“食物”,“哪个当前代码可以,如果为真,则显示标记为”Deliver Order?“的复选框。如果为真,则在购物车中的同一行上添加费用。
我在代码中将行添加到购物车之前,但得到致命错误。如果我得到添加行代码工作,我可以插入到add_action自定义费用代码循环后,通过购物车,但之前的条件,以申请费用。
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('food');
$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 = 25;
}
// Adding the fee
if ( $fee_amount > 0 ){
// Last argument is related to enable tax (true or false)
WC()->cart->add_fee( __( "Delivery Fee", "woocommerce" ), $fee_amount,
false );
}
}我期望该操作确定购物车中是否存在“食品”弹头,如果是,则在购物车中添加行到购物车中,并在客户输入框中显示是否已交付订单。如果为真,则向同一新行添加$25费用?
发布于 2019-04-29 17:40:07
add_action( 'woocommerce_cart_calculate_fees',function($cart) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$is_food = false;
foreach ( $cart->cart_contents as $item ) {
if ( $item['data']->is_type( 'food' ) ) {
$is_food = true;
break;
}
}
if($is_food){
$fee=25;
$cart->add_fee( 'Delivery Fee',$fee, true, 'standard' );
}
});请尝试上面的代码,粘贴到一个子主题的functions.php中。
https://stackoverflow.com/questions/55897851
复制相似问题