Jump to content

[MOD] Quantity discounts for all the products in the same default category.


Recommended Posts

The following modification will allow you to use the quantity discount for all the products in the same default category, regardless of attributes.

The discount will apply to all the products that have the same quantity discount and are in the same default category.

I.E.
Product A, Product B and Product C are all in the same category.
Product A and B have q quantity discount of 10% for 5 items.

The discount will apply for 1 X Product A (blue) + 1 X Product A (red) + 3X Product B

It requires making changes in 2 Prestashop classes.

Prestashop 1.2.*
/classes/PaymentModule.php

Replace

// Quantity discount
$reduc = 0.0;
if ($product['quantity'] > 1 AND ($qtyD = QuantityDiscount::getDiscountFromQuantity($product['id_product'], $product['quantity'])))


With

// Get quantity including attributes for all products in the same default category //
$result = Db::getInstance()->ExecuteS('SELECT a.id_product FROM '._DB_PREFIX_.'discount_quantity a, '._DB_PREFIX_.'discount_quantity b, '._DB_PREFIX_.'product p1, '._DB_PREFIX_.'product p2
WHERE
b.id_product = '.$product['id_product'].' AND
a.quantity = b.quantity AND
a.value = b.value AND
a.id_product = p1.id_product AND
b.id_product = p2.id_product AND
p1.id_category_default = p2.id_category_default');
foreach ($result AS $row)
  $in_product_discount .= isset($in_product_discount)?','.$row['id_product']:$row['id_product'];
// Get total quantity including attributes //
$qty_result = Db::getInstance()->ExecuteS('SELECT SUM(quantity) AS sum FROM `'._DB_PREFIX_.'cart_product` WHERE `id_cart` = '.intval($cart->id > 0?$cart->id:$cookie->id_cart).' AND `id_product` IN ('.$in_product_discount.')');
$real_quantity = $qty_result[0]['sum'];
// Quantity discount
$reduc = 0.0;
if ($product['quantity'] > 1 AND ($qtyD = QuantityDiscount::getDiscountFromQuantity($product['id_product'], $real_quantity)))




Prestashop 1.3.*
/classes/PaymentModule.php

Replace

// Quantity discount
$reduc = 0.0;
if ($product['cart_quantity'] > 1 AND ($qtyD = QuantityDiscount::getDiscountFromQuantity($product['id_product'], $product['cart_quantity'])))


With

// Get quantity including attributes for all products in the same default category //
$result = Db::getInstance()->ExecuteS('SELECT a.id_product FROM '._DB_PREFIX_.'discount_quantity a, '._DB_PREFIX_.'discount_quantity b, '._DB_PREFIX_.'product p1, '._DB_PREFIX_.'product p2
WHERE
b.id_product = '.$product['id_product'].' AND
a.quantity = b.quantity AND
a.value = b.value AND
a.id_product = p1.id_product AND
b.id_product = p2.id_product AND
p1.id_category_default = p2.id_category_default');
foreach ($result AS $row)
  $in_product_discount .= isset($in_product_discount)?','.$row['id_product']:$row['id_product'];
// Get total quantity including attributes //
$qty_result = Db::getInstance()->ExecuteS('SELECT SUM(quantity) AS sum FROM `'._DB_PREFIX_.'cart_product` WHERE `id_cart` = '.intval($cart->id > 0?$cart->id:$cookie->id_cart).' AND `id_product` IN ('.$in_product_discount.')');
$real_quantity = $qty_result[0]['sum'];
// Quantity discount
$reduc = 0.0;
if ($product['cart_quantity'] > 1 AND ($qtyD = QuantityDiscount::getDiscountFromQuantity($product['id_product'], $real_quantity)))




Prestashop 1.2.* AND 1.3.*
/classes/Product.php

Replace

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


With

$result = Db::getInstance()->ExecuteS('SELECT a.id_product FROM '._DB_PREFIX_.'discount_quantity a, '._DB_PREFIX_.'discount_quantity b, '._DB_PREFIX_.'product p1, '._DB_PREFIX_.'product p2
WHERE
b.id_product = '.$id_product.' AND
a.quantity = b.quantity AND
a.value = b.value AND
a.id_product = p1.id_product AND
b.id_product = p2.id_product AND
p1.id_category_default = p2.id_category_default');
foreach ($result AS $row)
 $in_product_discount .= isset($in_product_discount)?','.$row['id_product']:$row['id_product'];
// Get quantity including attributes //
$qty_result = Db::getInstance()->ExecuteS('SELECT SUM(quantity) AS sum FROM `'._DB_PREFIX_.'cart_product` WHERE `id_cart` = '.intval($cart->id > 0?$cart->id:$cookie->id_cart).' AND `id_product` IN ('.$in_product_discount.')');
$real_quantity = $qty_result[0]['sum'];

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



And

       if ($quantity > 1 AND ($qtyD = QuantityDiscount::getDiscountFromQuantity($id_product, $quantity)))


With

       if ($quantity > 0 AND ($qtyD = QuantityDiscount::getDiscountFromQuantity($id_product, $real_quantity)))

Share this post


Link to post
Share on other sites

Hi Tomerg3,

Can you give this a quick once over in case you missed something off it as I have tried this on a PrestaShop v.1.3.2 install and I could not get it to work for me.

Thanks

Share this post


Link to post
Share on other sites

Hi Tomerg

Tried your site and it does work so I must be pasting in the code wrong. Damn!
Would it be possible to let me see your modified Product.php and PaymentModule.php?
Sorry if this sounds a bit forward but I've spent the last hour trying again to get this working on a V1.3.2.

Raffs

Share this post


Link to post
Share on other sites

Tomerg

I would like to say a big Thank you, I have downloaded the two files and it works a treat. you're a genius!

Raffs

PS. on another note my friend in England purchased your Attribute Wizard Pro last week and hasn't stopped talking about it!

Share this post


Link to post
Share on other sites
  • 2 weeks later...

This is awesome!

Is there a way to mod it so that it only affects one category(or sub-category in my case)? I want to use this code for a "special" like "3 for 20" in a specific subcategory.

Thanks in advance!

Share this post


Link to post
Share on other sites

Change

p1.id_category_default = p2.id_category_default');


To

p1.id_category_default = p2.id_category_default AND p1.id_category_default = X');



X should be the ID of the category you want this to apply to.

I haven't tested this, so try it out before using it on a live site.

Share this post


Link to post
Share on other sites
  • 2 weeks later...

I am not sure whether you guys encounter the problem I met after applying this code.

The scenario is like this:
When you added in certain quantities to get the discount, the price will be automatically changed to after-discount price at category page, but the product page will still show original price. It will confuse customers.

Share this post


Link to post
Share on other sites
  • 3 weeks later...
  • 1 month later...

Hi, I'm using PS 1.3.6 and I just tried to "install" this mod.
I tried with the provided files and also by manually replacing the code posted before. Sadly I always get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
SELECT SUM(quantity) AS sum FROM `ps_cart_product` WHERE `id_cart` = 10 AND `id_product` IN ()



Is there any way to fix it? or did I do something wrong? I would really LOVE to be able to use this very nice and useful discount feature

Share this post


Link to post
Share on other sites

Try changing

$qty_result = Db::getInstance()->ExecuteS('SELECT SUM(quantity) AS sum FROM `'._DB_PREFIX_.'cart_product` WHERE `id_cart` = '.intval($cart->id > 0?$cart->id:$cookie->id_cart).' AND `id_product` IN ('.$in_product_discount.')');
$real_quantity = $qty_result[0]['sum'];



To

if ($in_product_discount != "")
{
$qty_result = Db::getInstance()->ExecuteS('SELECT SUM(quantity) AS sum FROM `'._DB_PREFIX_.'cart_product` WHERE `id_cart` = '.intval($cart->id > 0?$cart->id:$cookie->id_cart).' AND `id_product` IN ('.$in_product_discount.')');
$real_quantity = $qty_result[0]['sum'];
}
else
$real_quantity = $quantity;

Share this post


Link to post
Share on other sites
  • 2 weeks later...
Change
p1.id_category_default = p2.id_category_default');


To

p1.id_category_default = p2.id_category_default AND p1.id_category_default = X');



X should be the ID of the category you want this to apply to.

I haven't tested this, so try it out before using it on a live site.



Just wondering can discount be applied to you specific categories only:

For example i have to apply discount for category 5 and 9:

I have tried

p1.id_category_default = p2.id_category_default AND p1.id_category_default = 5 AND p1.id_category_default = 9 ');

&


p1.id_category_default = p2.id_category_default AND p1.id_category_default = 5 OR p1.id_category_default = 9 ');

but no joy. Could somebody help me this issues please.

Thanks in Advance

Share this post


Link to post
Share on other sites
  • 2 weeks later...

Hello,

First of all, thank you very much for this feature. I have tested it on PS 1.3.7 and it works very well.

This is nearly what I was looking for. What I would like to do, is to extend this feature, not to products in a same category, but to the products in every category.

For example, 5 products of cat 1, 5 products of cat 2, and 5 products of cat 3 = total 15 products, i would like to apply a discount on every product.

Is this possible and could you point me in the right direction ?


Many thanks,

Regards

Share this post


Link to post
Share on other sites
  • 1 month later...

Hi

I have tried to apply the quantity discounts to my store and I have a problem with it.
In my BO (Prestashop 1.3.7) appears the following message.

Notice: Undefined variable: in_product_discount in /Applications/MAMP/htdocs/efimeracaenis/classes/Product.php on line 1448


I have checked the line 1448.

$qty_result = Db::getInstance()->ExecuteS('SELECT SUM(quantity) AS sum FROM `'._DB_PREFIX_.'cart_product` WHERE `id_cart` = '.intval($cart->id > 0?$cart->id:$cookie->id_cart).' AND `id_product` IN ('.$in_product_discount.')');



The problem seems to be the variable "$in_product_discount".

How can I fix it? Who can help me, please?

Thanks for all.

Share this post


Link to post
Share on other sites
:(

I have it done, but I have the same problem.
I am attaching the code, thanks.

PaymentModule.php
// Get quantity including attributes for all products in the same default category //
$in_product_discount = "";
$result = Db::getInstance()->ExecuteS('SELECT a.id_product FROM '._DB_PREFIX_.'discount_quantity a, '._DB_PREFIX_.'discount_quantity b, '._DB_PREFIX_.'product p1, '._DB_PREFIX_.'product p2
WHERE
b.id_product = '.$product['id_product'].' AND
a.quantity = b.quantity AND
a.value = b.value AND
a.id_product = p1.id_product AND
b.id_product = p2.id_product AND
p1.id_category_default = p2.id_category_default');
foreach ($result AS $row)
$in_product_discount .= isset($in_product_discount)?','.$row['id_product']:$row['id_product'];
// Get total quantity including attributes //
$qty_result = Db::getInstance()->ExecuteS('SELECT SUM(quantity) AS sum FROM `'._DB_PREFIX_.'cart_product` WHERE `id_cart` = '.intval($cart->id > 0?$cart->id:$cookie->id_cart).' AND `id_product` IN ('.$in_product_discount.')');
$real_quantity = $qty_result[0]['sum'];
// Quantity discount
$reduc = 0.0;
if ($product['cart_quantity'] > 1 AND ($qtyD = QuantityDiscount::getDiscountFromQuantity($product['id_product'], $real_quantity)))
$reduc = QuantityDiscount::getValue($price_wt, $qtyD->id_discount_type, $qtyD->value, new Currency(intval($order->id_currency)));



Product.php

// Get quantity including attributes //
$qty_result = Db::getInstance()->ExecuteS('SELECT SUM(quantity) AS sum FROM `'._DB_PREFIX_.'cart_product` WHERE `id_cart` = '.intval($cart->id > 0?$cart->id:$cookie->id_cart).' AND `id_product` IN ('.$in_product_discount.')');
$real_quantity = $qty_result[0]['sum'];

Share this post


Link to post
Share on other sites

Hi.

I have tried to use the file that you have uploading, product.php, and I have same problem. I have thought that the problem maybe my data base. Maybe some variable is missing?

Thanks.

Share this post


Link to post
Share on other sites

thanks so much tomerg3, you really help me..
beside that, i also apply a discount if a total amount of buying for product in ALL category reached $50 (before the cut off prices), so all the product price in ALL category would be reduced by $0,6..
Do you have any solutions to help me again ? thanks before..

sorry about my english..

Share this post


Link to post
Share on other sites
  • 1 month later...

This looks like what I'm after... maybe.

When i tried it on my local MAMP server it just brought up the white screen of death. But i assume thats because i am using prestashop 1.4. can i make some modifications to bring your creation to life?

Share this post


Link to post
Share on other sites
  • 1 month later...

Hello tomerg3,
We have on our site the words "Our Price:" before the price.
But when I turn on quantity discounts module they dissapear. How can I add this to the module so they show?
Can you show me where please?

Share this post


Link to post
Share on other sites

Yes thanks, ive been looking at that for the last 2 hours!
;-)

function fetchCurrentPrice() {ldelim}
   var val = $("#our_price_display").text().replace(currencySign,'');
   var qd_price = "";
   for (var i = 0 ; i < val.length ; i++)
       if (/[0-9.,]/.test(val.charAt(i)))
           qd_price += val.charAt(i);
   var arr = qd_price.split(",");
   if (arr[arr.length - 1].length == 2)
       qd_price = qd_price.replace(/,/g,'.');
   else
       qd_price = qd_price.replace(/,/g,'');
   return parseFloat(qd_price);
{rdelim}



Is it this area?

Share this post


Link to post
Share on other sites

Sorry tomerg3, I ment the main price display, not the quantitydiscount price display.
Our main price has lost its text.. I assume your module is hiding the price and putting it back in again, then showing the quantitydiscounts below it.

Where would I put the actual text in for the main price?

Share this post


Link to post
Share on other sites
  • 2 weeks later...

Tomerg3 Hello. Thank you for the file modifications to create quantity discounts. What should I change the files so that the quantity discount only applies to a group of clients, for example, by default. Thank you

Share this post


Link to post
Share on other sites
  • 3 weeks later...

Ah!!! This is what I have been looking for! But alas... like the Better Together module... it isn't working with 1.4...

 

I am more optimistic and still have hope on this one! It seems tomerg3 is still actively helping in this thread!

 

Thank you tomerg3! I look forward to your update on this for 1.4!

Share this post


Link to post
Share on other sites
  • 3 weeks later...
  • 2 months later...
  • 9 months later...
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

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