WC_AJAX::apply_coupon()public staticWC 1.0

AJAX apply coupon on checkout page.

Method of the class: WC_AJAX{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = WC_AJAX::apply_coupon();

WC_AJAX::apply_coupon() code WC 8.9.0

public static function apply_coupon() {

	check_ajax_referer( 'apply-coupon', 'security' );

	$coupon_code   = ArrayUtil::get_value_or_default( $_POST, 'coupon_code' );
	$billing_email = ArrayUtil::get_value_or_default( $_POST, 'billing_email' );

	if ( is_string( $billing_email ) && is_email( $billing_email ) ) {
		wc()->customer->set_billing_email( $billing_email );
	}

	if ( ! StringUtil::is_null_or_whitespace( $coupon_code ) ) {
		WC()->cart->add_discount( wc_format_coupon_code( wp_unslash( $coupon_code ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
	} else {
		wc_add_notice( WC_Coupon::get_generic_coupon_error( WC_Coupon::E_WC_COUPON_PLEASE_ENTER ), 'error' );
	}

	wc_print_notices();
	wp_die();
}