'Prestashop Dynamic Pricing PHP
I am trying to make a fully customizable product page and then add the product to the cart. However I'm struggling to be able to make a temporary price for that cart/session for the user.
I've looked at Cart and can't seem to find a solution in there, I've looked at Product and tried setting a new Product and then setting the price, however as soon as its added to the cart it goes back to the default price. I've read about SpecificPrice but can't really find a use case of it anywhere and can't seem to get it to work.
The only thing I can think of now is to make a new product each time for the customizable products and then deleting it after its been bought or after X amount of time, however I just don't like the idea of this as I think it wouldn't scale well and it could cause a lot of clutter in the admin panel.
I would love any help you could give me about this, if you could point me in the right direction that would be great! Thanks!
Edit:
After doing some more research I've found that I can do it by Price Rules and Specific price. I've used the following code.
$product_id = 1;
$product = new Product($product_id);
$cart = $this->context->cart;
$cart->updateQty(1, $product_id, $product->getDefaultAttribute($product->id));
$specific_price_rule = new SpecificPriceRule();
$specific_price_rule->name = time();
$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 = 1;
$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_rule->add();
$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 = 1;
$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;
$specific_price->add();
However, when doing this it adds a specific price to the table for that. So I'm essentially going to be making a new price for every customer I get. I guess I could set it up so it clears after X amount of time or after order however this doesn't seem right...
Also the second issue I'm having with this method is that its slow. When I add the product to the cart if I go straight to the cart it shows the initial price, then when I refresh again it goes to the specific price...
All I really need to do is to be able to change the price when adding to the cart and then validate on order and leave it at that. Any advice?
Solution 1:[1]
In my opinion, you doing well with SpecificPrice
and it may be the best and the most reliable approach with no core modification. But I cannot figure out why do you use both SpecificPriceRule
and SpecificPrice
classes. I think that using SpecificPrice
would be enough to achieve the goal. And to display SpecificPrice
in cart try to create some ajax call to create your new price before a product will be added to cart. Customized products work this way so you can check it
Solution 2:[2]
For anybody encountering a similar issue, check this guide https://www.prestashop.com/forums/topic/549666-guide-the-new-customization-system-in-prestashop-17/
The customization system in prestashop 1.7 supports adding a price and a weight impact just like combinations. So you can add a customization to cart and give it a price to affect the cart total.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alexander Grosul |
Solution 2 | unloco |