Reputation: 25
I have been trying to make a Coupon that makes a discount based on the cart weight when a coupon is used.
I have achieved something using a fixed amount coupon (with fixed value zero) and adding fees :
$cart_weight = $cart->get_cart_contents_weight() ;
and
$cart->add_fee( 'Discount ' . $coupon_code, $cost* -1 , false );
Even it works ,I dont like this, I would like to have it as a regular coupon. Is there any way? Thanks
Upvotes: 0
Views: 147
Reputation: 683
Sure, it's possible, you only have to create a unique coupon and apply it to the cart instead of adding a negative fee
//ADD coupon based of cart weight
add_action( 'woocommerce_before_calculate_totals', 'generate_coupon_based_on_total_weight' );
function generate_coupon_based_on_total_weight( $cart ) {
// Set the threshold weight for applying the coupon
$threshold_weight = 10;
$discount_amount = 2;
$coupon_prefix = 'weight_';
$total_weight = $cart->get_cart_contents_weight() ;
// Check if the total weight is greater than or equal to the threshold weight
if ( $total_weight >= $threshold_weight ) {
// Check if the coupon code with the "weight_" prefix has already been applied
if ( has_coupon_code_with_prefix( $coupon_prefix ) ) {
// Coupon code already applied, do nothing
return;
}
// Generate a unique coupon code
$coupon_code = uniqid( $coupon_prefix );
// Create a new coupon object
$coupon = new WC_Coupon();
$coupon->set_code( $coupon_code );
$coupon->set_amount( $discount_amount ); // Set the coupon amount
$coupon->set_individual_use( true ); // Set the coupon to be used only once
$coupon->set_usage_limit( 1 ); // Set the usage limit to 1
$coupon->save(); // Save the coupon
// Apply the coupon to the cart
$cart->add_discount( $coupon_code );
} else {
// Total weight is less than the threshold, remove the coupon if it exists
remove_coupon_from_cart( $coupon_prefix );
}
}
function has_coupon_code_with_prefix( $prefix ) {
// Get the applied coupons
$applied_coupons = WC()->cart->get_applied_coupons();
// Check if there are any applied coupons
if ( ! empty( $applied_coupons ) ) {
// Iterate through the applied coupons
foreach ( $applied_coupons as $coupon_code ) {
// Check if the coupon code starts with the specified prefix
if ( strpos( $coupon_code, $prefix ) === 0 ) {
// There is a coupon code applied that starts with the specified prefix
return true;
}
}
}
// No coupon code was found that starts with the specified prefix
return false;
}
function remove_coupon_from_cart($prefix) {
global $woocommerce;
// Get the cart object
$cart = $woocommerce->cart;
// Get the coupon codes applied to the cart
$coupon_codes = $cart->get_coupons();
// Loop through the coupon codes and remove any that start with the specified prefix
foreach ($coupon_codes as $code => $coupon) {
if (strpos($code, $prefix) === 0) {
$cart->remove_coupon($code);
// Save the updated cart
$cart->calculate_totals();
}
}
}
The main idea of this function is to apply a discount coupon to the user's cart if the total weight of the products in the cart meets a certain threshold, and to remove the coupon if the total weight falls below the threshold.
The function is triggered by the woocommerce_before_calculate_totals
action, which occurs before the total cost of the products in the cart is calculated.
The function first sets a few variables:
$threshold_weight
: This is the weight that the total weight of the products in the cart must meet or exceed in order for the coupon to be applied.$discount_amount
: This is the amount of the discount that will be applied if the coupon is applied.$coupon_prefix
: This is a prefix that will be added to the coupon code to make it unique.The function then calculates the total weight of the products in the cart using the get_cart_contents_weight
method.
Next, the function checks if the total weight
is greater than or equal to the threshold weight. If it is, the function generates a unique coupon code using the uniqid
function and the $coupon_prefix
variable, and creates a new coupon object using the WC_Coupon
class. The coupon object is then configured with the discount amount, individual use, and usage limit. Finally, the coupon is saved and applied to the cart using the add_discount
method.
If the total weight is less than the threshold weight, the function removes the coupon from the cart if it exists by calling the remove_coupon_from_cart
function and passing in the $coupon_prefix
variable.
Overall, this function allows you to apply a discount coupon to the user's cart if the total weight of the products in the cart meets a certain threshold, and to remove the coupon if the total weight falls below the threshold.
Upvotes: 1