Enhance Coupons functionality

Hi all,

I’m new to VELO and Wix and I’m trying to enhance the functionality of Coupons. As you know, there are several types but none of them meet my requirements.

What I’m trying to achieve is this:

Customer gets a free product “x” when they order for 45 USD/EUR.
Customer gets a free product “y” when they order for 65 USD/EUR.

Both of these products are also available for purchase on the website. Their respective prices are 10 USD/EUR and 25 USD/EUR.

So far, my approach has been:

  1. Use regular discount coupons “FREE X” and “FREE Y” for respective discounts of 10 and 25 USD/EUR.

  2. Create custom code to interfere when the subtotal of the order is insufficient → remove coupon from cart & show message to customer that coupon does not apply.

I have used wixEcom_onCheckoutUpdated i n events.js. This allows me to re-check the value of the order throughout the checkout procedure. I’ve managed to retrieve the total order value and the coupon code so I’m at a point where I can decide if the coupon should be removed or not.

import wixData from ‘wix-data’ ;
import wixEcom from ‘wix-ecom-backend’ ;
import wixStoresBackend from ‘wix-stores-backend’ ;
import { myRemoveCouponFunction } from ‘backend/Cart-override’ ;

export function wixEcom_onCheckoutUpdated ( event ) {
//Get subtotal from order
const checkoutSubtotal = event . entity . priceSummary . subtotal . amount ;
const appliedDiscounts = event . entity . appliedDiscounts ;
//Check for discounts of type COUPON. If applied, verify which coupon code is used.
appliedDiscounts . forEach (( discount ) => {
if ( discount . coupon && discount . coupon . code === “KEY23” ) {
if ( checkoutSubtotal >= 45 ){
//Minimum total is 45 so all good here. Customer is entitled to discount coupon.
console . log ( “Coupon KEY23 was found is valid!” );
} else {
console . log ( “Code 400: Coupon KEY23 was found and NOT valid!” );
//Minimum total of 45 is not reached. Customer is not entitled to discount coupon.
//Coupon should be removed from cart and customer should get a message “This coupon is only valid for orders above 45”
myRemoveCouponFunction ( event );

But this is where I’m stuck. I can’t seem to remove the coupon from the cart and show a message to the customer. Any thoughts on this? Are there other ways to deal with this? Any assistance would be greatly appreciated!