0

I've created a module to override

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\ShippingMethodManagement" type="Custom\FixShippingMethod\Model\ShippingMethodManagement"/>
</config>

class:

namespace Custom\FixShippingMethod\Model;

    class ShippingMethodManagement extends \Magento\Quote\Model\ShippingMethodManagement
    {
        /**
         * Get list of available shipping methods
         *
         * @param \Magento\Quote\Model\Quote $quote
         * @param \Magento\Framework\Api\ExtensibleDataInterface $address
         * @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
         */
        public function getShippingMethods(Quote $quote, $address)
        {

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

            $output = [];
            $shippingAddress = $quote->getShippingAddress();
            //Patch for Magento issue #26682
            /** @see https://github.com/magento/magento2/issues/26682 */
            $addressData = $this->extractAddressData($address);
            if (array_key_exists('extension_attributes', $addressData)) {
                unset($addressData['extension_attributes']);
            }
            $shippingAddress->addData($addressData);
            //End patch
            $shippingAddress->setCollectShippingRates(true);

            $this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
            $shippingRates = $shippingAddress->getGroupedAllShippingRates();
            foreach ($shippingRates as $carrierRates) {
                foreach ($carrierRates as $rate) {
                    $output[] = $this->converter->modelToDataObject($rate, $quote->getQuoteCurrencyCode());
                }
            }
            return $output;
        }

The method didn't run, anyone know what is the problem?

0

I think you try to override private methode from Magento\Quote\Model\ShippingMethodManagement

private function getShippingMethods(Quote $quote, $address)

you can't overriding private method with magento 2, it's non-public methods, you can refer to here for the documentation https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html

You can get solution from here Magento 2 Override Private method

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.