Apply custom promo rule using observer in Magento 2

1.2k views Asked by At

I have created new table in Magento 2, for storing custom promorules, now I have created observer for event controller_action_predispatch_checkout_cart_couponPost and inside that observer I want to check if user has entered any custom rule then apply discount according to that rule.

Below is my code:

<?php
namespace Webkul\Grid\Observer;

use Magento\Framework\Event\ObserverInterface;

class coupenAppliedAfter implements ObserverInterface
{
    /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager
    ) {
        $this->_objectManager = $objectManager;
    }

    /**
     * customer register event handler
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // get enetered coupen code
        $controller = $observer->getControllerAction();
        $couponCode = $controller->getRequest()->getParam('coupon_code');

        $objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
        $connection = $objectManager->get('Magento\Framework\App\ResourceConnection')->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION'); 
        // get list of coupon codes from that custom table
        $all_custom_codes = $connection->fetchAll("SELECT * FROM custom_promotion_rules");

        foreach($all_custom_codes as $code) {
            $db_coupen_code = $code['code'];
            // matching if user has entered any custom code 
            if($couponCode == $db_coupen_code) {
                // if yes trying to apply custom discount
                $DiscountAmount = $code['discount_amount'];
                $result = $observer->getEvent()->getResult();
                $result->setAmount($DiscountAmount);
                $result->setBaseAmount($DiscountAmount); 
            }
        }

    }
}

but above code is not working and giving error Fatal error: Uncaught Error: Call to a member function setAmount() on null

Please suggest solution for this.

2

There are 2 answers

0
Andrew On

The issue is that you're trying to use result which is not passed as a parameter to controller pre dispatch events

$observer->getEvent()->getResult(); // always null.

if you check the front controller that triggers the predipatch event(s)

vendor/magento/framework/App/FrontController.php

you will notice that two objects are passed to the event:

['controller_action' => $subject, 'request' => $this->request];

I think you're getting confused with plugins, they can provide a $result from the method being plugged into, whereas events can have different parameters passed to them via the event dispatcher.

0
TheRealJAG On

$result is null. Are you trying to run this function manually? If you are, that could be your problem since $observer would be empty or null.

I would also move the initialization of $result above the foreach statement.