2

I'm building Mobile Magento 2 application with REST API. I'm using the following endpoint to add an item to cart

/V1/carts/mine/items

Then I'm placing an order with

/V1/carts/mine/order

After place order when I add a product to cart first time it has 0 price for an item in the cart.

I've tried to debug on the Magento side and I see that collectTotals for quote produce 0 price, but I'm not sure why. Any ideas here?

0

After debugging I've found out that shippingAddress entity is missed inside the quote and totals calculation isn't correct. So my workaround here is simple invoke

$quote->getShippingAddress();

before adding an item to cart.

Here is my plugin code

// app/code/My/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/config.xsd">
    <type name="Magento\Quote\Model\Quote\Item\Repository">
        <plugin sortOrder="1" name="myModuleRepository" type="My\Module\Plugin\Model\Quote\Item\RepositoryPlugin"/>
    </type>
</config>


// app/code/My/Module/Plugin/Model/Quote/Item.php
<?php

namespace My\Module\Plugin\Model\Quote\Item;

class RepositoryPlugin
{
    /**
     * Quote repository.
     *
     * @var \Magento\Quote\Api\CartRepositoryInterface
     */
    protected $quoteRepository;

    /**
     * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
     */
    public function __construct(
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
    ) {
        $this->quoteRepository = $quoteRepository;
    }

    public function beforeSave(
        \Magento\Quote\Model\Quote\Item\Repository $subject,
        \Magento\Quote\Api\Data\CartItemInterface $cartItem
    )
    {
        $cartId = $cartItem->getQuoteId();
        $quote = $this->quoteRepository->getActive($cartId);
        $quote->getShippingAddress();
    }
}

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.