rafwis84 Posted January 4 Share Posted January 4 Hello! Is there an option to create a cart rule that would look like this: "The customer buys 5 products, gets the cheapest one in the cart for free (100% discount), the second rule buys 10 products and gets the 2 cheapest ones for free (100% discount on the two cheapest ones)." When I make such rules in Presta 8.1.0, then in fact when adding 10 products there is a 2x100% discount BUT it is a discount for the same ONE cheapest product (i.e. the cheapest product of these 10 costs 5$ and the second cheapest in the queue costs 6$) and the system gives a discount of 2x5$. I am looking for a solution to this problem. I added this code to classes/CartRule.php, but the rule works the same: public function getContextualValue($use_tax, Context $context = null, $filter = null, $package = null, $use_cache = true) { if (!CartRule::isFeatureActive()) { return 0; } if (!$context) { $context = Context::getContext(); } if (!$filter) { $filter = CartRule::FILTER_ACTION_ALL; } $all_products = $context->cart->getProducts(); $package_products = (null === $package ? $all_products : $package['products']); $reduction_value = 0; // Discount (%) on the cheapest product if ((float) $this->reduction_percent && $this->reduction_product == -1) { $minPrice = false; $cheapest_product = null; foreach ($all_products as $product) { $price = $product['price']; if ($use_tax) { $price *= (1 + $context->cart->getAverageProductsTaxRate()); } if ($price > 0 && ($minPrice === false || $minPrice > $price) && (($this->reduction_exclude_special && !$product['reduction_applies']) || !$this->reduction_exclude_special)) { $minPrice = $price; $cheapest_product = $product['id_product'] . '-' . $product['id_product_attribute']; } } $discount_applied = false; foreach ($package_products as $product) { if (!$discount_applied && $product['id_product'] . '-' . $product['id_product_attribute'] == $cheapest_product) { $reduction_value += $minPrice * $this->reduction_percent / 100; $discount_applied = true; } } } // Discount (%) on the two cheapest products if ((float) $this->reduction_percent && $this->reduction_product == -2) { $cheapest_products = []; foreach ($all_products as $product) { $price = $product['price']; if ($use_tax) { $price *= (1 + $context->cart->getAverageProductsTaxRate()); } if ($price > 0 && (($this->reduction_exclude_special && !$product['reduction_applies']) || !$this->reduction_exclude_special)) { $cheapest_products[] = ['price' => $price, 'id' => $product['id_product'] . '-' . $product['id_product_attribute']]; } } usort($cheapest_products, fn($a, $b) => $a['price'] <=> $b['price']); $discount_applied = 0; foreach ($cheapest_products as $cheapest_product) { if ($discount_applied < 2) { foreach ($package_products as $product) { if ($product['id_product'] . '-' . $product['id_product_attribute'] == $cheapest_product['id']) { $reduction_value += $cheapest_product['price'] * $this->reduction_percent / 100; $discount_applied++; break; } } } } } return $reduction_value; } Has anyone made such a modification so that a few of the cheapest products in the basket were free? The idea is for discounts to be combined and to reduce the price of different products, e.g. two, and not the same cheapest product times two. The cheapest products that receive a 100% discount must be different, and not the same product discounted multiple times. Prestashop 8.1.0 Link to comment Share on other sites More sharing options...
Mr Rick Posted January 28 Share Posted January 28 Hi, Why not use Gift on cart modules, it has rules and easy to make the modifications. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now