public function onCalculateFreeDeliveryThreshold( float $threshold, TakeAwayCart $cart, array $args ) : float

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 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.

Return Value

Float. The new threshold to use.


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 default threshold taken from the configuration.
 * @param   TakeAwayCart  $cart       The cart instance.
 * @param   array         $args       An associative array containing the order query.
 *
 * @return  float         The new threshold to use.
 */
public function onCalculateFreeDeliveryThreshold($threshold, $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)
    {
        // divide threshold by 2
        $threshold /= 2;
    }

    return $threshold;
}

Changelog

Version Description
1.8.3 Introduced.
Last Update: 2023-12-29 14:15
Helpful?