- Posts: 73
- Thank you received: 1
Add flat discount if two products are in cart
12 years 11 months ago #125951
by Ortix
Add flat discount if two products are in cart was created by Ortix
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?
Example psuedo code:
Code:
if($product->id == 1 && $product->id == 2) {
$cart->addDiscount = 50;
}
Which API should I use?
Please Log in or Create an account to join the conversation.
12 years 11 months ago #125971
by Jerome
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.
Replied by Jerome on topic Add flat discount if two products are in cart
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,
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.
12 years 11 months ago - 12 years 11 months ago #126008
by Ortix
Replied by Ortix on topic Add flat discount if two products are in cart
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.
When are these methods triggered? I don't want the customer to enter anything in the coupon field.
Last edit: 12 years 11 months ago by Ortix.
Please Log in or Create an account to join the conversation.
12 years 11 months ago #126037
by nicolas
Replied by nicolas on topic Add flat discount if two products are in cart
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.
12 years 11 months ago #126264
by Ortix
Replied by Ortix on topic Add flat discount if two products are in cart
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?
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.
12 years 11 months ago #126303
by Xavier
Replied by Xavier on topic Add flat discount if two products are in cart
Hi,
To get the products ids, you need something like:
To get the products ids, you need something like:
Code:
$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.
12 years 11 months ago #126683
by Ortix
Replied by Ortix on topic Add flat discount if two products are in cart
excellent! Got everything to work!
Please Log in or Create an account to join the conversation.
Time to create page: 0.199 seconds