#php #magento #magento2
Вопрос:
Я хочу переопределить модуль-модуль цитирования. Я хочу переопределить файл Model/QuoteManagment.php имеющая защищенную функцию submitQuote
Я сделал то же самое, используя предпочтения. Шаги, которым я следовал:
- Создал пользовательский модуль
- Создано ect/di.xml файл
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="MagentoQuoteModelQuoteManagement" type="QuoteTrackerModuleModelQuoteManagement" />
- Создано 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>
- Создано Model/QuoteManagement.php файл, который я хочу переопределить для записи журналов
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace QuoteTrackerModuleModel; use MagentoQuoteModelQuoteManagement as MagentoQuoteManagement; use MagentoAuthorizationModelUserContextInterface; use MagentoCustomerApiDataGroupInterface; use MagentoFrameworkAppObjectManager; use MagentoFrameworkEventManagerInterface as EventManager; use MagentoFrameworkExceptionCouldNotSaveException; use MagentoFrameworkExceptionLocalizedException; use MagentoFrameworkExceptionStateException; use MagentoQuoteApiDataPaymentInterface; use MagentoQuoteModelQuoteAddressToOrder as ToOrderConverter; use MagentoQuoteModelQuoteAddressToOrderAddress as ToOrderAddressConverter; use MagentoQuoteModelQuote as QuoteEntity; use MagentoQuoteModelQuoteItemToOrderItem as ToOrderItemConverter; use MagentoQuoteModelQuotePaymentToOrderPayment as ToOrderPaymentConverter; use MagentoSalesApiDataOrderInterfaceFactory as OrderFactory; use MagentoSalesApiOrderManagementInterface as OrderManagement; use MagentoStoreModelStoreManagerInterface; /** * Class QuoteManagement * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) */ //class QuoteManagement implements MagentoQuoteApiCartManagementInterface 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 MagentoQuoteApiCartRepositoryInterface */ protected $quoteRepository; /** * @var MagentoCustomerApiCustomerRepositoryInterface */ protected $customerRepository; /** * @var MagentoCustomerModelCustomerFactory */ protected $customerModelFactory; /** * @var MagentoQuoteModelQuoteAddressFactory */ protected $quoteAddressFactory; /** * @var MagentoFrameworkApiDataObjectHelper */ protected $dataObjectHelper; /** * @var StoreManagerInterface */ protected $storeManager; /** * @var MagentoCheckoutModelSession */ protected $checkoutSession; /** * @var MagentoCustomerModelSession */ protected $customerSession; /** * @var MagentoCustomerApiAccountManagementInterface */ protected $accountManagement; /** * @var QuoteFactory */ protected $quoteFactory; /** * @var MagentoQuoteModelQuoteIdMaskFactory */ private $quoteIdMaskFactory; /** * @var MagentoCustomerApiAddressRepositoryInterface */ private $addressRepository; /** * @var array */ private $addressesToSync = []; /** * @var MagentoFrameworkAppRequestInterface */ private $request; /** * @var MagentoFrameworkHTTPPhpEnvironmentRemoteAddress */ 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 MagentoQuoteApiCartRepositoryInterface $quoteRepository * @param MagentoCustomerApiCustomerRepositoryInterface $customerRepository * @param MagentoCustomerModelCustomerFactory $customerModelFactory * @param MagentoQuoteModelQuoteAddressFactory $quoteAddressFactory * @param MagentoFrameworkApiDataObjectHelper $dataObjectHelper * @param StoreManagerInterface $storeManager * @param MagentoCheckoutModelSession $checkoutSession * @param MagentoCustomerModelSession $customerSession * @param MagentoCustomerApiAccountManagementInterface $accountManagement * @param QuoteFactory $quoteFactory * @param MagentoQuoteModelQuoteIdMaskFactory|null $quoteIdMaskFactory * @param MagentoCustomerApiAddressRepositoryInterface|null $addressRepository * @param MagentoFrameworkAppRequestInterface|null $request * @param MagentoFrameworkHTTPPhpEnvironmentRemoteAddress $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, MagentoQuoteApiCartRepositoryInterface $quoteRepository, MagentoCustomerApiCustomerRepositoryInterface $customerRepository, MagentoCustomerModelCustomerFactory $customerModelFactory, MagentoQuoteModelQuoteAddressFactory $quoteAddressFactory, MagentoFrameworkApiDataObjectHelper $dataObjectHelper, StoreManagerInterface $storeManager, MagentoCheckoutModelSession $checkoutSession, MagentoCustomerModelSession $customerSession, MagentoCustomerApiAccountManagementInterface $accountManagement, MagentoQuoteModelQuoteFactory $quoteFactory, MagentoQuoteModelQuoteIdMaskFactory $quoteIdMaskFactory = null, MagentoCustomerApiAddressRepositoryInterface $addressRepository = null, MagentoFrameworkAppRequestInterface $request = null, MagentoFrameworkHTTPPhpEnvironmentRemoteAddress $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(MagentoQuoteModelQuoteIdMaskFactory::class); $this->addressRepository = $addressRepository ?: ObjectManager::getInstance() ->get(MagentoCustomerApiAddressRepositoryInterface::class); $this->request = $request ?: ObjectManager::getInstance() ->get(MagentoFrameworkAppRequestInterface::class); $this->remoteAddress = $remoteAddress ?: ObjectManager::getInstance() ->get(MagentoFrameworkHTTPPhpEnvironmentRemoteAddress::class); } /** * Submit quote * * @param Quote $quote * @param array $orderData * @return MagentoFrameworkModelAbstractExtensibleModel|MagentoSalesApiDataOrderInterface|object * @throws Exception * @throws MagentoFrameworkExceptionLocalizedException */ 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( MagentoSalesApiDataOrderInterface::class, $order, $this->quoteAddressToOrder->convert($quote->getBillingAddress(), $orderData) ); } else { $this->dataObjectHelper->mergeDataObjects( MagentoSalesApiDataOrderInterface::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 ZendLogWriterStream(BP . '/var/log/order-tracker.log'); $logger = new ZendLogLogger(); $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 я получаю ошибки для нескольких файлов зависимостей, которые вызываются в этой функции. Пожалуйста, поделитесь способом переопределения этого файла модели.
Ответ №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>
</module>
</config>