[Skip to main content](#vessel-content)

#   Add flat discount if two products are in cart

##

- **[Ortix](/forum/user/12608-ortix "View Ortix's Profile")**
- [![Ortix's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/12608-ortix "View Ortix's Profile") Topic Author
- [ Offline](/forum/user/12608-ortix)

Less More

- Posts: 73
- Thank you received: 1
-
-

13 years 1 month ago [\#125951](#125951) by [Ortix](/forum/user/12608-ortix "View Ortix's Profile")  *[Add flat discount if two products are in cart](/forum/31-development/862480-add-flat-discount-if-two-products-are-in-cart#125951 "How would you guys recommend to create a plugin (or hack if really necessary) which adds a flat discount if two products exists in a cart?  Example psuedo code:  So both products would total at 200 EUR but since it's these 2 products the discount is 50 EUR so grand total it's 150 EUR.  Which API should I use?")* was created by *[Ortix](/forum/user/12608-ortix "View Ortix's Profile")*

How would you guys recommend to create a plugin (or hack if really necessary) which adds a flat discount if two products exists in a cart?

Example psuedo code: Code:

if($product-&gt;id == 1 &amp;&amp; $product-&gt;id == 2) { $cart-&gt;addDiscount = 50; }

So both products would total at 200 EUR but since it's these 2 products the discount is 50 EUR so grand total it's 150 EUR.

Which API should I use?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg2MjQ4MC1hZGQtZmxhdC1kaXNjb3VudC1pZi10d28tcHJvZHVjdHMtYXJlLWluLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Jerome](/forum/user/80-jerome "View Jerome's Profile")**
- [![Jerome's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar80.png)](/forum/user/80-jerome "View Jerome's Profile")
- [ Offline](/forum/user/80-jerome)
- HikaShop core developer

Less More

- Posts: 26400
- Thank you received: 4079
-
- [](http://obsi.dev)
-

13 years 1 month ago [\#125971](#125971) by [Jerome](/forum/user/80-jerome "View Jerome's Profile")  Replied by *[Jerome](/forum/user/80-jerome "View Jerome's Profile")* on topic *[Add flat discount if two products are in cart](/forum/31-development/862480-add-flat-discount-if-two-products-are-in-cart#125971 "How would you guys recommend to create a plugin (or hack if really necessary) which adds a flat discount if two products exists in a cart?  Example psuedo code:  So both products would total at 200 EUR but since it's these 2 products the discount is 50 EUR so grand total it's 150 EUR.  Which API should I use?")*

Hi,

There are several triggers in the coupon/discount system.
- onBeforeCouponLoad( &amp; $coupon, &amp; $do)
- onBeforeCouponCheck( &amp;$coupon,&amp;$total,&amp;$zones,&amp;$products,&amp;$display_error, &amp;$error_message, &amp; $do)
- onAfterCouponCheck(&amp;$coupon, &amp;$total, &amp;$zones, &amp;$products, &amp;$display_error, &amp;$error_message, &amp;$do)

In your case, I will recommend the onBeforeCouponCheck or the onAfterCouponCheck.
You have the access to the products of the cart so you can check the ids, check if there are your two products and access the discount (by not setting the variable $do to false).

The value and the basic rules would be already implemented in the coupon, but your plugin will add the product check rules.

Regards,

---

Jerome - Obsidev.com
HikaMarket &amp; HikaSerial developer / HikaShop core dev team.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg2MjQ4MC1hZGQtZmxhdC1kaXNjb3VudC1pZi10d28tcHJvZHVjdHMtYXJlLWluLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Ortix](/forum/user/12608-ortix "View Ortix's Profile")**
- [![Ortix's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/12608-ortix "View Ortix's Profile") Topic Author
- [ Offline](/forum/user/12608-ortix)

Less More

- Posts: 73
- Thank you received: 1
-
-

13 years 1 month ago -  13 years 1 month ago [\#126008](#126008) by [Ortix](/forum/user/12608-ortix "View Ortix's Profile")  Replied by *[Ortix](/forum/user/12608-ortix "View Ortix's Profile")* on topic *[Add flat discount if two products are in cart](/forum/31-development/862480-add-flat-discount-if-two-products-are-in-cart#126008 "How would you guys recommend to create a plugin (or hack if really necessary) which adds a flat discount if two products exists in a cart?  Example psuedo code:  So both products would total at 200 EUR but since it's these 2 products the discount is 50 EUR so grand total it's 150 EUR.  Which API should I use?")*

So the coupon should be created beforehand in the discount manager and if the two products match in the plugin I just load in the coupon right?

When are these methods triggered? I don't want the customer to enter anything in the coupon field.

Last edit: 13 years 1 month ago by [Ortix](/forum/user/12608-ortix "View Ortix's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg2MjQ4MC1hZGQtZmxhdC1kaXNjb3VudC1pZi10d28tcHJvZHVjdHMtYXJlLWluLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Away](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

13 years 1 month ago [\#126037](#126037) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Add flat discount if two products are in cart](/forum/31-development/862480-add-flat-discount-if-two-products-are-in-cart#126037 "How would you guys recommend to create a plugin (or hack if really necessary) which adds a flat discount if two products exists in a cart?  Example psuedo code:  So both products would total at 200 EUR but since it's these 2 products the discount is 50 EUR so grand total it's 150 EUR.  Which API should I use?")*

If the "auto load" option of the coupon is turned on (commercial editions of HikaShop), the system will try to load the coupon automatically if it meets the criterias (so if you don't set $do to false in the triggers of the plugin).

The following user(s) said Thank You: [Ortix](/forum/user/12608-ortix "View Ortix's Profile")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg2MjQ4MC1hZGQtZmxhdC1kaXNjb3VudC1pZi10d28tcHJvZHVjdHMtYXJlLWluLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Ortix](/forum/user/12608-ortix "View Ortix's Profile")**
- [![Ortix's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/12608-ortix "View Ortix's Profile") Topic Author
- [ Offline](/forum/user/12608-ortix)

Less More

- Posts: 73
- Thank you received: 1
-
-

13 years 1 month ago [\#126264](#126264) by [Ortix](/forum/user/12608-ortix "View Ortix's Profile")  Replied by *[Ortix](/forum/user/12608-ortix "View Ortix's Profile")* on topic *[Add flat discount if two products are in cart](/forum/31-development/862480-add-flat-discount-if-two-products-are-in-cart#126264 "How would you guys recommend to create a plugin (or hack if really necessary) which adds a flat discount if two products exists in a cart?  Example psuedo code:  So both products would total at 200 EUR but since it's these 2 products the discount is 50 EUR so grand total it's 150 EUR.  Which API should I use?")*

Ah i see, i was using discounts instead of coupons.

However I still have a little issue. I don't understand what all these properties are inside the $products object:
[ ![&#91;img](http://i.imgur.com/eyU5Cjl.png) ](http://i.imgur.com/eyU5Cjl.png "&#91;img")

Why are there 4 properties per product? 2 properties contain data to the image file and 2 properties contain the product data.. and that twice!

I just want to get the product id's of all the products.. How would i go about doing that?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg2MjQ4MC1hZGQtZmxhdC1kaXNjb3VudC1pZi10d28tcHJvZHVjdHMtYXJlLWluLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Xavier](/forum/user/4105-xavier "View Xavier's Profile")**
- [![Xavier's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4105.png)](/forum/user/4105-xavier "View Xavier's Profile")
- [ Offline](/forum/user/4105-xavier)

Less More

- Posts: 13201
- Thank you received: 2322
-
-
-
- [](http://www.kidiweb.fr)
-

13 years 1 month ago [\#126303](#126303) by [Xavier](/forum/user/4105-xavier "View Xavier's Profile")  Replied by *[Xavier](/forum/user/4105-xavier "View Xavier's Profile")* on topic *[Add flat discount if two products are in cart](/forum/31-development/862480-add-flat-discount-if-two-products-are-in-cart#126303 "How would you guys recommend to create a plugin (or hack if really necessary) which adds a flat discount if two products exists in a cart?  Example psuedo code:  So both products would total at 200 EUR but since it's these 2 products the discount is 50 EUR so grand total it's 150 EUR.  Which API should I use?")*

Hi,

To get the products ids, you need something like: Code:

$prods = array[]; foreach($products as $product){ $prods[] = $product-&gt;product_id; } if(in_array(array('1','2'),$prods){ $do = true; }else{ $do = false; }

The following user(s) said Thank You: [Ortix](/forum/user/12608-ortix "View Ortix's Profile")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg2MjQ4MC1hZGQtZmxhdC1kaXNjb3VudC1pZi10d28tcHJvZHVjdHMtYXJlLWluLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Ortix](/forum/user/12608-ortix "View Ortix's Profile")**
- [![Ortix's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/12608-ortix "View Ortix's Profile") Topic Author
- [ Offline](/forum/user/12608-ortix)

Less More

- Posts: 73
- Thank you received: 1
-
-

13 years 1 month ago [\#126683](#126683) by [Ortix](/forum/user/12608-ortix "View Ortix's Profile")  Replied by *[Ortix](/forum/user/12608-ortix "View Ortix's Profile")* on topic *[Add flat discount if two products are in cart](/forum/31-development/862480-add-flat-discount-if-two-products-are-in-cart#126683 "How would you guys recommend to create a plugin (or hack if really necessary) which adds a flat discount if two products exists in a cart?  Example psuedo code:  So both products would total at 200 EUR but since it's these 2 products the discount is 50 EUR so grand total it's 150 EUR.  Which API should I use?")*

excellent! Got everything to work!

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg2MjQ4MC1hZGQtZmxhdC1kaXNjb3VudC1pZi10d28tcHJvZHVjdHMtYXJlLWluLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop Install &amp; Update Product &amp; Category Display Content Creation Sales and Orders Orders management Reports &amp; Statistics Customers Checkout Payment Methods Shipping Methods Import &amp; Export (CSV, XLS…) Specific Features Affilation Taxes Wishlist Vote Carousel &amp; Slider Mass Actions Filters &amp; Search Discount &amp; Coupon Feature Request Integrations Showcase General Talk about HikaShop Development HikaShop 3.0 Beta Translation Commercial Jobs HikaShop Components HikaMarket Market: Feature requests Market: How to? Market: Bug Report HikaSerial General talk about HikaSerial Serial: Bug Report Serial: How to? Support en Français de HikaSerial (French Support) HikaSubscription HikaAuction Auction: Feature requests Auction: How to? Auction: Bug report Hikashop around the world Support en Français (French Support) Russian FAQ. Курс выживания с Hikashop. Частые вопросы и о Расширения Hikashop Наши сайты на Hikashop 日本語版サポート(Japanese Support) Thai German Archives Bug Report How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.079 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)
