0

I am trying to save custom shipping address to quote. I've made a plugin for

Magento\Checkout\Api\ShippingInformationManagementInterface

on before saveAddressInformation method, and the new values are saved in quote_address table, however the plugin seems to be called twice and in the second time the shipping ata is again updated with the default shipping address. What is causing several plugin calls and how to save it in the last place? <?php

    declare(strict_types=1);

    namespace Namespace;

    use Magento\Checkout\Api\Data\ShippingInformationInterface;
    use Magento\Checkout\Api\ShippingInformationManagementInterface;
    use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
    use Magento\Quote\Api\CartRepositoryInterface;
    use Magento\Quote\Api\Data\CartInterface;
    use Magento\Quote\Model\ShippingAssignment;


    class ShippingInformationManagementPlugin 
    {

    private $quoteRepository;

    protected $jsonSerializer;

    public function __construct(
        CartRepositoryInterface $quoteRepository,
        JsonSerializer $jsonSerializer
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->jsonSerializer = $jsonSerializer;
    }

    public function beforeSaveAddressInformation(
        ShippingInformationManagementInterface $subject,
        $cartId,
        ShippingInformationInterface $addressInformation
    ) {
        $quote = $this->quoteRepository->getActive($cartId);
        if (!$quote->getId()) {
            throw new \Exception('Quote is missing!');
        }

        $shippingData = $this->extractShippingData($addressInformation);
        $pickupAddress = $shippingData->getInpostAddress();
        $shippingAddress = $quote->getShippingAddress();
        $shippingAddress->setStreet($pickupAddress['street']);
        $shippingAddress->setCity($pickupAddress['city']);
        $quote->setShippingAddress($shippingAddress);
        $this->cartRepository->save($quote);

    }

    private function extractShippingData($addressInformation) 
    {
        $baseServices = $addressInformation->getExtensionAttributes()->getBaseServices();
        return $baseServices[0];
    }
}

0

Your Answer

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

Browse other questions tagged or ask your own question.