Italian (Italy)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Offerte  >  Load Supported

apply_filters_ref_array( 'vikrestaurants_load_supported_deals', bool $status, array &$config )

Fires while loading all the supported deals.


Description

This hook can be used to support custom types of deals.

It is enough to include the directory containing the new rules. Only the files that inherits the DealRule class will be taken.

In addition, the loaded classes must have a name built as DealRule + [FILE_NAME].

In case the class name is not correct or it doesn't extend DealRule, the file will be ignored.

IMPORTANT WARNING: This filter has been officially deprecated since the 1.3 release and will be no longer supported starting from the 1.4 version of VikRestaurants. You should rather use the vikrestaurants_setup_takeaway_deals action.

Parameters

$status

(bool)  True on success, false otherwise.

&$config

(array)  It is possible to inject here the configuration for a specific deal. The parameters have to be assigned by using the deal file name.


Example

The example below adds support for all the deals contained in a specific folder of a third-party plugin. Such as:

/wp-content/plugins/vikwp/deals/

All the PHP files contained within the deals folder of the VikWP plugin will be loaded.

/** 
 * This hook can be used to support custom deals.
 * It is enough to include the directory containing
 * the new rules. Only the files that inherits the
 * DealRule class will be taken.
 *
 * @param  boolean  $status   True on success, false otherwise.
 * @param  array    &$config  It is possible to inject the configuration for
 *                            a specific deal. The parameters have to be assigned
 *                            by using the deal file name.
 */
add_filter('vikrestaurants_load_supported_deals', function($status, &$config)
{
    // fetch deals folder path
    $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'deals';

    // include deals folder
    DealsHandler::addIncludePath($folder);

    // recover deal configuration from database
    $params = json_decode(get_option('myplugin_deal_test_config', '{}'));

    // register plugin configuration for being used
    // while trying to apply the deal
    $config['test'] = $params;

    return true;
}, 10, 2);

The class (shell) of the deal will be built as follows. The path of the file will be equal to this one.

/wp-content/plugins/vikwp/deals/test.php
class DealRuleTest extends DealRule
{
    /**
     * Returns the deal code identifier.
     *
     * @return  integer
     */
    public function getID()
    {
        // DO NOT use values between 1 and 10, because
        // they have been reserved by the system
        return 76;
    }

    /**
     * Returns a deal readable name.
     *
     * @return  string
     */
    public function getName()
    {
        return 'VikWP - Test Deal'
    }

    /**
     * Returns the description of the deal
     *
     * @return  string
     */
    public function getDescription()
    {
        // return empty description
        return 'Deal for test purposes';
    }

    /**
     * Executes the rule before start checking for deals to apply.
     *
     * @param   TakeAwayCart  &$cart  The cart with the items.
     *
     * @return  void
     */
    public function preflight(&$cart)
    {
        /** 
         * @todo optional preflight method
         */
    }

    /**
     * Applies the deal to the cart instance, if needed.
     *
     * @param   TakeAwayCart  &$cart  The cart with the items.
     * @param   array         $deal   The deal to apply.
     *
     * @return  boolean       True if applied, false otherwise.
     */
    public function apply(&$cart, $deal)
    {
        /**
         * @todo apply the deal in case all the conditions
         *       have been satisfied
         */
    }
}

Changelog

Version Description
1.3 This filter has been officially deprecated and will be no longer supported starting from the 1.4 version.
1.0 Introduced.
Ultimo aggiornamento: 2023-12-15
Utile?
Potrebbe interessarti:
This site uses cookies. By continuing to browse you accept their use. Further information