How to add a coupon only for main price when using volume prices?

  • Posts: 230
  • Thank you received: 27
  • Hikashop Business
3 weeks 6 days ago #363704

-- HikaShop version -- : 5.1.1
-- Joomla version -- : 5.1.4
-- PHP version -- : 8.2

Hello guys,
Is there any way to applay this restriction now?
So I have "Product 1" price 20$
If quantity is 5 price is 16$
If quantity is 10 price is 14$
And now, I want to create a discount or coupon that applay only for the main price.
The discount / coupon has a -2$ flat.
If I create a discount, not all volume prices should be cut
20$ 18$
16$ 14$ not like this
14 12$ not like this
===========================
20$ 18$
16$ ok
14$ ok
===========================
Is not ok to create the discount or the coupon with the limit on max qty 4, because that limitation it is applied to the entire cart, so if I have 5 different products, my discount is not gonna be applied anymore.
Also, is no ok to create a discount for each product.

So you think that this can be made as a plugin, or a new feature in hikashop?
If you think that it can be done as a plugin, can you show me the starting point? :D

Please Log in or Create an account to join the conversation.

  • Posts: 82683
  • Thank you received: 13335
  • MODERATOR
3 weeks 5 days ago #363705

Hi,

There is no option for that, but it can definitely be done as a plugin.

First, we have documentation about creating plugins for Joomla / HikaShop in general here:
www.hikashop.com/support/documentation/6...mentation.html#intro
If you're not familiar with this, I would recommend looking at this first.

I think the best is to implement the event onAfterCartProductsLoad of the cart API:
www.hikashop.com/support/documentation/6...fterCartProductsLoad
This event is triggered each time a cart is being loaded. You'll have most of the information about the cart in the one and only parameter of the event $cart, including an array of the product objects in $cart->products
So you can loop on that array in order to check which products are in the cart and the quantity of the product in the cart ( in the attribute cart_product_quantity ).
That way, you can calculate how much you want to give as a discount with whatever rules you need to apply.
Once you know the amount, you can add it to the cart:

$discountAmount = 10; // suppose we want to add a discount of 10$
$additionalDiscount = new stdClass();
$additionalDiscount->name = 'The label of the discount';
$additionalDiscount->price_currency_id = 2; // 2 is the id of the currency for USD in System>Currencies
$additional->price_value = - $discountAmount; // since the amount is added to the cart, you want to make it negative so that it can reduce the total amount, and not increase it
$additional->price_value_with_tax = - $discountAmount; // here you can add the amount with taxes. I just give the same amount for both discount amounts as this is just a simple example, but you'll likely want to calculate the taxes to be added or removed from your discount amount so that it matches with the taxes to apply to the cart
$cart->additional['my_custom_discount'] = $additionalDiscount; // here the "fee" is added to the cart.
This mechanism of additional fees is used by several plugins, like the "multi coupon plugin", the "cart fee plugin" or the "global cart fee plugin" (this one is free, you can easily look at its code if you want) which are available in our marketplace.

Please Log in or Create an account to join the conversation.

Time to create page: 0.056 seconds
Powered by Kunena Forum