0
votes

SCENARIO :

We are an online multi-restaurant delivery service :

And we need to add an extra fee if the customer orders from 2 or more different restaurants. So for each restaurant we add an extra 2€ fee.

EXAMPLES :

  • customer selects 1 meal : no fee
  • customer selects 2 meals from same restaurant : no fee
  • customer selects 2 meals from 2 diff restaurants : 2€ extra fee
  • customer selects 3 meals from 3 diff restaurants : 2€ + 2€ = 4€
  • customer selects 4 meals from 4 diff restaurants : 2€ + 2€ +2€ = 6€ ... and so on

CATEGORIES :

  • Each restaurant is 1 category
  • Each meal has more than 1 category AND
  • We need to check if this category in particular is equal or different for each meal on cart checkout

PROBLEM :

Here's what we think might work :

  • We might have to scroll thru each meal (product) on cart checkout AND
  • We have to get the categories for each meal AND
  • Compare each meal + check if they belong to diff restaurants

Restaurants are a sub-category like :

City (top cat.) -> Restaurant (child cat.) -> Meal (product)

SOLUTION :

So we think might be easy to write some code that checks if products (meals) on cart checkout are from more than 1 category (restaurant) ? And then apply the additional fee ?

Need some help here on how to best approach this scenario. Thank you!

1

1 Answers

0
votes

To do so, add follows code snippet in your active theme's 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' );