Cart and Checkout Filters – Coupons
The following Coupon filters are available:
-
coupons
-
showApplyCouponNotice
-
showRemoveCouponNotice
coupons
Description
The current functionality is to display the coupon codes in the Cart and Checkout sidebars. This could be undesirable if you dynamically generate a coupon code that is not user-friendly. It may, therefore, be desirable to change the way this code is displayed. To achieve this, the filter coupons
exists. This filter could also be used to show or hide coupons. This filter must not be used to alter the value/totals of a coupon. This will not carry through to the Cart totals.
Parameters
-
coupons
object
– The coupons object with the following keys:-
code
string
– The coupon code. -
discount_type
string
– The type of discount. Can bepercent
orfixed_cart
. -
totals
object
– The totals object with the following keys:-
currency_code
string
– The currency code. -
currency_decimal_separator
string
– The currency decimal separator. -
currency_minor_unit
number
– The currency minor unit. -
currency_prefix
string
– The currency prefix. -
currency_suffix
string
– The currency suffix. -
currency_symbol
string
– The currency symbol. -
currency_thousand_separator
string
– The currency thousand separator. -
total_discount
string
– The total discount. -
total_discount_tax
string
– The total discount tax.
-
currency_code
-
code
-
extensions
object
(default:{}
) – The extensions object. -
args
object
– The arguments object with the following key:-
context
string
(default:summary
) – The context of the item.
-
context
Returns
-
array
– The coupons array of objects with the same keys as above.
Code example
const { registerCheckoutFilters } = window.wc.blocksCheckout;
const modifyCoupons = ( coupons, extensions, args ) => {
return coupons.map( ( coupon ) => {
if ( ! coupon.label.match( /autocoupon(?:_\d+)+/ ) ) {
return coupon;
}
return {
...coupon,
label: 'Automatic coupon',
};
} );
};
registerCheckoutFilters( 'example-extension', {
coupons: modifyCoupons,
} );
Filters can be also combined. See Combined filters for an example.
Screenshots
Before | After |
---|---|
showApplyCouponNotice
Description
Parameters
-
value
boolean
(default:true
) – Weather to show the apply coupon notice. -
extensions
object
(default:{}
) – The extensions object. -
args
object
– The arguments object with the following keys:-
context
string
(allowed values:wc/cart
andwc/checkout
) – The context of the coupon notice. -
code
string
– The coupon code.
-
context
Returns
-
boolean
– Weather to show the apply coupon notice.
Code examples
Basic example
const { registerCheckoutFilters } = window.wc.blocksCheckout;
const modifyShowApplyCouponNotice = ( defaultValue, extensions, args ) => {
return false;
};
registerCheckoutFilters( 'example-extension', {
showApplyCouponNotice: modifyShowApplyCouponNotice,
} );
Advanced example
const { registerCheckoutFilters } = window.wc.blocksCheckout;
const modifyShowApplyCouponNotice = ( defaultValue, extensions, args ) => {
if ( args?.couponCode === '10off' ) {
return false;
}
return defaultValue;
};
registerCheckoutFilters( 'example-extension', {
showApplyCouponNotice: modifyShowApplyCouponNotice,
} );
Filters can be also combined. See Combined filters for an example.
Screenshots
Before | After |
---|---|
showRemoveCouponNotice
Description
Parameters
-
value
boolean
(default:true
) – Weather to show the remove coupon notice. -
extensions
object
(default:{}
) – The extensions object. -
args
object
– The arguments object with the following keys:-
context
string
(allowed values:wc/cart
andwc/checkout
) – The context of the coupon notice. -
code
string
– The coupon code.
-
context
Returns
-
boolean
– Weather to show the apply coupon notice.
Code examples
Basic example
const { registerCheckoutFilters } = window.wc.blocksCheckout;
const modifyShowRemoveCouponNotice = ( defaultValue, extensions, args ) => {
return false;
};
registerCheckoutFilters( 'example-extension', {
showRemoveCouponNotice: modifyShowRemoveCouponNotice,
} );
Advanced example
const { registerCheckoutFilters } = window.wc.blocksCheckout;
const modifyShowRemoveCouponNotice = ( defaultValue, extensions, args ) => {
if ( args?.couponCode === '10off' ) {
return false;
}
return defaultValue;
};
registerCheckoutFilters( 'example-extension', {
showRemoveCouponNotice: modifyShowRemoveCouponNotice,
} );
Filters can be also combined. See Combined filters for an example.
Screenshots
Before | After |
---|---|
Last updated: December 05, 2024