Magento – Zero cart price Issue in M2 ADD TO CART rest API in case of new customers

magento2rest api

I'm working on M2 rest API's. I'm getting zero price issue in the same API.

How to re-create issue:

1. Create new customer through rest API.
2. Login with the registered customer.
3. Now add the product to cart.
4. Now check the cart info through rest API URL.

Now cart showing zero price with zero total.

If you again add a new product or same product again. The correct price will be displayed.

The zero price issue always comes only in case of new customers. 

Request URL with parameters:
storeurl/rest/V1/carts/mine/items
SKU, qty, user_id

Please check screenshots of the response:

enter image description here

enter image description here

Best Answer

I had same issue and fixed it on the Magento side with following plugin

// app/code/Custom/Rest/Plugin/Model/Quote/Item/RepositoryPlugin.php

<?php
/**
 * Created by PhpStorm.
 * User: Dmytro Portenko
 * Date: 10/16/18
 * Time: 10:36 PM
 */
namespace Custom\Rest\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();
    }
}

// app/code/Custom/Rest/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\Quote\Model\Quote\Item\Repository">
        <plugin sortOrder="1" name="customRestRepository" type="Custom\Rest\Plugin\Model\Quote\Item\RepositoryPlugin"/>
    </type>
</config>
Related Topic