0

I m trying to understand how plugins works with M2.

I have created a few plugins for some functions. Such as: addProduct setCustomerDataAsLoggedIn both taken from examples. These are working.

The one doesn't work is from ShippingInformationManagement. The function I m trying to catch is saveAddressInformation.

More or less the code is this: 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\Checkout\Model\ShippingInformationManagement">
        <plugin name="Vendor_WelcomePlugin::before_shipping" type="Vendor\WelcomePlugin\Plugin\ShippingInformationManagementPlugin" sortOrder="1"/>
    </type>
</config>

and the class:

namespace Vendor\WelcomePlugin\Plugin;

use \Magento\Checkout\Api\ShippingInformationManagementInterface;

class ShippingInformationManagementPlugin
{

    public function afterSaveAddressInformation(
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ){

        $address = $addressInformation->getShippingAddress();
        $billingAddress = $addressInformation->getBillingAddress();
        if($billingAddress->getCountryId() != 'US' || $address->getCountryId() != 'US'){
            throw new StateException(__('You can\'t order this book.'));
        }

        return array($cartId, $addressInformation);
    }

}

On the same topic I wasn't able to understand why I couldn't catch setCustomerAsLoggedIn function but I could make it work for setCustomerDataAsLoggedIn. Both are inside the Customer/Model/Session.php class

I have read the limitations of plugins but maybe I don't understand something. Thanks

1

Try following way:

Vendor/WelcomePlugin/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\ShippingInformationManagement">
    <plugin name="Vendor_WelcomePlugin::before_shipping" type="Vendor\WelcomePlugin\Plugin\ShippingInformationManagementPlugin" sortOrder="1"/>
</type>

Vendor/WelcomePlugin/Plugin/ShippingInformationManagementPlugin.php

namespace Vendor\WelcomePlugin\Plugin;

use Magento\Framework\Exception\StateException;

class ShippingInformationManagementPlugin
{
    public function aroundSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        \Closure $proceed,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ) {

        $address = $addressInformation->getShippingAddress();
        $billingAddress = $addressInformation->getBillingAddress();
        if($billingAddress->getCountryId() != 'US' || $address->getCountryId() != 'US'){
            throw new StateException(__('You can\'t order this book.'));
        }

        return $proceed($cartId, $addressInformation);
    }
}
  • No still doesn't work. Does it matter at which step is that function called? – vbak Jan 31 '17 at 19:51
  • Check file location and Clear cache. It should work. It's working fine here. – Sohel Rana Jan 31 '17 at 20:00
  • Right.Yes it is working even with after or before. Problem was the di.xml file that I put it inside the frontend folder.It needs to go inside etc. Thanks – vbak Jan 31 '17 at 20:04

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.