This snippet makes the pickup and delivery estimate in FoodMaster (formerly WooFood) follow the longest processing time in the cart. Add it to your child theme’s functions.php file, or to a code snippets plugin.
Set the pickup time from the cart
The snippet reads the process_time_woocommerce custom field of every product in the cart and returns the highest value. A product without that field counts as 0.
Updated for current FoodMaster versions: the code below uses the foodmaster_ names. Older installs that still run WooFood used the woofood_ prefix, so on those change foodmaster_ back to woofood_.
add_filter("foodmaster_adjust_pickup_time", "change_dynamically_pickup_time", 10, 1);
function change_dynamically_pickup_time($default)
{
global $woocommerce;
if(WC()->cart)
{
$woofood_delivery_time = 0;
$all_product_times = array();
$total_time_to_delivery = 0;
foreach( WC()->cart->get_cart() as $cart_item ):
## Using WC_Order_Item methods ##
// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $cart_item['data']->get_id();
$item_name = $cart_item['data']->get_name(); // Name of the product
$item_type = $cart_item['data']->get_type(); // Type of the order item ("line_item")
## Access Order Items data properties (in an array of values) ##
$item_data = $cart_item['data']->get_data();
$product_name = $item_data['name'];
$product_id = $item_data['id'];
$all_product_times[] = intval(get_post_meta($product_id, 'process_time_woocommerce', true));
endforeach;
$woofood_delivery_time = max($all_product_times) + intval($woofood_delivery_time) ;
return $woofood_delivery_time;
}
else{
return $default;
}
}How the snippet works
The function hooks into foodmaster_adjust_pickup_time. It loops over the cart, collects the processing time of each product, and returns the maximum. If the cart is empty, it returns the default time it was given.
Before you start
- Set a processing time for each menu item in the
process_time_woocommercecustom field. - To check it worked, add two items with different processing times to the cart and open the checkout. The estimate should match the slower item.
- Test on a staging site before you use it on the live shop.
FoodMaster (formerly WooFood)
The snippet uses a filter from FoodMaster (formerly WooFood), the restaurant ordering plugin for WooCommerce. You can get FoodMaster here.
