Jump to content

Dynamic Pricing


hershey97

Recommended Posts

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! 

Link to comment
Share on other sites

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?

Link to comment
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...