PrestaShop: Quantity Discounts Fix

In PrestaShop 1.3 Quantity Discounts do not work properly – currently they only work correctly for percentage discounts and with a Tax assigned to the product which “correct” the final price so everything looks almost fine. However in fact it doesn’t in certain cases.

Note: We’ve previously posted this fix at prestashop.com: [Fix] Quantity Discounts do not work properly (BUGS 6792 & 6827)

The reason why it does not work is basically due to the design of code located in classes/PaymentModule.php file:

$reduc = QuantityDiscount::getValue($price_wt, $qtyD->id_discount_type, $qtyD->value, new Currency(intval($order->id_currency)));

This code INCORRECTLY computes quantity discount amount for product_quantity_discount field in order_details table, because it computes the discount from price ($price_wt variable) to which was Quantity Discount already applied (also Group Reduction was applied what is wrong as well).

The problem with “Quantity Discount already applied” can easily be solved by getting the price of product by calling Product::getPriceStatic() method with 1 quantity of product (currently it uses variable with price computed for multiple products).
The problem with “Group Reduction already applied” has to be solved by adding a new argument to Product::getPriceStatic() method.

Let’s fix it.

Find:

$reduc = QuantityDiscount::getValue($price_wt, $qtyD->id_discount_type, $qtyD->value, new Currency(intval($order->id_currency)));

Replace with:

$price_wt_no_qtyD = Product::getPriceStatic(intval($product['id_product']), true, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), 6, NULL, false, true, 1, false, NULL, NULL, NULL, false);
$reduc = QuantityDiscount::getValue($price_wt_no_qtyD, $qtyD->id_discount_type, $qtyD->value, ($tax) ? true : false, $tax, $currency);

This fixes the price inserted to product_price field in order_details table which needs to be pure price without any reductions applied:

Find:

'.floatval(Product::getPriceStatic(intval($product['id_product']), false, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), (Product::getTaxCalculationMethod(intval($order->id_customer)) == PS_TAX_EXC ? 2 : 6), NULL, false, false, $product['cart_quantity'], false, intval($order->id_customer), intval($order->id_cart), intval($order->id_address_delivery))).',

Replace with:

'.floatval(Product::getPriceStatic(intval($product['id_product']), false, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), (Product::getTaxCalculationMethod(intval($order->id_customer)) == PS_TAX_EXC ? 2 : 6), NULL, false, false, 1, false, intval($order->id_customer), intval($order->id_cart), intval($order->id_address_delivery))).',

This fixes strange warning (not required):

.(int)QuantityDiscount::getDiscountFromQuantity(intval($product['id_product']), intval($product['cart_quantity'])).',

Replace with:

'.((!QuantityDiscount::getDiscountFromQuantity(intval($product['id_product']), intval($product['cart_quantity']))) ? '0' : '1').',

Now we will add a new argument to Product::getPriceStatic() method in classes/Product.php file:
(to be able to get price without Group Reduction discount applied – this fix does not change the behavior of this method which means any old code will work exactly the same as before).

Find:

public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = NULL, $decimals = 6, $divisor = NULL, $only_reduc = false, $usereduc = true, $quantity = 1, $forceAssociatedTax = false, $id_customer = NULL, $id_cart = NULL, $id_address_delivery = NULL)

Replace with:

public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = NULL, $decimals = 6, $divisor = NULL, $only_reduc = false, $usereduc = true, $quantity = 1, $forceAssociatedTax = false, $id_customer = NULL, $id_cart = NULL, $id_address_delivery = NULL, $group_reduc = true)

Find:

if ($usereduc)
	$price -= Tools::ps_round($price * Group::getReduction(((isset($id_customer) AND $id_customer) ? $id_customer : 0)) / 100, 2);

Replace with:

if ($usereduc && $group_reduc)
	$price -= Tools::ps_round($price * Group::getReduction(((isset($id_customer) AND $id_customer) ? $id_customer : 0)) / 100, 2);

Find:

$cacheId = $id_product.'-'.($usetax?'1':'0').'-'.$id_product_attribute.'-'.$decimals.'-'.$divisor.'-'.($only_reduc?'1':'0').'-'.($usereduc?'1':'0').'-'.$quantity.'-'.($id_customer ? $id_customer : '0');

Replace with:

$cacheId = $id_product.'-'.($usetax?'1':'0').'-'.$id_product_attribute.'-'.$decimals.'-'.$divisor.'-'.($only_reduc?'1':'0').'-'.($usereduc?'1':'0').'-'.($group_reduc?'1':'0').'-'.$quantity.'-'.($id_customer ? $id_customer : '0');

From now on all records are inserted to the database with correct values and the only last two issues needs to be corrected: Order::_deleteProduct() and Order::setProductPrices() methods in classes/Order.php file.

Open classes/Order.php file and replace the entire Order::setProductPrices() method with the following code:

