xxxxxxxxxx
WC()->cart->apply_coupon( $coupon_code );
xxxxxxxxxx
function wc_ninja_apply_coupon( $coupon_code ) {
if ( 'test_code' === $coupon_code ) {
$product_id = 99;
WC()->cart->add_to_cart( $product_id );
}
}
add_action( 'woocommerce_applied_coupon', 'wc_ninja_apply_coupon' );
xxxxxxxxxx
/**
* WebAppick – Apply Discount Coupon automatically to the cart
*/
function ts_apply_discount_to_cart() {
$order_total = WC()->cart->get_subtotal();
if( $order_total > 100 ) {
$coupon_code = '10OFF';
if ( !WC()->cart->apply_coupon( sanitize_text_field( $coupon_code ) ) ) {
wc_print_notices();
}
}
}
add_action( 'woocommerce_before_cart_table', 'ts_apply_discount_to_cart' );