Jump to content

How to change product price while adding to cart


Recommended Posts

Hello everyone,

in prestashop on product detail page I have created a form wizard asking the customer some questions about customizing the product. All answers are stored in variables via javascript. In this js file there is also a new final price of the product calculated as product customization depends on customer input of width and lenght of the product.

So finally I have a form with some hidden input fields which fetch all the values of the custom wishes like custom width, custom height, custom shape, custom price and so on which could be send to cart controller.

    <form {if $PS_CATALOG_MODE AND !isset($groups) AND $product->quantity > 0}class="hidden"{/if} action="{$link->getPageLink('cart')}" method="post">
      <input type="hidden" name="token" value="{$static_token}" />
      <input type="hidden" name="id_product" value="{$product->id|intval}" id="product_page_product_id" />
      <input type="hidden" name="add" value="1" />
      <input type="hidden" name="id_product_attribute" id="idCombination" value="" />
      <input type="hidden" name="customordertext" id="customordertext" value="" />
      <input type="hidden" name="customfinalprice" id="customfinalprice" value="" />
      <label>Menge: </label><input type="text" name="qty" id="quantity_wanted" class="text" value="{if isset($quantityBackup)}{$quantityBackup|intval}{else}{if $product->minimal_quantity > 1}{$product->minimal_quantity}{else}1{/if}{/if}" size="2" maxlength="3" {if $product->minimal_quantity > 1}onkeyup="checkMinimalQuantity({$product->minimal_quantity});"{/if} />
      <input type="submit" value="In den Warenkorb" name="submit" class="exclusive">

But now when I click on "add to cart" of course the standard product is added to the cart with standard price. How can I achieve that custom final price is added to the cart along with my custom input variables like mentioned above??

I just dont know to send my additional data created with javascript to cart.

I thought of creating new rows withing database table of cart or cart_product to save custom price and custom text (just a text string with custom options) when adding product to cart. Same time in cart controller I think I have to override one or more methods to check if recently added product to cart has value for custom price saved in database so the controller can change the price in cart with the custom price. Also I have to show my custom text in the cart.

I just dont know where to start and how to save my custom price and custom text which are beeing generated by the javascript file I have created into the database and after that how to push them into the cart.

Can we somehow create an instance of the product added to cart and change variables like price, attribute text etc. on the fly like it is possible with magento observer model afer adding product to cart??

Could anyone give me some needable ideas in the right direction please...

Thank you very much

Share this post

Link to post
Share on other sites

  • 9 months later...
Sorry for my poor English.

I too am trying to do your very same thing, and I can not find a solution to solve this problem.


The form that you have shown in your last response fails to send the cart a price decided by the customer, but if known well, the module creates a product, the catalog prestashop, with that particular price.

So in the basket are sending a clone of that product, you just created by clicking on add to cart.


I think that this solution can create a lot of confusion in the product catalog.

Share this post

Link to post
Share on other sites

  • 1 year later...
  • 9 months later...

I found a solution using the specificPriceRule, here it is : 

$cart->updateQty(1, $product_id, $product->getDefaultAttribute($product->id));
$specific_price_rule = new SpecificPriceRule();
$specific_price_rule->name = 'Price rule name here';
$specific_price_rule->id_shop = (int)$context->shop->id;
$specific_price_rule->id_currency = 0;
$specific_price_rule->id_country = 0;
$specific_price_rule->id_group = 0;
$specific_price_rule->from_quantity = 1;
$specific_price_rule->price = 2.5;
$specific_price_rule->reduction = 0;
$specific_price_rule->reduction_tax = 0;
$specific_price_rule->reduction_type = 'amount';
$specific_price_rule->from = date("Y-m-d H:i:s");
$specific_price_rule->to = date("Y-m-d").' 23:59:59';
$specific_price = new SpecificPrice();
$specific_price->id_product = (int)$product_id; // choosen product id
$specific_price->id_product_attribute = $product->getDefaultAttribute($product->id);
$specific_price->id_cart = (int)$cart->id;
$specific_price->id_shop = (int)$context->shop->id;
$specific_price->id_currency = 0;
$specific_price->id_country = 0;
$specific_price->id_group = 0;
$specific_price->id_customer = 0;
$specific_price->from_quantity = 1;
$specific_price->price = 2.5;
$specific_price->reduction_type = 'amount';
$specific_price->reduction_tax = 1;
$specific_price->reduction = 0;
$specific_price->from = date("Y-m-d H:i:s");
$specific_price->to = date("Y-m-d").' 23:59:59'; // or set date x days from now
$specific_price->id_specific_price_rule = $specific_price_rule->id;

It's a work in progress, this code can be optimiezd but it's working for what I have to do.

Hope it helps!

  • Like 1

Share this post

Link to post
Share on other sites

  • 3 months later...

You should probably remove the "customfinalprice" from the form. Anyone could manipulate your sales price when the price is submitted client-side (either remove it, or add a serverside calculation that have to match the values (and when doing that, you don't need the first one)

  • Like 1

Share this post

Link to post
Share on other sites

  • 10 months later...

Guys im not so sure about the setspecificprice way, because if you do this then if customer add 2 differents customized priced products with same id then both will be same price in cart ?


Tell me if im wrong but this is what im checking actually.

Share this post

Link to post
Share on other sites

  • 2 years later...
  • 8 months later...
On 7/8/2020 at 3:17 AM, IronMan0803 said:

Did you experience that it will not reflect immediately? You need to refresh and wait for more seconds for the specific price will reflect. Can anyone help me with the issue?

It happens to me too, how did you solve it?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More