我正在尝试重写Magento\Quote\Model\Cart\ShippingMethodManagement,以拦截apply函数中抛出的发货地址错误。
我的app/code/vendor/module/etc/frontend/di.xml是这样的:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Quote\Model\ShippingMethodMangement" type="Vendor\Module\Model\Rewrite\ShippingMethodMangement" />
</config>和app/code/Vendor/Model/Rewrite/ShippingMethodManagement.php
<?php
namespace Vendor\Module\Model\Rewrite;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\EstimateAddressInterface;
use Magento\Quote\Api\ShipmentEstimationInterface;
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
/**
* Shipping method read service
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ShippingMethodManagement extends \Magento\Quote\Model\ShippingMethodManagement implements
\Magento\Quote\Api\ShippingMethodManagementInterface,
\Magento\Quote\Model\ShippingMethodManagementInterface,
ShipmentEstimationInterface
{
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* Shipping method converter
*
* @var \Magento\Quote\Model\Cart\ShippingMethodConverter
*/
protected $converter;
/**
* Customer Address repository
*
* @var \Magento\Customer\Api\AddressRepositoryInterface
*/
protected $addressRepository;
/**
* @var Quote\TotalsCollector
*/
protected $totalsCollector;
/**
* @var \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor
*/
private $dataProcessor;
/**
* @var AddressInterfaceFactory $addressFactory
*/
private $addressFactory;
/**
* @var QuoteAddressResource
*/
private $quoteAddressResource;
/**
* Constructor
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
* @param Cart\ShippingMethodConverter $converter
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
* @param Quote\TotalsCollector $totalsCollector
* @param AddressInterfaceFactory|null $addressFactory
* @param QuoteAddressResource|null $quoteAddressResource
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
\Magento\Quote\Model\Cart\ShippingMethodConverter $converter,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
\Magento\Quote\Model\Quote\TotalsCollector $totalsCollector,
AddressInterfaceFactory $addressFactory = null,
QuoteAddressResource $quoteAddressResource = null
) {
$this->quoteRepository = $quoteRepository;
$this->converter = $converter;
$this->addressRepository = $addressRepository;
$this->totalsCollector = $totalsCollector;
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
->get(AddressInterfaceFactory::class);
$this->quoteAddressResource = $quoteAddressResource ?: ObjectManager::getInstance()
->get(QuoteAddressResource::class);
}
/**
* {@inheritDoc}
*/
public function apply($cartId, $carrierCode, $methodCode)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if (0 == $quote->getItemsCount()) {
throw new InputException(
__('The shipping method can\'t be set for an empty cart. Add an item to cart and try again.')
);
}
if ($quote->isVirtual()) {
throw new NoSuchEntityException(
__('The Cart includes virtual product(s) only, so a shipping address is not used.')
);
}
$shippingAddress = $quote->getShippingAddress();
if (!$shippingAddress->getCountryId()) {
// Remove empty quote address
$this->quoteAddressResource->delete($shippingAddress);
throw new StateException(__('*** The shipping address is missing. Set the address and try again.'));
}
$shippingAddress->setShippingMethod($carrierCode . '_' . $methodCode);
}
}我已经运行了rm -r var/generated,s:up,s:d:c,c:f,s:s:d
编译时不会抛出错误,但模型覆盖不起作用。apply方法在此文件中未命中,仍从Magento\Quote\Model\ShippingMethodManagement运行
有人能解释一下为什么这个重写不起作用吗?
发布于 2021-02-09 05:49:59
尝试将di.xml放在以下路径中:
app/code/Vendor/Module/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Quote\Model\ShippingMethodMangement" type="Vendor\Module\Model\Rewrite\ShippingMethodMangement" />
</config>https://stackoverflow.com/questions/66106144
复制相似问题