public function setProductPrices(&$row)
{
	if ($this->_taxCalculationMethod == PS_TAX_EXC)
		$row['product_price'] = Tools::ps_round($row['product_price'], 2);
	else
		$row['product_price_wt'] = Tools::ps_round($row['product_price'] * (1 + $row['tax_rate'] / 100), 2);
	if ($row['reduction_percent'])
	{
		if ($this->_taxCalculationMethod == PS_TAX_EXC)
			$row['product_price'] = $row['product_price'] - $row['product_price'] * ($row['reduction_percent'] * 0.01);
		else
			$row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] - $row['product_price_wt'] * ($row['reduction_percent'] * 0.01), 2);
	}
	if ($row['reduction_amount'])
	{
		if ($this->_taxCalculationMethod == PS_TAX_EXC)
			$row['product_price'] = $row['product_price'] - $row['reduction_amount'];
		else
			$row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] - $row['reduction_amount'] * (1 + ($row['tax_rate'] * 0.01)), 2);
	}

	if ($row['group_reduction'])
	{
		if ($this->_taxCalculationMethod == PS_TAX_EXC)
			$row['product_price'] = $row['product_price'] - $row['product_price'] * ($row['group_reduction'] * 0.01);
		else
			$row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] - $row['product_price_wt'] * ($row['group_reduction'] * 0.01), 2);
	}
	if (($row['reduction_percent'] OR $row['reduction_amount'] OR $row['group_reduction']) AND $this->_taxCalculationMethod == PS_TAX_EXC)
		$row['product_price'] = Tools::ps_round($row['product_price'], 2);

	if ($this->_taxCalculationMethod == PS_TAX_EXC)
		$row['product_price_wt'] = Tools::ps_round($row['product_price'] * (1 + ($row['tax_rate'] * 0.01)), 2) + Tools::ps_round($row['ecotax'] * (1 + $row['ecotax_tax_rate'] / 100), 2);
	else
	{
		$row['product_price_wt_but_ecotax'] = $row['product_price_wt'];
		$row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] + $row['ecotax'] * (1 + $row['ecotax_tax_rate'] / 100), 2);
	}
	$row['total_wt'] = $row['product_quantity'] * $row['product_price_wt'];
	$row['total_price'] = $row['product_quantity'] * $row['product_price_wt'];
}

Replace with:

public function setProductPrices(&$row)
{
	if ($this->_taxCalculationMethod == PS_TAX_EXC)
		$row['product_price'] = Tools::ps_round($row['product_price'], 2);
	else
		$row['product_price_wt'] = Tools::ps_round($row['product_price'] * (1 + $row['tax_rate'] / 100), 2);
	if ($row['reduction_percent'])
	{
		if ($this->_taxCalculationMethod == PS_TAX_EXC)
			$row['product_price'] = $row['product_price'] - $row['product_price'] * ($row['reduction_percent'] * 0.01);
		else
			$row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] - $row['product_price_wt'] * ($row['reduction_percent'] * 0.01), 2);
	}
	if ($row['reduction_amount'])
	{
		if ($this->_taxCalculationMethod == PS_TAX_EXC)
			$row['product_price'] = $row['product_price'] - $row['reduction_amount'];
		else
			$row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] - $row['reduction_amount'] * (1 + ($row['tax_rate'] * 0.01)), 2);
	}

	// Added - makes Qty discounts work properly - BEGIN
	if ($row['product_quantity_discount'])
	{
		if ($this->_taxCalculationMethod == PS_TAX_EXC)
			$row['product_price'] = Tools::ps_round($row['product_price'] - $row['product_quantity_discount'] / (1 + ($row['tax_rate'] * 0.01)), 2);
		else
			$row['product_price_wt'] = $row['product_price_wt'] - $row['product_quantity_discount'];
	}
	// Added - makes Qty discounts work properly - END

	if ($row['group_reduction'])
	{
		if ($this->_taxCalculationMethod == PS_TAX_EXC)
			$row['product_price'] = $row['product_price'] - $row['product_price'] * ($row['group_reduction'] * 0.01);
		else
			$row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] - $row['product_price_wt'] * ($row['group_reduction'] * 0.01), 2);
	}
	if (($row['reduction_percent'] OR $row['reduction_amount'] OR $row['group_reduction']) AND $this->_taxCalculationMethod == PS_TAX_EXC)
		$row['product_price'] = Tools::ps_round($row['product_price'], 2);
	// Added - makes Qty discounts work properly - BEGIN
	if($row['product_quantity_discount'] AND $this->_taxCalculationMethod == PS_TAX_INC)
		$row['product_price'] = Tools::ps_round($row['product_price'], 2);
	// Added - makes Qty discounts work properly - END
	if ($this->_taxCalculationMethod == PS_TAX_EXC)
		$row['product_price_wt'] = Tools::ps_round($row['product_price'] * (1 + ($row['tax_rate'] * 0.01)), 2) + Tools::ps_round($row['ecotax'] * (1 + $row['ecotax_tax_rate'] / 100), 2);
	else
	{
		$row['product_price_wt_but_ecotax'] = $row['product_price_wt'];
		$row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] + $row['ecotax'] * (1 + $row['ecotax_tax_rate'] / 100), 2);
	}
	$row['total_wt'] = $row['product_quantity'] * $row['product_price_wt'];
	$row['total_price'] = $row['product_quantity'] * $row['product_price_wt'];
}

