设想情况:
我们是一家在线的多餐厅送货服务:
如果顾客从两家或更多的不同餐馆订购,我们需要额外的费用。所以每一家餐厅都要多付2欧元的费用。
例子:
类别:
问题:
以下是我们认为可能有效的方法:
餐馆是一个子类别,例如:
城市(顶级猫) ->餐厅(儿童猫) ->餐(产品)
解决办法:
因此,我们认为很容易编写一些代码,检查购物车结账上的产品(餐食)是否来自一个以上的类别(餐馆)?然后再加收附加费?
这里需要一些关于如何最好地处理这个场景的帮助。谢谢!
发布于 2019-06-27 13:03:05
为此,在活动主题的functions.php中添加以下代码片段-
function woo_add_cart_fee() {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$restaurant = array();
foreach($items as $item => $values) {
$available_restaurant_cat_ids = array(); // Replace the empty array with all avaiable 'restaurants' categories ids
$restaurant_list_ids = wp_get_post_terms($values['data']->get_id(),'product_cat',array('fields'=>'ids')); // we assumed that restaurant belongs to product_cat
if($restaurant_list_ids){
foreach ($restaurant_list_ids as $id) {
if(!in_array($id, $available_restaurant_cat_ids)) continue; // id not belongs to restaurant
if(!in_array($id, $restaurant)){
$restaurant[] = $id;
}
}
}
}
$restaurant_multiply = count($restaurant) - 1; // first restaurant id free
$extra_fee = 2; // 2 euro
$woocommerce->cart->add_fee( __('Extra fees', 'woocommerce'), $extra_fee*$restaurant_multiply );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );https://stackoverflow.com/questions/56790320
复制相似问题