覆盖供应商模块(module-quote)模型文件Magento 2

g6baxovj  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(131)

我想覆盖module-quote模块我想覆盖具有函数protected submitQuote文件Model/QuoteManagment. php
我使用preference做了同样的事情。我遵循的步骤:
1.已创建自定义模块
1.已创建ect/di.xml文件

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Magento\Quote\Model\QuoteManagement" type="QuoteTracker\Module\Model\QuoteManagement" />

1.已创建module.xml文件

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="QuoteTracker_Module" setup_version="1.0.2" />
  <sequence>
         <module name="Magento_Backend"/>
          <module name="Magento_Sales"/>
         <module name="Magento_Quote"/>
         <module name="Magento_Checkout"/>>
     </sequence>
 </config>
 </config>

1.创建了Model/QuoteManagement.php文件,我想覆盖该文件以写入日志

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

 namespace QuoteTracker\Module\Model;

 use Magento\Quote\Model\QuoteManagement as MagentoQuoteManagement;
 use Magento\Authorization\Model\UserContextInterface;
 use Magento\Customer\Api\Data\GroupInterface;
 use Magento\Framework\App\ObjectManager;
 use Magento\Framework\Event\ManagerInterface as EventManager;
 use Magento\Framework\Exception\CouldNotSaveException;
 use Magento\Framework\Exception\LocalizedException;
 use Magento\Framework\Exception\StateException;
 use Magento\Quote\Api\Data\PaymentInterface;
 use Magento\Quote\Model\Quote\Address\ToOrder as ToOrderConverter;
 use Magento\Quote\Model\Quote\Address\ToOrderAddress as ToOrderAddressConverter;
 use Magento\Quote\Model\Quote as QuoteEntity;
 use Magento\Quote\Model\Quote\Item\ToOrderItem as ToOrderItemConverter;
 use Magento\Quote\Model\Quote\Payment\ToOrderPayment as ToOrderPaymentConverter;
 use Magento\Sales\Api\Data\OrderInterfaceFactory as OrderFactory;
 use Magento\Sales\Api\OrderManagementInterface as OrderManagement;
 use Magento\Store\Model\StoreManagerInterface;

 /**
  * Class QuoteManagement
  *
  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  * @SuppressWarnings(PHPMD.TooManyFields)
  */
 //class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
 class QuoteManagement extends MagentoQuoteManagement
 {
     /**
      * @var EventManager
      */
     protected $eventManager;

     /**
      * @var SubmitQuoteValidator
      */
     private $submitQuoteValidator;

     /**
      * @var OrderFactory
      */
     protected $orderFactory;

     /**
      * @var OrderManagement
      */
     protected $orderManagement;

     /**
      * @var CustomerManagement
      */
     protected $customerManagement;

     /**
      * @var ToOrderConverter
      */
     protected $quoteAddressToOrder;

     /**
      * @var ToOrderAddressConverter
      */
     protected $quoteAddressToOrderAddress;

     /**
      * @var ToOrderItemConverter
      */
     protected $quoteItemToOrderItem;

     /**
      * @var ToOrderPaymentConverter
      */
     protected $quotePaymentToOrderPayment;

     /**
      * @var UserContextInterface
      */
     protected $userContext;

     /**
      * @var \Magento\Quote\Api\CartRepositoryInterface
      */
     protected $quoteRepository;

     /**
      * @var \Magento\Customer\Api\CustomerRepositoryInterface
      */
     protected $customerRepository;

     /**
      * @var \Magento\Customer\Model\CustomerFactory
      */
     protected $customerModelFactory;

     /**
      * @var \Magento\Quote\Model\Quote\AddressFactory
      */
     protected $quoteAddressFactory;

     /**
      * @var \Magento\Framework\Api\DataObjectHelper
      */
     protected $dataObjectHelper;

     /**
      * @var StoreManagerInterface
      */
     protected $storeManager;

     /**
      * @var \Magento\Checkout\Model\Session
      */
     protected $checkoutSession;

     /**
      * @var \Magento\Customer\Model\Session
      */
     protected $customerSession;

     /**
      * @var \Magento\Customer\Api\AccountManagementInterface
      */
     protected $accountManagement;

     /**
      * @var QuoteFactory
      */
     protected $quoteFactory;

     /**
      * @var \Magento\Quote\Model\QuoteIdMaskFactory
      */
     private $quoteIdMaskFactory;

     /**
      * @var \Magento\Customer\Api\AddressRepositoryInterface
      */
     private $addressRepository;

     /**
      * @var array
      */
     private $addressesToSync = [];

     /**
      * @var \Magento\Framework\App\RequestInterface
      */
     private $request;

     /**
      * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
      */
     private $remoteAddress;

     /**
      * @param EventManager $eventManager
      * @param SubmitQuoteValidator $submitQuoteValidator
      * @param OrderFactory $orderFactory
      * @param OrderManagement $orderManagement
      * @param CustomerManagement $customerManagement
      * @param ToOrderConverter $quoteAddressToOrder
      * @param ToOrderAddressConverter $quoteAddressToOrderAddress
      * @param ToOrderItemConverter $quoteItemToOrderItem
      * @param ToOrderPaymentConverter $quotePaymentToOrderPayment
      * @param UserContextInterface $userContext
      * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
      * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
      * @param \Magento\Customer\Model\CustomerFactory $customerModelFactory
      * @param \Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory
      * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
      * @param StoreManagerInterface $storeManager
      * @param \Magento\Checkout\Model\Session $checkoutSession
      * @param \Magento\Customer\Model\Session $customerSession
      * @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
      * @param QuoteFactory $quoteFactory
      * @param \Magento\Quote\Model\QuoteIdMaskFactory|null $quoteIdMaskFactory
      * @param \Magento\Customer\Api\AddressRepositoryInterface|null $addressRepository
      * @param \Magento\Framework\App\RequestInterface|null $request
      * @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
      * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
     public function __construct(
         EventManager $eventManager,
         SubmitQuoteValidator $submitQuoteValidator,
         OrderFactory $orderFactory,
         OrderManagement $orderManagement,
         CustomerManagement $customerManagement,
         ToOrderConverter $quoteAddressToOrder,
         ToOrderAddressConverter $quoteAddressToOrderAddress,
         ToOrderItemConverter $quoteItemToOrderItem,
         ToOrderPaymentConverter $quotePaymentToOrderPayment,
         UserContextInterface $userContext,
         \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
         \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
         \Magento\Customer\Model\CustomerFactory $customerModelFactory,
         \Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory,
         \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
         StoreManagerInterface $storeManager,
         \Magento\Checkout\Model\Session $checkoutSession,
         \Magento\Customer\Model\Session $customerSession,
         \Magento\Customer\Api\AccountManagementInterface $accountManagement,
         \Magento\Quote\Model\QuoteFactory $quoteFactory,
         \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory = null,
         \Magento\Customer\Api\AddressRepositoryInterface $addressRepository = null,
         \Magento\Framework\App\RequestInterface $request = null,
         \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress = null
     ) {
         $this->eventManager = $eventManager;
         $this->submitQuoteValidator = $submitQuoteValidator;
         $this->orderFactory = $orderFactory;
         $this->orderManagement = $orderManagement;
         $this->customerManagement = $customerManagement;
         $this->quoteAddressToOrder = $quoteAddressToOrder;
         $this->quoteAddressToOrderAddress = $quoteAddressToOrderAddress;
         $this->quoteItemToOrderItem = $quoteItemToOrderItem;
         $this->quotePaymentToOrderPayment = $quotePaymentToOrderPayment;
         $this->userContext = $userContext;
         $this->quoteRepository = $quoteRepository;
         $this->customerRepository = $customerRepository;
         $this->customerModelFactory = $customerModelFactory;
         $this->quoteAddressFactory = $quoteAddressFactory;
         $this->dataObjectHelper = $dataObjectHelper;
         $this->storeManager = $storeManager;
         $this->checkoutSession = $checkoutSession;
         $this->accountManagement = $accountManagement;
         $this->customerSession = $customerSession;
         $this->quoteFactory = $quoteFactory;
         $this->quoteIdMaskFactory = $quoteIdMaskFactory ?: ObjectManager::getInstance()
             ->get(\Magento\Quote\Model\QuoteIdMaskFactory::class);
         $this->addressRepository = $addressRepository ?: ObjectManager::getInstance()
             ->get(\Magento\Customer\Api\AddressRepositoryInterface::class);
         $this->request = $request ?: ObjectManager::getInstance()
             ->get(\Magento\Framework\App\RequestInterface::class);
         $this->remoteAddress = $remoteAddress ?: ObjectManager::getInstance()
             ->get(\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class);
     }

     /**
      * Submit quote
      *
      * @param Quote $quote
      * @param array $orderData
      * @return \Magento\Framework\Model\AbstractExtensibleModel|\Magento\Sales\Api\Data\OrderInterface|object
      * @throws \Exception
      * @throws \Magento\Framework\Exception\LocalizedException
      */
     protected function submitQuote(QuoteEntity $quote, $orderData = [])
     {
         $order = $this->orderFactory->create();
         $this->submitQuoteValidator->validateQuote($quote);
         if (!$quote->getCustomerIsGuest()) {
             if ($quote->getCustomerId()) {
                 $this->_prepareCustomerQuote($quote);
                 $this->customerManagement->validateAddresses($quote);
             }
             $this->customerManagement->populateCustomerInfo($quote);
         }
         $addresses = [];
         $quote->reserveOrderId();
         if ($quote->isVirtual()) {
             $this->dataObjectHelper->mergeDataObjects(
                 \Magento\Sales\Api\Data\OrderInterface::class,
                 $order,
                 $this->quoteAddressToOrder->convert($quote->getBillingAddress(), $orderData)
             );
         } else {
             $this->dataObjectHelper->mergeDataObjects(
                 \Magento\Sales\Api\Data\OrderInterface::class,
                 $order,
                 $this->quoteAddressToOrder->convert($quote->getShippingAddress(), $orderData)
             );
             $shippingAddress = $this->quoteAddressToOrderAddress->convert(
                 $quote->getShippingAddress(),
                 [
                     'address_type' => 'shipping',
                     'email' => $quote->getCustomerEmail()
                 ]
             );
             $shippingAddress->setData('quote_address_id', $quote->getShippingAddress()->getId());
             $addresses[] = $shippingAddress;
             $order->setShippingAddress($shippingAddress);
             $order->setShippingMethod($quote->getShippingAddress()->getShippingMethod());
         }
         $billingAddress = $this->quoteAddressToOrderAddress->convert(
             $quote->getBillingAddress(),
             [
                 'address_type' => 'billing',
                 'email' => $quote->getCustomerEmail()
             ]
         );
         $billingAddress->setData('quote_address_id', $quote->getBillingAddress()->getId());
         $addresses[] = $billingAddress;
         $order->setBillingAddress($billingAddress);
         $order->setAddresses($addresses);
         $order->setPayment($this->quotePaymentToOrderPayment->convert($quote->getPayment()));
         $order->setItems($this->resolveItems($quote));
         if ($quote->getCustomer()) {
             $order->setCustomerId($quote->getCustomer()->getId());
         }
         $order->setQuoteId($quote->getId());
         $order->setCustomerEmail($quote->getCustomerEmail());
         $order->setCustomerFirstname($quote->getCustomerFirstname());
         $order->setCustomerMiddlename($quote->getCustomerMiddlename());
         $order->setCustomerLastname($quote->getCustomerLastname());
         $this->submitQuoteValidator->validateOrder($order);

         $this->eventManager->dispatch(
             'sales_model_service_quote_submit_before',
             [
                 'order' => $order,
                 'quote' => $quote
             ]
         );

         $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/order-tracker.log');
         $logger = new \Zend\Log\Logger();
         $logger->addWriter($writer);

         try {
             $order = $this->orderManagement->place($order);

             $logger->info("Order placed sucessfully. - Order #".$order->getIncrementId());
             $logger->info(json_encode($order->getData()));

             $quote->setIsActive(false);
             $this->eventManager->dispatch(
                 'sales_model_service_quote_submit_success',
                 [
                     'order' => $order,
                     'quote' => $quote
                 ]
             );
             $this->quoteRepository->save($quote);
         } catch (\Exception $e) {
             $this->rollbackAddresses($quote, $order, $e);

             $logger->info("Order placed unsucessfully. - Order #".$order->getIncrementId());
             $logger->info($e->getMessage());

             throw $e;
         }
         return $order;
     }
 }

运行setup:di:compile时,在此函数中调用的多个依赖项文件出现错误。请共享覆盖此模型文件的方法。

niknxzdl

niknxzdl1#

请将module.xml文件更改为以下文件并重试:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="QuoteTracker_Module" setup_version="1.0.2">
        <sequence>
            <module name="Magento_Backend"/>
            <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

相关问题