Open classes/Order.php file and replace the entire Order::_deleteProduct() method with the following code:

private function _deleteProduct($orderDetail, $quantity)
{
	$row = Db::getInstance()->getRow('
					SELECT *
					FROM `'._DB_PREFIX_.'order_detail` od
					WHERE od.`id_order_detail` = '.intval($orderDetail->id));
	$this->setProductPrices($row);

	/* Update cart */
	$cart = new Cart($this->id_cart);
	$cart->updateQty($quantity, $orderDetail->product_id, $orderDetail->product_attribute_id, false, 'down'); // customization are deleted in deleteCustomization
	$cart->update();
	
	/* Update order */
	$shippingDiff = $this->total_shipping - $cart->getOrderShippingCost();
	$price = $row['product_price_wt'] * $quantity;
	$this->total_products -= $row['product_price'] * $quantity;
	$this->total_products_wt -= $row['product_price_wt'] * $quantity;
	$this->total_shipping = $cart->getOrderShippingCost();
	$this->total_paid -= ($price + $shippingDiff);
	$this->total_paid_real -= ($price + $shippingDiff);
	
	/* Prevent from floating precision issues (total_products has only 2 decimals) */
	if ($this->total_products < 0)
		$this->total_products = 0;
		
	/* Prevent from floating precision issues */
	$this->total_paid = number_format($this->total_paid, 2, '.', '');
	$this->total_paid_real = number_format($this->total_paid_real, 2, '.', '');
	$this->total_products = number_format($this->total_products, 2, '.', '');
	$this->total_products_wt = number_format($this->total_products_wt, 2, '.', '');

	/* Update order detail */
	$orderDetail->product_quantity -= intval($quantity);
	
	if (!$orderDetail->product_quantity)
	{
		if (!$orderDetail->delete())
			return false;
		if (count($this->getProductsDetail()) == 0)
		{
			global $cookie;
			$history = new OrderHistory();
			$history->id_order = intval($this->id);
			$history->changeIdOrderState(_PS_OS_CANCELED_, intval($this->id));
			if (!$history->addWithemail())
				return false;
		}
		return $this->update();
	}
	return $orderDetail->update() AND $this->update();
}

That’s all. Now Quantity Discounts in PrestaShop 1.3 start to work properly 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Cart  
(empty)

Cart Check out  »

Prices are tax inclusive.

The VAT rate for your country (US) * is 0,0 % because it is not a member of the European Union (EU).

* Please create an account if your country does not match.

Community feed
  • [phpBB3] How to disable Magic SEO URL for phpBB?
    Hello ! permanently disable Magic SEO URL for phpBB v3.0 / 3.1 5.0 is it possible ? please Could you explain to me ? Delete directory www/cache-msu/ and all files ? Delete directory...
    by Binano
  • [Zen Cart] How to add .html to urls
    How do I get .html added to the product url? I could not find any mention in the documentation.
    by jekingwws
  • [Zen Cart] .htaccess file will not generate
    I have version 6 installed on a development server for a client. Installation went smoothly, no issues or errors. Files all uploaded in binary. Yet the htaccess file will not generate....
    by jekingwws
  • [phpBB] Magic SEO URLs and phpBB 3.1.6
    Hi, I uploaded the files on my dev server for test before install on the live forum. But in STEP 6 nothing happened - http://dev.flowersnet.info/?inveoActivation . In other steps happened...
    by emil.kozhuharov
  • [Swift Mailer] Swift Mailer lib files missing
    Hi I got the Swift mailer and tried to install it but ended up withnmissing files. I cant find them at github, the provided link is not valid. Please advice. I have a screenshot but it seems it...
    by Vegaanders
Join our support forum » Pre-Sales Questions »
Featured Testimonials

Magic SEO URLs seem to be having a really positive effect on our ranking and look so much cleaner in the browser than a random set of numbers. The sitemaps have also saved me a lot of time!

Simon, the owner of Beltingonline.com

Skeptical at first as I am with all new purchases that can dramatically effect traffic, but this one comes highly recommended. And not just for the product, the support is second to none and all issues are dealt with politely and efficiently. Top Marks!

Phillip, the owner of Sexy Ladies Fancy Dress Costumes

More Testimonials »