Magento 2在下订单和重定向到购物车时出现致命错误

时间:2018-05-20 17:36:28

标签: php magento2

  

"致命错误:'未捕获的TypeError:传递给Magento \ SalesRule \ Observer \ SalesOrderAfterPlaceObserver :: execute()的参数1必须是Magento \ Framework \ Event \ Observer的实例,null给定,调用在第58行的/home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php中,在/ home / kerstinf / public_html / dev / vendor / magento / module-sales-rule / Observer / SalesOrderAfterPlaceObserver中定义。 php:58 \ n堆栈跟踪:\ n#0 /home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php(58):Magento \ SalesRule \ Observer \ SalesOrderAfterPlaceObserver->执行(NULL)\ n#1 /home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php(138):Magento \ SalesRule \ Observer \ SalesOrderAfterPlaceObserver \ Interceptor-> ___ callParent(' execute',数组)\ n#2 /home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php(153):Magento \ SalesRule \ Observer \ SalesOrderAfterPlaceObserver \ Interceptor-> Magento \ Fra mework \拦截\ {闭合}(对象(Magento的\框架\事件\观察员))' in' /home/kerstinf/public_html/dev/vendor/magento/module-sales-rule/Observer/SalesOrderAfterPlaceObserver.php'在第58行"

我将参数传递给SalesOrderAfterPlaceObserver类方法,但它仍然给我错误

我的SalesOrderAfterPlaceObserver类是

<?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;
        }
    }

我无法在google和stackoverflow上找到解决此问题的方法。

1 个答案:

答案 0 :(得分:0)

您注入了构造函数。如果仍然面临问题意味着清除var/di文件夹并再次重新运行相同的命令,请尝试运行此命令。我希望这对你有所帮助

php bin/magento setup:di:compile