English (United Kingdom)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Take-Away  >  Calculate Minimum Cost

apply_filters( 'vikrestaurants_calculate_order_min_cost', float $cost, float $default, array $args )

Fires while fetching the minimum cost for ordering.


Description

Plugins can use this hook to override at runtime the minimum cost for food ordering.


Parameters

$cost

(float)  The minimum cost to use.

$default

(float)  The minimum cost based on configuration and delivery area.

$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 increases the minimum total for order placed outside the weekend and which require a delivery.

/**
 * Plugins can use this hook to override the minimum cost at runtime.
 *
 * @param  float  $cost     The minimum cost to use.
 * @param  float  $default  The minimum cost based on configuration and delivery area.
 * @param  array  $args     An associative array containing the order query.
 */
add_filter('vikrestaurants_calculate_order_min_cost', function($cost, $default, $args)
{
    // check if we have a delivery service
    if ($args['delivery'])
    {
        // 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 cost has been already manipulated
            $cost = is_null($cost) ? $default : $cost;
            // increase minimum cost by 5
            $cost += 5;
        }
    }

    return $cost;
}, 10, 3);

Changelog

Version Description
1.2 Introduced.
Last Update: 2021-01-26
Helpful?
This site uses cookies. By continuing to browse you accept their use. Further information