cancel
Showing results for 
Search instead for 
Did you mean: 

Can't Override or Plugin Magento\Quote\Model\Cart\ShippingMethodManagement

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Can't Override or Plugin Magento\Quote\Model\Cart\ShippingMethodManagement

I must be not understanding something. When I try to override or extend via plugin the Magento\Quote\Model\Cart\ShippingMethodManagement class it just doesn't work. Here's my code:

frontend/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\ShippingMethodManagement">
    <plugin name="vnsShippingmethodmanagement" type="VNS\Shippingquote\Model\ShippingMethodManagement" sortOrder="1"/>
</type>

 

Model/ShippingMethodManagement.php

public function __construct(    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository) {    $this->productRepository = $productRepository;
}

private function afterGetShippingMethods(Quote $quote, $output)
{    error_log('test');
}

Module is installed/enabled, cache cleared, genereated/ deleted.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Can't Override or Plugin Magento\Quote\Model\Cart\ShippingMethodManagement

Hi 

I see this is private method so you can not use Plugin for this case. 

Plugins cannot be used with any of the following:

  • Final methods
  • Final classes
  • Non-public methods
  • Static methods
  • __construct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped
  • Objects that are not instantiated by the ObjectManager (e.g. by using new directly).

You can read more on Magento document https://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html

 

View solution in original post

4 REPLIES 4

Re: Can't Override or Plugin Magento\Quote\Model\Cart\ShippingMethodManagement

Hi 

I see this is private method so you can not use Plugin for this case. 

Plugins cannot be used with any of the following:

  • Final methods
  • Final classes
  • Non-public methods
  • Static methods
  • __construct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped
  • Objects that are not instantiated by the ObjectManager (e.g. by using new directly).

You can read more on Magento document https://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html

 

Re: Can't Override or Plugin Magento\Quote\Model\Cart\ShippingMethodManagement

Thanks for this. That makes sense. Is this also why I can't override it using <preference?

Re: Can't Override or Plugin Magento\Quote\Model\Cart\ShippingMethodManagement

This is solved now. I found out that to override it I need to include all the public and private functions so I basically copied ShippingMethodManagement.php from the core to my module and added the things I needed. It's working great now. Thanks!

Re: Can't Override or Plugin Magento\Quote\Model\ShippingMethodManagement

I am also overriding same function can you tell how you override it.

I also used the preference method but getting the type error and then run di:compile command but its not solve that issue .

 

Can you tell me how you solved that