Apply Woocommerce coupon after custom discount in the cart or check-in
-
In my child theme functions.php I added a custom code to calculate dynamic discounts based on my client needs. What I am not able to do is, when a fixed price coupon (let’s say fixed price on cart) is applied, it is automatically added BEFORE the custom discounts, while I want it applied AFTER.
Example of desired output:
Subtotal = 1000 10%
discount (custom) = -100
coupon = -50
Total = 850
What is happening instead
Subtotal = 1000
coupon = -50
10% discount = -95
Total = 855
Here’s my custom discount code (it works on simple products that have a custom field “is_student” where the ouput is ‘yes’ / ‘no’)
// Display custom field value in the cart
function display_student_option_in_cart($item_data, $cart_item) {
$product = wc_get_product($cart_item['product_id']);
if (isset($cart_item['is_student'])) {
if ($product->is_on_sale()) {
$item_data[] = array(
'key' => __('Student discount', 'woocommerce'),
'value' => __('Not applicable', 'woocommerce')
);
} else {
$item_data[] = array(
'key' => __('Student discount', 'woocommerce'),
'value' => __('Yes', 'woocommerce')
);
}
}
return $item_data;
}
add_filter('woocommerce_get_item_data', 'display_student_option_in_cart', 10, 2);
//function to calculate discounts for students and adults (only if they book > 1 product)
function cart_discount_subsequent_variations_same_product() {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
global $woocommerce;
$cart = WC()->cart->get_cart();
$cart_array = array();
$total_student_discount = 0; // Initialize total student discount
$total_adult_discount = 0; // Initialize total adult discount
$student_percentage = 0.10;
$adult_percentage = 0.10;
foreach ($cart as $cart_item_key => $values) {
$product_id = $values['product_id'];
$cart_lines_quantity = $values["quantity"];
$cart_lines_total = $values["line_total"];
$is_student = isset($values['is_student']) ? $values['is_student'] : false;
// Retrieve the product object
$product = wc_get_product($product_id);
// Check if product is on sale
$is_on_sale = $product && $product->is_on_sale();
//case 1: is student && is not on sale
if ($is_student && !$is_on_sale) {
$student_discount = $student_percentage * $cart_lines_total;
$total_student_discount += $student_discount; // Accumulate the student discount for each item
}
//case 2: is student && is on sale
/* else if($is_student && $is_on_sale) {
}*/
//case 3: is not student
else if(!$is_student && !$is_on_sale) {
$cart_array[] = array('product_id' => $product_id, 'quantity' => $cart_lines_quantity, 'total' => $cart_lines_total);
}
//case 4: is not student && is on sale
else if(!$is_student && $is_on_sale) {
$cart_array[] = array('product_id' => $product_id, 'quantity' => $cart_lines_quantity, 'total' => $cart_lines_total);
}
}
// Apply student discount
if ($total_student_discount > 0) {
$student_discount_neg = -($total_student_discount);
$discount_text = __('10% student discount', 'woocommerce');
WC()->cart->add_fee($discount_text, $student_discount_neg, false);
}
// Apply adult discount for subsequent cruises
if (count($cart_array) > 1) {
usort($cart_array, function($a, $b) {
return $a['product_id'] <=> $b['product_id'];
});
// Calculate discount for subsequent adult bookings
for ($i = 1; $i < count($cart_array); $i++) {
$total_adult_discount += ($adult_percentage * $cart_array[$i]['total']);
}
if ($total_adult_discount > 0) {
$adult_discount_neg = -($total_adult_discount);
$discount_text = __('10% discount on subsequent week(s)', 'woocommerce');
WC()->cart->add_fee($discount_text, $adult_discount_neg, false);
}
}
}
add_action('woocommerce_cart_calculate_fees', 'cart_discount_subsequent_variations_same_product', 10);
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.