0

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
    }
}
2
+100

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;
    }
  • it's not update cart items price – Ketan Borada Aug 13 at 8:35
  • it update quote item you have to call API for update item and all done – MageCoder Aug 13 at 12:10
  • dont change parameter/value when you call update cart item API – MageCoder Aug 13 at 12:10
3

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()

  • I just want it in API not every where,ANyway i'm not able to retrive that price sent in API. – Ketan Borada Aug 12 at 6:59
  • Api is just a wrapper for the models that handle the web interface add to cart. – Marius Aug 12 at 7:46
  • is there any way to update cart item price through API,As we can update price from admin while ordering by set custom price – Ketan Borada Aug 12 at 10:16
  • 1
    By default there isn't. For the reason I stated above. If a customer finds this out, he/she can buy everything for free. In the backend is possible because it is assumed that the admin doing the order knows what he/she is doing. – Marius Aug 12 at 10:39

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.