0

I need to change the value of the shipping cost when the user select the shipping address on my magento 2 checkout. Using some params of the products they are buying and the params of the shipping address.

So I decide to create a Plugin to execute some code before and after shipping is selected. Reading on magento 2 documentation (Plugins (Interceptors) | Magento 2 Developer Documentation) I created an aroundMethod(). Well I created and I added only the sample code I got from the documentation and a simple var_dump() but when I execute the code on my checkout I get this javascript error:

Cannot read property 'items' of undefined at ko.subscription.callback (totals.js:18)

This seems like my plugin doesn't return something of necessary.

This is my Plugin:

The declaration on <vendor>\<module>\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/con
fig.xsd">
     <type name="Magento\Checkout\Model\ShippingInformationManagement">
         <plugin name="get_shipping_info" type="<vendor>\<module>\Plugin\ShippingCalculate" sortOrder="1" disabled="false"/>
     </type>
</config>

Then my plugin code <vendor>\<module>\Plugin\ShippingCalculate

<?php

namespace <vendor>\<module>\Plugin;

class ShippingCalculate
{

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_checkoutSession;

    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $_request;

    /**
     * @var \Magento\Quote\Api\CartRepositoryInterface
     */
    protected $_quoteRepository;

    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $_productFactory;

    /**
     * ShippingCalculate constructor.
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param \Magento\Framework\App\Request\Http $request
     * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     */
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        \Magento\Catalog\Model\ProductFactory $productFactory
    )
    {
        $this->_checkoutSession = $checkoutSession;
        $this->_request = $request;
        $this->_quoteRepository = $quoteRepository;
        $this->_productFactory = $productFactory;
    }

    /**
     * @param \Magento\Checkout\Model\ShippingInformationManagement $shipping
     * @param callable $proceed
     */
    public function aroundSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $shipping,
        callable $proceed,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    )
    {
        $quote = $this->_quoteRepository->getActive($cartId);
        var_dump($quote->debug());
        echo $cartId;

        $returnValue = $proceed($cartId, $addressInformation);
        if ($returnValue) {
            // No Code here
        }
        return $returnValue;
    }
}

1 Answer 1

1

You are doing var_dump and echo in the code. This ends up being send in the response and it is not parsed properly on the frontend because the response is not a valid json. Remove them and all should work.

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.