cancel
Showing results for 
Search instead for 
Did you mean: 

Change price of item added to cart and update the total after login

Change price of item added to cart and update the total after login

I have integrated functionality if customer belong to wholesale group then price will be 50%.

Now issue is when customer come to site and add to cart without login product will be added with actual price.  On checkout page when customer will login and if customer is from wholegroup then price should go 50% but as he added product with actual price cart and product  price doesn't update.

 

I want to if wholesale customer logged in then decrease 50% price of the item added to cart.

Theoretically, i know i need to load the quote and update the product price but practically i need help how can i start in M2?

7 REPLIES 7

Re: Chaneg price of item added to cart and update the total after login

Hi @jacktorris,

Please let us know how you did the customization. Is there any event you are using to trigger the observer? may be changing that event properly may help you. Anyway please share more details about your code level implementation.

Re: Chaneg price of item added to cart and update the total after login

I found solution myself to update the price of the item added to cart.

 

Using Object Manager:

 

            $cart = $objectManager->get('\Magento\Checkout\Model\Cart');
           $priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
          $items = $cart->getQuote()->getAllItems();
               if($items){
                foreach ($items as $item){
                   // $this->logger->info(print_r($item->getProductId(),true));
                    // echo $item->getPrice()."<br/>";
                    $subprice = $item->getPrice();

                    $subprice = $priceHelper->currency($subprice, false, false);
                    //$price = (100/121) * $subprice;

                    $finalprice = round($subprice,2);
                    // echo $finalprice."<br/>";
                    $item->setCustomPrice($finalprice);
                    $item->setOriginalCustomPrice($finalprice);
                    $item->getProduct()->setIsSuperMode(true);
                   // echo "FinF";
                }
            }

Re: Chaneg price of item added to cart and update the total after login

Thanks for your answer. It works perfectly. Appricate your help

Re: Chaneg price of item added to cart and update the total after login

@jacktorris 

Thanks for your input.

Though there is one issue with that solution.

After I updated a price for one item, I want to receive the subtotal of the cart.

I do that via:

$quote = $this->checkoutSession->getQuote();
$subtotal = $quote->getSubtotal();

But then the subtotal is not correct.

I also tried to save the quote before using 

$this->checkoutSession->getQuote()->save();

But this doesn't help!

Any suggestions?

Re: Chaneg price of item added to cart and update the total after login

From where I can change this code?

Re: Chaneg price of item added to cart and update the total after login

To fix the subtotal you need to add 

$quote = $cart->getQuote();
    --
    -- 
    $finalprice = round($subprice,2);
    $item->setCustomPrice($finalprice);
    $item->setOriginalCustomPrice($finalprice);
    $item->getProduct()->setIsSuperMode(true);
}
$quote->collectTotals()->save();

Then after it should work for you.

---
Problem Solved Click Accept as Solution!:Magento Community India Forum

Re: Chaneg price of item added to cart and update the total after login

I had absolutely same task - to change item price after login - and this solution helped me. Thank you