Italian (Italy)

apply_filters( 'vikrestaurants_load_delivery_area', string $classname, string $type )

Fires while creating a new instance for the provided delivery area type.


Description

Trigger hook to allow external plugins to include new types of delivery areas that might have been implemented out of this project.

Plugins must include here the file holding the class of the area type. The resulting class must inherit the E4J\VikRestaurants\DeliveryArea\Area declaration, otherwise it will be ignored.


Parameters

$classname

(string)  A custom classname that should be used to handle a specific type of delivery areas.

$type

(string)  The delivery area type.


Example

The following examples loads a custom delivery area type from the types subfolder.

/**
 * Trigger hook to allow external plugins to include new types of delivery
 * areas that might have been implemented out of this project. Plugins must
 * include here the file holding the class of the area type.
 *
 * @param  string  $classname  The classname used to load the delivery area type instance.
 * @param  string  $type       The requested delivery area type.
 */
add_filter('vikrestaurants_load_delivery_area', function($classname, $type) {
    if ($type === 'custom') { 
        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'types' . DIRECTORY_SEPARATOR . 'custom.php';
        $classname = 'CustomDeliveryAreaType';
    }
    
    return $classname;
}, 10, 2);

Here's how a custom delivery area type should be implemented.

use E4J\VikRestaurants\DeliveryArea\Area;
use E4J\VikRestaurants\DeliveryArea\DeliveryQuery;

class CustomDeliveryAreaType extends Area
{
    /**
     * Returns the name of the area type.
     *
     * @return  string
     */
    public function getType()
    {
        return 'Custom';
    }

    /**
     * Checks whether the provided delivery query can be covered by this area.
     * 
     * @param   DeliveryQuery  $query
     * 
     * @return  bool  True if supported, false otherwise.
     */
    public function canAccept(DeliveryQuery $query)
    {
        /** @var null|object (latitude, longitude) */
        $coordinates = $query->getCoordinates();

        /** @var null|string */
        $address = $query->getAddress();

        /** @var null|string */
        $city = $query->getCity();

        /** @var null|string */
        $zip = $query->getZipCode();

        // to see all the registered components
        echo json_encode($query, JSON_PRETTY_PRINT);

        /**
         * @todo check whether the delivery query is acceptable
         */
        return false;
    }

    /**
     * Fires when the model is going to save the delivery area.
     * Children classes can overwrite this method to manipulate the
     * contents and the attributes.
     * 
     * @param   array  &$src   The data to bind.
     * @param   mixed  $model  The form model.
     * 
     * @return  bool   True on success, false otherwise.
     */
    public function onSave(array &$src, $model)
    {
        return true;
    }
}

Changelog

Version Description
1.3 Introduced.
Ultimo aggiornamento: 2023-12-28
Utile?
Potrebbe interessarti: