0
votes

"Fatal Error: 'Uncaught TypeError: Argument 1 passed to Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver::execute() must be an instance of Magento\Framework\Event\Observer, null given, called in /home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php on line 58 and defined in /home/kerstinf/public_html/dev/vendor/magento/module-sales-rule/Observer/SalesOrderAfterPlaceObserver.php:58\nStack trace:\n#0 /home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php(58): Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver->execute(NULL)\n#1 /home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php(138): Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver\Interceptor->___callParent('execute', Array)\n#2 /home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php(153): Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\Event\Observer))' in '/home/kerstinf/public_html/dev/vendor/magento/module-sales-rule/Observer/SalesOrderAfterPlaceObserver.php' on line 58"

I am passing argument to SalesOrderAfterPlaceObserver class method but it is still giving me error

my SalesOrderAfterPlaceObserver class is

<?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Magento\SalesRule\Observer;

    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;

    class SalesOrderAfterPlaceObserver implements ObserverInterface
    {
        /**
         * @var \Magento\SalesRule\Model\RuleFactory
         */
        protected $_ruleFactory;

        /**
         * @var \Magento\SalesRule\Model\RuleFactory
         */
        protected $_ruleCustomerFactory;

        /**
         * @var \Magento\SalesRule\Model\Coupon
         */
        protected $_coupon;
        protected $_observer;
        /**
         * @var \Magento\SalesRule\Model\ResourceModel\Coupon\Usage
         */
        protected $_couponUsage;

        /**
         * @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
         * @param \Magento\SalesRule\Model\Rule\CustomerFactory $ruleCustomerFactory
         * @param \Magento\SalesRule\Model\Coupon $coupon
         * @param \Magento\SalesRule\Model\ResourceModel\Coupon\Usage $couponUsage
         */
        public function __construct(
            \Magento\Framework\Event\Observer $observer,
            \Magento\SalesRule\Model\RuleFactory $ruleFactory,
            \Magento\SalesRule\Model\Rule\CustomerFactory $ruleCustomerFactory,
            \Magento\SalesRule\Model\Coupon $coupon,
            \Magento\SalesRule\Model\ResourceModel\Coupon\Usage $couponUsage
        ) {
            $this->_observer = $observer;
            $this->_ruleFactory = $ruleFactory;
            $this->_ruleCustomerFactory = $ruleCustomerFactory;
            $this->_coupon = $coupon;
            $this->_couponUsage = $couponUsage;
        }

        /**
         * @param EventObserver $observer
         * @return $this
         * @SuppressWarnings(PHPMD.CyclomaticComplexity)
         */
        public function execute(Observer $observer)
        {
            $observer = $this->_observer;
            $order = $observer->getEvent()->getOrder();

            if (!$order || !$order->getAppliedRuleIds()) {
                return $this;
            }

            // lookup rule ids
            $ruleIds = explode(',', $order->getAppliedRuleIds());
            $ruleIds = array_unique($ruleIds);

            $ruleCustomer = null;
            $customerId = $order->getCustomerId();

            // use each rule (and apply to customer, if applicable)
            foreach ($ruleIds as $ruleId) {
                if (!$ruleId) {
                    continue;
                }
                /** @var \Magento\SalesRule\Model\Rule $rule */
                $rule = $this->_ruleFactory->create();
                $rule->load($ruleId);
                if ($rule->getId()) {
                    $rule->loadCouponCode();
                    $rule->setTimesUsed($rule->getTimesUsed() + 1);
                    $rule->save();

                    if ($customerId) {
                        /** @var \Magento\SalesRule\Model\Rule\Customer $ruleCustomer */
                        $ruleCustomer = $this->_ruleCustomerFactory->create();
                        $ruleCustomer->loadByCustomerRule($customerId, $ruleId);

                        if ($ruleCustomer->getId()) {
                            $ruleCustomer->setTimesUsed($ruleCustomer->getTimesUsed() + 1);
                        } else {
                            $ruleCustomer->setCustomerId($customerId)->setRuleId($ruleId)->setTimesUsed(1);
                        }
                        $ruleCustomer->save();
                    }
                }
            }

            $this->_coupon->load($order->getCouponCode(), 'code');
            if ($this->_coupon->getId()) {
                $this->_coupon->setTimesUsed($this->_coupon->getTimesUsed() + 1);
                $this->_coupon->save();
                if ($customerId) {
                    $this->_couponUsage->updateCustomerCouponTimesUsed($customerId, $this->_coupon->getId());
                }
            }

            return $this;
        }
    }

I am not able to find a solution to this problem on google and stackoverflow.

1

1 Answers

0
votes

You injected the constructor. Please try to run this command, if your still facing issue means clear var/di folder and re-run the same command again. I hope this will helpful for you

php bin/magento setup:di:compile