2

I create plugin in my custom module to override the method collectCarrierRates($carrierCode, $request), below code is not working. What mistake here?

app/code/Vendor/Shippingtest/etc/frontend/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">
    <type name="Magento\Shipping\Model\Shipping">
        <plugin name="vendor-shippingtest-test"
                type="Vendor\Shippingtest\Plugin\Test"
                sortOrder="10"/>
    </type>
</config>

app/code/Vendor/Shippingtest/Plugin/Test.php

<?php

namespace Vendor\Shippingtest\Plugin;

class Test
{
    public function after(Magento\Shipping\Model\Shipping $subject, $result)
    {
        echo "Calling";
    }
}
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
Payal Patel
  • 1,511
  • 1
  • 20
  • 40

1 Answers1

3

I'm not entirely sure what you're trying to achieve but there's a major problem in your method name, it does not contain the name of the original method.

You should replace:

public function after(Magento\Shipping\Model\Shipping $subject, $result)

With:

public function afterCollectCarrierRates(Magento\Shipping\Model\Shipping $subject, $result)

So your plugin can work.

Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
  • Hello Raphael,thanks to reply. I try to use your code, but same here plugin is not working. – Payal Patel Sep 05 '16 at 10:27
  • @PayalPatel I'm not sure an echo would be enough to show your the code is working. Try to log data by using the following method http://magento.stackexchange.com/questions/92434/magento-2-replacement-for-magelog-method – Raphael at Digital Pianism Sep 05 '16 at 10:30
  • Try to log data but log file is not create in var/log folder. – Payal Patel Sep 05 '16 at 10:36
  • @PayalPatel How are you testing your module ? Are you sure you follow a testing procedure that triggers the original collectCarrierRates method ? – Raphael at Digital Pianism Sep 05 '16 at 10:38
  • Ya i am sure for collectCarrierRates method. I try to update display error message in checkout/cart/ page. I already ask question previously. But no any answer get here http://magento.stackexchange.com/questions/134461/magento-2-1-update-display-error-message-in-checkout-cart-page – Payal Patel Sep 05 '16 at 10:46
  • 1
    @PayalPatel maybe try to move your di.xml outside the frontend folder – Raphael at Digital Pianism Sep 05 '16 at 10:47