public function onSetupTakeawayDeals( DealsFactory $factory ) : void

Fires while constructing the object used to provide the deal handlers.


Description

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

In order to support new deal types, you need to attach your classes to the $factory object, which can be done through the code below.

$factory->registerDealProvider('custom_offer', function(array $options) {
    require_once dirname(__FILE__) . '/rules/CustomOfferDealRule.php';
    return new CustomOfferDealRule($options);
});

 The registered deals must inherit the E4J\VikRestaurants\Deals\DealRule class, otherwise they will be ignored.


Parameters

$factory

(DealFactory)  The object used to create the available deal providers. This class is part of the E4J\VikRestaurants\Deals namespace.

Return Value

None.


Example

The example below explains how to support a custom deal.

/**
 * Trigger event to let the plugins register new deal rules.
 *
 * @param   E4J\VikRestaurants\Deals\DealsFactory  $factory
 *
 * @return  void
 */
public function onSetupTakeawayDeals($factory)
{
    // register a new deal with id "custom_offer"
    $factory->registerDealProvider('custom_offer', function(array $options) {
        // anonymous classes are supported too, but still need to inherit the DealRule class
        return new class ($options) extends E4J\VikRestaurants\Deals\DealRule {
            /**
             * Returns the deal code identifier.
             *
             * @return  string
             */
            public function getID()
            {
                return 'custom_offer';
            }

            /**
             * Returns a deal readable name.
             *
             * @return  string
             */
            public function getName()
            {
                return 'Custom Offer';
            }

            /**
             * Returns the description of the deal.
             *
             * @return  string
             */
            public function getDescription()
            {
                return 'Describe here the purpose of this deal. For administrative purposes only.';
            }

            /**
             * Executes the rule before start checking whether this rule can be applied or not.
             * This method is called only once.
             *
             * @param   Cart  $cart  The cart with the ordered items.
             *
             * @return  void
             */
            public function prepare($cart)
            {
                // set up the deal if needed
            }

            /**
             * Applies the deal to the cart instance, if needed.
             * This method is called a number of times equals to the compatible deals
             * configured from the back-end.
             *
             * @param   Cart  $cart  The cart with the items.
             * @param   Deal  $deal  The deal to apply.
             *
             * @return  bool  True if applied, false otherwise.
             */
            public function serve($cart, $deal)
            {
                $should_apply = false;

                /**
                 * @todo check whether the deal should be applied
                 */

                // check whether the same discount has been already applied
                $index = $cart->deals->indexOfType($deal->get('type'));

                if ($should_apply) {
                    // create discount
                    $discount = new E4J\VikRestaurants\TakeAway\Cart\Deals\Discount([
                        'id'      => $deal->get('id'),
                        'amount'  => 20,
                        'percent' => true,
                        'type'    => $deal->get('type'),
                    ]);

                    // Register the discount into the cart.
                    // In case there are no discount at the specified index,
                    // the discount will be inserted instead.
                    $cart->deals()->set($discount, $index);
                } elseif ($index !== false) {
                    // the discount should be removed from the cart
                    $cart->deals->removeAt($index);
                }

                return $should_apply;
            }
        };
    });
}

Changelog

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