0

I have added a field for the cart graphql called cart_updated in the response interface which is a boolean value (true/false) using my custom resolver. If Custom Price for any quote item is set then it will return true else false.

Now the issue is I have used the below code for setting the custom price for Quote Item. When I am hitting the cart query API the first time it is only setting the original custom price in the DB table but not updating the row total, subtotal, etc.

But when I am hitting the cart query the 2nd time after that it is updating the row total, subtotal, etc.

Here is my code -

         // This is the cart object which is coming in the cart query response            
        $cart = $value['model'];
         $cartUpdated = false;
        $cartItems = $cart->getAllVisibleItems();
        /** @var  QuoteItem $cartItem */
        foreach ($cartItems as $cartItem) {
            $cartItemPrice = $cartItem->getOriginalCustomPrice();
            $currentPrice = 2008.98;               
            // Here I am comparing the quote item price in DB and price from my custom table. 
            //If they are not same then only I am updating the price.                
           
            if ($currentPrice && $cartItemPrice != $currentPrice) {
                $cartItem->setOriginalCustomPrice($currentPrice);
                $cartItem->setCustomPrice($currentPrice);
                $cartUpdated = true;
            }
        }
        $this->cart->save();

        return $cartUpdated;

Please help.

2 Answers 2

0

After the line

$cartItem->setCustomPrice($my-price);

add the line

$quoteItem->getProduct()->setIsSuperMode(true);

If that doesn't work, then I would recommend that you check out the answers at this link: Magento 2 Unable to set custom Price in cart item by API

3
  • Thanks for your response. May be i made a mistake while phrasing my question. Updated now. Please check @byalexes Commented Jul 10, 2023 at 3:45
  • @biplabrout before saving the cart, try calling a function on it $this->cart->collectTotals();
    – byalexes
    Commented Jul 11, 2023 at 8:11
  • yes, but the same will not work if we are doing this in a custom resolver. I have shared my ans below Commented Jul 13, 2023 at 5:50
0

Seems the above issue was solved by using the after plugin for the cart query resolver.

For those who want to achieve the same can follow the below steps -

  1. After Plugin for cart query resolver
  2. Get the cart from the resolver result
  3. Get cart items and set custom price
  4. Save the cart using cart repository
  5. Return the result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.