0

I have a form, and when I submit the first time it adds a product with a custom price, that works fine, then I go to the checkout without problem, but when i hit the back button on browser and change values on the form, the price will change, then if I go to checkout again, new price won't be shown.

The right total amount will show just after adding billing address information, because it seems like the whole quote-cart component is updated.

This is my code:

Controller

 public function execute(){
        $quote = $this->checkoutSession->getQuote();

        $this->coreSession->start();
        $allItems = $quote->getAllVisibleItems();
        $itemsQty = $quote->getItemsQty();

        $request = $this->getRequest()->getPostValue();

        $product_id = $request['product_id'];
        $price = $request['grand_total'];
        $uniqueid = $request['uniqueid'];

        $this->coreSession->setUniqueid($uniqueid);

            $params = array(
                'formKey' => $this->formKey,
                'product_id' => $product_id,
                'qty' => 0
            );

        $productFactory = $this->product->create();
        try{
                $this->cart->truncate()->save();
                //here I clear the cart so the product is not added more than 1
                $product = $productFactory->load($product_id);
                $this->coreSession->setProductPrice($price);
                $this->cart->addProduct($product,$params);
                $this->cart->save();
                $quote->collectTotals()->save();
         
        }
        catch(\Exception $e){
            return $request;
        }
    }

My submit part on the knockout.js component

                  $.ajax({
                        url: cart_url ,
                        data: {product_id: 1,grand_total:data[0].grand_total, uniqueid: data[0].uniqueId},
                        method: "POST"
                    }).done(function(response){
                        console.log('DONE');
                        console.log(response);
                        var sections = ['cart'];
                        customerData.invalidate(sections);
                        customerData.reload(sections, true);
                         window.location.href = checkout_url;
                    }).fail(function(jqXHR, textStatus){
                        console.log('FAILED');
                        console.log(jqXHR.responseText);
                        console.log(jqXHR);
                    });

When I'm redirected to checkout the new price is not shown, just the old one, until I add billing information and hit update it shows the new price.

What am I doing wrong?

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.