Skip to content

Magento2 – some doubts using a before plugin

We want to add some logic when Magento saves shipping information during checkout, so we have tried using a before plugin for saveAddressInformation method

This would be method definition

/**
 * Save address information.
 *
 * @param int $cartId
 * @param ShippingInformationInterface $addressInformation
 * @return PaymentDetailsInterface
 * @throws InputException
 * @throws NoSuchEntityException
 * @throws StateException
 */
public function saveAddressInformation(
    $cartId,
    ShippingInformationInterface $addressInformation
): PaymentDetailsInterface {

vendor/magento/module-checkout/Model/ShippingInformationManagement.php

So, our plugin looks like this

public function beforeSaveAddressInformation(
    MagentoCheckoutModelShippingInformationManagement $subject,
    $cartId,
    MagentoCheckoutApiDataShippingInformationInterface $addressInformation
) {

Question would be, which data should we return during our custom code, if some validation fails? We’ve tried return $subject; but that throws this error, which I am not sure to understand

CRITICAL: TypeError: Argument 2 passed to
MagentoCheckoutModelShippingInformationManagement::saveAddressInformation()
must implement interface
MagentoCheckoutApiDataShippingInformationInterface, instance of
MagentoCheckoutModelPaymentDetailsFactory given, called in
/home/raulsanchez/workspace/artero/vendor/magento/framework/Interception/Interceptor.php
on line 58 and defined in
/home/raulsanchez/workspace/artero/vendor/magento/module-checkout/Model/ShippingInformationManagement.php:158

UPDATE

I have read in devdocs we should return an array including all arguments (which we are not doing by returning only $subject) but if so what I’d expect would be having some error of null given for second parameter, and not that instance of MagentoCheckoutModelPaymentDetailsFactory

Not sure if I understand what’s happening here. Any tips?