WC_Coupon::get_coupon_errorpublicWC 1.0

Map one of the WC_Coupon error codes to a message string.

Method of the class: WC_Coupon{}

Hooks from the method

Returns

String. Message/error string

Usage

$WC_Coupon = new WC_Coupon();
$WC_Coupon->get_coupon_error( $err_code );
$err_code(int) (required)
Message/error code.

WC_Coupon::get_coupon_error() code WC 10.5.0

public function get_coupon_error( $err_code ) {
	switch ( $err_code ) {
		case self::E_WC_COUPON_INVALID_FILTERED:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Coupon "%s" cannot be applied because it is not valid.', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		case self::E_WC_COUPON_NOT_EXIST:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Coupon "%s" cannot be applied because it does not exist.', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		case self::E_WC_COUPON_INVALID_REMOVED:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Sorry, it seems the coupon "%s" is invalid - it has now been removed from your order.', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		case self::E_WC_COUPON_NOT_YOURS_REMOVED:
			// We check for supplied billing email. On shortcode, this will be present for checkout requests.
			$billing_email = \Automattic\WooCommerce\Utilities\ArrayUtil::get_value_or_default( $_POST, 'billing_email' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
			if ( ! is_null( $billing_email ) ) {
				$err = sprintf(
					/* translators: %s: coupon code */
					esc_html__( 'Please enter a valid email to use coupon code "%s".', 'woocommerce' ),
					esc_html( $this->get_code() )
				);
			} else {
				$err = sprintf(
					/* translators: %s: coupon code */
					esc_html__( 'Please enter a valid email at checkout to use coupon code "%s".', 'woocommerce' ),
					esc_html( $this->get_code() )
				);
			}
			break;
		case self::E_WC_COUPON_ALREADY_APPLIED:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Coupon code "%s" already applied!', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		case self::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Sorry, coupon "%s" has already been applied and cannot be used in conjunction with other coupons.', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		case self::E_WC_COUPON_USAGE_LIMIT_REACHED:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Usage limit for coupon "%s" has been reached.', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		case self::E_WC_COUPON_EXPIRED:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Coupon "%s" has expired.', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		case self::E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET:
			$allowed_tags = array(
				'span'  => array(
					'class' => true,
				),
				'bdi'   => true,
				'small' => true,
			);
			$err          = sprintf(
				/* translators: %1$s: coupon code,  %2$s: coupon minimum amount */
				esc_html__( 'The minimum spend for coupon "%1$s" is %2$s.', 'woocommerce' ),
				esc_html( $this->get_code() ),
				wp_kses( wc_price( $this->get_minimum_amount() ), $allowed_tags )
			);
			break;
		case self::E_WC_COUPON_MAX_SPEND_LIMIT_MET:
			$allowed_tags = array(
				'span'  => array(
					'class' => true,
				),
				'bdi'   => true,
				'small' => true,
			);
			$err          = sprintf(
				/* translators: %1$s: coupon code, %2$s: coupon maximum amount */
				esc_html__( 'The maximum spend for coupon "%1$s" is %2$s.', 'woocommerce' ),
				esc_html( $this->get_code() ),
				wp_kses( wc_price( $this->get_maximum_amount() ), $allowed_tags )
			);
			break;
		case self::E_WC_COUPON_NOT_APPLICABLE:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Sorry, coupon "%s" is not applicable to your cart contents.', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		case self::E_WC_COUPON_USAGE_LIMIT_COUPON_STUCK:
			if ( is_user_logged_in() && wc_get_page_id( 'myaccount' ) > 0 && ! WC()->is_store_api_request() ) {
				$err = sprintf(
					/* translators: %1$s: coupon code, %2$s: myaccount page link. */
					wp_kses_data( __( 'Usage limit for coupon "%1$s" has been reached. If you were using this coupon just now but your order was not complete, you can retry or cancel the order by going to the <a href="%2$s">my account page</a>.', 'woocommerce' ) ),
					esc_html( $this->get_code() ),
					esc_attr( wc_get_endpoint_url( 'orders', '', wc_get_page_permalink( 'myaccount' ) ) )
				);
			} else {
				$err = $this->get_coupon_error( self::E_WC_COUPON_USAGE_LIMIT_REACHED );
			}
			break;
		case self::E_WC_COUPON_USAGE_LIMIT_COUPON_STUCK_GUEST:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Usage limit for coupon "%s" has been reached. Please try again after some time, or contact us for help.', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		case self::E_WC_COUPON_EXCLUDED_PRODUCTS:
			// Store excluded products that are in cart in $products.
			$products = array();
			if ( ! WC()->cart->is_empty() ) {
				foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
					if ( in_array( intval( $cart_item['product_id'] ), $this->get_excluded_product_ids(), true ) || in_array( intval( $cart_item['variation_id'] ), $this->get_excluded_product_ids(), true ) || in_array( intval( $cart_item['data']->get_parent_id() ), $this->get_excluded_product_ids(), true ) ) {
						$products[] = $cart_item['data']->get_name();
					}
				}
			}

			$err = sprintf(
				/* translators: %1$s: coupon code, %2$s: products list */
				esc_html__( 'Sorry, coupon "%1$s" is not applicable to the products: %2$s.', 'woocommerce' ),
				esc_html( $this->get_code() ),
				esc_html( implode( ', ', $products ) )
			);
			break;
		case self::E_WC_COUPON_EXCLUDED_CATEGORIES:
			// Store excluded categories that are in cart in $categories.
			$categories = array();
			if ( ! WC()->cart->is_empty() ) {
				foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
					$product_cats = wc_get_product_cat_ids( $cart_item['product_id'] );
					$intersect    = array_intersect( $product_cats, $this->get_excluded_product_categories() );

					if ( count( $intersect ) > 0 ) {
						foreach ( $intersect as $cat_id ) {
							$cat          = get_term( $cat_id, 'product_cat' );
							$categories[] = $cat->name;
						}
					}
				}
			}

			$err = sprintf(
				/* translators: "%1$s": coupon code, %2$s: categories list */
				esc_html__( 'Sorry, coupon "%1$s" is not applicable to the categories: %2$s.', 'woocommerce' ),
				esc_html( $this->get_code() ),
				esc_html( implode( ', ', array_unique( $categories ) ) )
			);
			break;
		case self::E_WC_COUPON_NOT_VALID_SALE_ITEMS:
			$err = sprintf(
				/* translators: %s: coupon code */
				esc_html__( 'Sorry, coupon "%s" is not valid for sale items.', 'woocommerce' ),
				esc_html( $this->get_code() )
			);
			break;
		default:
			$err = '';
			break;
	}
	return apply_filters( 'woocommerce_coupon_error', $err, $err_code, $this );
}