English (United Kingdom)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Take-Away  >  Calculate Free Delivery

apply_filters( 'vikrestaurants_calculate_free_delivery_threshold', float $threshold, float $default, TakeAwayCart $cart, array $args )

Fires while fetching the threshold needed to offer the delivery.


Description

Plugins can use this hook to override the threshold used to offer free deliveries at runtime.

The default threshold is defined by the Free Delivery With setting within the take-away configuration. When the total cost of the cart reaches the specified threshold, the delivery charge will be always unset.


Parameters

$threshold

(float)  The new threshold to use.

$default

(float)  The default value taken from the configuration.

$cart

(TakeAwayCart)  The instance holding the purchased items.

$args

(array)  An associative array containing the order query.

  • date - the check-in date of the order, formatted according to the configuration;
  • hourmin - the check-in time of the order, using the 24H format;
  • delivery - true in case of delivery service, false in case of pickup.

Example

The example below decreases the free delivery threshold for order placed outside the weekend.

/**
 * Plugins can use this hook to override the threshold used to offer free deliveries at runtime.
 *
 * @param  float         $threshold  The new threshold to use.
 * @param  float         $default    The default threshold taken from the configuration.
 * @param  TakeAwayCart  $cart       The cart instance.
 * @param  array         $args       An associative array containing the order query.
 */
add_filter('vikrestaurants_calculate_free_delivery_threshold', function($threshold, $default, $cart, $args)
{
    // extract hours and minutes
    list($h, $m) = explode(':', $args['hourmin']);
    // create timestamp
    $ts = VikRestaurants::createTimestamp($args['date'], (int) $h, (int) $m);
    // get day of the week
    $w = (int) date('w', $ts);

    // check whether the day is included between Mon and Thu
    if ($w >= 1 && $w <= 4)
    {
        // check whether the threshold has been already manipulated
        $threshold = is_null($threshold) ? $default : $threshold;
        // divide threshold by 2
        $threshold /= 2;
    }

    return $threshold;
}, 10, 4);

Changelog

Version Description
1.2 Introduced.
Last Update: 2021-01-26
Helpful?