Add flat discount if two products are in cart

  • Posts: 73
  • Thank you received: 1
11 years 1 month ago #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:

if($product->id == 1 && $product->id == 2) {
$cart->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 or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
11 years 1 month ago #125971

Hi,

There are several triggers in the coupon/discount system.
- onBeforeCouponLoad( & $coupon, & $do)
- onBeforeCouponCheck( &$coupon,&$total,&$zones,&$products,&$display_error, &$error_message, & $do)
- onAfterCouponCheck(&$coupon, &$total, &$zones, &$products, &$display_error, &$error_message, &$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 & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

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

  • Posts: 73
  • Thank you received: 1
11 years 1 month ago #126008

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: 11 years 1 month ago by Ortix.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
11 years 1 month ago #126037

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

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

  • Posts: 73
  • Thank you received: 1
11 years 1 month ago #126264

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:



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 or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
11 years 1 month ago #126303

Hi,

To get the products ids, you need something like:

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

The following user(s) said Thank You: Ortix

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

  • Posts: 73
  • Thank you received: 1
11 years 1 month ago #126683

excellent! Got everything to work!

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

Time to create page: 0.078 seconds
Powered by Kunena Forum