Skip to content

Instantly share code, notes, and snippets.

@zakirsajib
Created April 28, 2023 18:53
Show Gist options
  • Save zakirsajib/2efa6539d7fee13aad3c70c18110fe1d to your computer and use it in GitHub Desktop.
Save zakirsajib/2efa6539d7fee13aad3c70c18110fe1d to your computer and use it in GitHub Desktop.
Combo RTL
/**
* Applies a combo discount when specific products are in the cart.
*
* This function checks if two or more products with the specified product IDs are in the cart.
* If both products are present, a discount is applied as a negative fee.
*
* @param WC_Cart $cart The WooCommerce cart instance.
*/
function apply_discount_on_combo( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$product_id1 = 360894; // epp
$product_id2 = 360773; // oap
$product_id3 = 3037; // mycotoxin
$product_id4 = 357953; // Glyphosate
$product_id5 = 3171; // Environmental Mycotoxin Panel
$product_id6 = 359895; // Fungal Count Dx
$oap_epp_discount_amount = 60;
$tox_complete_discount_amount = 150;
$tox_complete_plus_discount_amount = 229;
$emma_combo_discount_amount = 119;
// Check if both products are in the cart
$product1_in_cart = false;
$product2_in_cart = false;
$product3_in_cart = false;
$product4_in_cart = false;
$product5_in_cart = false;
$product6_in_cart = false;
foreach ( $cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] == $product_id1 ) {
$product1_in_cart = true;
}
if ( $cart_item['product_id'] == $product_id2 ) {
$product2_in_cart = true;
}
if ( $cart_item['product_id'] == $product_id3 ) {
$product3_in_cart = true;
}
if ( $cart_item['product_id'] == $product_id4 ) {
$product4_in_cart = true;
}
if ( $cart_item['product_id'] == $product_id5 ) {
$product5_in_cart = true;
}
if ( $cart_item['product_id'] == $product_id6 ) {
$product6_in_cart = true;
}
}
// Apply the discount if 4 products are in the cart
if ( $product1_in_cart && $product2_in_cart && $product3_in_cart && $product4_in_cart ) {
$cart->add_fee( 'RTL Tox Complete Plus Panel Discount', -$tox_complete_plus_discount_amount );
}
// Apply the discount if 3 products are in the cart
elseif ( $product1_in_cart && $product2_in_cart && $product3_in_cart ) {
$cart->add_fee( 'RTL Tox Complete Panel Discount', -$tox_complete_discount_amount );
}
// Apply the discount if both products are in the cart
elseif ( $product1_in_cart && $product2_in_cart ) {
$cart->add_fee( 'OAP+EPP Combo Discount', -$oap_epp_discount_amount );
}
// Apply the discount if both products are in the cart
elseif ( $product5_in_cart && $product6_in_cart ) {
$cart->add_fee( 'EMMA Combo Discount', -$emma_combo_discount_amount );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'apply_discount_on_combo', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment