Question

I'm trying to add an item by API.every thing are working except item price
It's Magento Bug that not take price from API

https://devdocs.magento.com/redoc/2.3/admin-rest-api.html#operation/quoteCartItemRepositoryV1SavePost

POST : V1/carts/mine/items?cart_id=27333

Payload :

{
"cartItem": 
    {
        "sku": "1024P270PDTS",
        "qty": 1,
        "price": 20,
        "quote_id": "27333"
    }
}

It will add item correctly but not the price it will not take a price from API it set price from magento.
How can I add product in cart with price 20 as passed in API

Even facing same problem with update item API it is not change API price which is sent in payload PUT : V1/carts/mine/items/105287?cart_id=27333

{
"cartItem": 
    {
        "sku": "1024P270PDTS",
        "qty": 1,
        "price": 20,
        "quote_id": "27333",
        "item_id" : 105287
    }
}
Was it helpful?

Solution

I have same problem and i have solved it by Custom API
first create Qoute and update item price by item id saperately.

Add code in webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/carts/updateCartItem/:cartId/:itemId/:price" method="PUT">
        <service class="Vendor\Module\Api\CartManagementInterface" method="updateCartItem"/>
            <resources>
                <resource ref="Magento_Cart::manage"/>
            </resources>
    </route>
</routes>

add logic in file

public function updateCartItem($cartId,$itemId,$price) {

        if($price){
            //$quote = $this->quoteRepository->getActive($cartId);
            $quote = $this->quoteFactory->create()->load($cartId);
            $quoteItem = $quote->getItemById($itemId);
            $quoteItem->setCustomPrice($price);
            $quoteItem->setName($price);
            $quoteItem->setOriginalCustomPrice($price);
            $quoteItem->getProduct()->setIsSuperMode(true);
            $quoteItem->save();
            $quote->save();
        }
        return true;
    }

OTHER TIPS

I think this is not a bug, it's how it should be.
So you, as a customer, want to add stuff to the cart but set your own price on the item? Something does not add up.

If this is allowed, what would stop every customer from adding to the cart products and setting the price to 1 cent? Or even zero?

Anyway, if you really want this (I strongly advice against it), you can intervene somewhere int eh flow of Magento\Quote\Model\Quote::addProduct()

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top