Php Magento 2在下订单和重定向到购物车时出现致命错误
“致命错误:'未捕获的TypeError:参数1传递给Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver::execute()必须是Magento\Framework\Event\Observer的实例,为空,在第58行的/home/kerstinf/public_html/dev/vendor/Magento/Framework/intercept/Interceptor.php中调用,并在/home/kerstinf/public_html/dev/vendor/Magento/module sales-rule/Observer/salesforderafterplaceobserver.php:58\n堆栈跟踪:\n#0/home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php(58):magento\SalesRule\Observer\saleorderafterplaceobserver->执行(NULL)\n\1/home/kerstinf/public\html/dev/vendor/magento/framework/Interception/Interceptor.php(138):magento\SalesRule\Observer\saleorderafterplaceobserver\Interceptor->\uuuuuuuuuu callParent('execute',数组)\n#2/home/kerstinf/public_html/dev/vendor/magento/framework/Interception/Interceptor.php(153):magento\SalesRule\Observer\salesforderafterplaceobserver\Interceptor->magento\framework\Interception\{closure}(对象(magento\framework\Event\Observer))“在第58行的“/home/kerstinf/public_html/dev/vendor/magento/module sales rule/Observer/salesforderafterplaceobserver.php”中” 我正在将参数传递给SalesOrderAfterPlaceObserver类方法,但它仍然给我错误信息 我的SalesOrderAfterPlaceObserver类是Php Magento 2在下订单和重定向到购物车时出现致命错误,php,magento2,Php,Magento2,“致命错误:'未捕获的TypeError:参数1传递给Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver::execute()必须是Magento\Framework\Event\Observer的实例,为空,在第58行的/home/kerstinf/public_html/dev/vendor/Magento/Framework/intercept/Interceptor.php中调用,并在/home/kerstinf/public_
<?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;
}
}
您注入了构造函数。如果您仍然面临的问题意味着清除var/di
文件夹,请尝试运行此命令,然后再次运行相同的命令。我希望这对你有帮助
php bin/magento setup:di:compile
这是magento 2.2版本吗?它是2.2.3版本。我这样做了,但不起作用,我找到了一个解决办法,interceptor.php传递了一个空参数,所以我删除了这个参数,也从SalesOrderAfterPlaceObserver类中删除了它,现在它开始工作了。