auto-patch 638-dev-dev01-2024-05-14T20_44_36

This commit is contained in:
root
2024-05-14 20:44:36 +00:00
parent a941559057
commit 5dbb0b284e
1812 changed files with 29671 additions and 14588 deletions

View File

@@ -81,6 +81,18 @@ class WC_Coupon extends WC_Legacy_Coupon {
*/
protected $cache_group = 'coupons';
/**
* Error message.
*
* This property should not be considered public API, and should not be accessed directly.
* It is being added to supress PHP > 8.0 warnings against dynamic property creation, and all access
* should be through the getter and setter methods, namely `get_error_message()` and `set_error_message()`.
* In the future, the access modifier may be changed back to protected.
*
* @var string
*/
public $error_message;
/**
* Sorting.
*
@@ -864,6 +876,17 @@ class WC_Coupon extends WC_Legacy_Coupon {
return $this->error_message;
}
/**
* Sets the error_message string.
*
* @param string $message Message string.
*
* @return void
*/
public function set_error_message( string $message ) {
$this->error_message = $message;
}
/**
* Check if a coupon is valid for the cart.
*
@@ -899,7 +922,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
* @return bool
*/
public function is_valid_for_product( $product, $values = array() ) {
if ( ! $this->is_type( wc_get_product_coupon_types() ) ) {
if ( ! $this->is_type( wc_get_product_coupon_types() ) || ! is_a( $product, WC_Product::class ) ) {
return apply_filters( 'woocommerce_coupon_is_valid_for_product', false, $product, $this, $values );
}
@@ -944,20 +967,27 @@ class WC_Coupon extends WC_Legacy_Coupon {
* Converts one of the WC_Coupon message/error codes to a message string and.
* displays the message/error.
*
* @param int $msg_code Message/error code.
* @param int $msg_code Message/error code.
* @param string $notice_type Notice type.
*/
public function add_coupon_message( $msg_code ) {
$msg = $msg_code < 200 ? $this->get_coupon_error( $msg_code ) : $this->get_coupon_message( $msg_code );
public function add_coupon_message( $msg_code, $notice_type = 'success' ) {
if ( $msg_code < 200 ) {
$msg = $this->get_coupon_error( $msg_code );
$notice_type = 'error';
} else {
$msg = $this->get_coupon_message( $msg_code );
}
if ( ! $msg ) {
if ( empty( $msg ) ) {
return;
}
if ( $msg_code < 200 ) {
wc_add_notice( $msg, 'error' );
} else {
wc_add_notice( $msg );
// Since coupon validation is done multiple times (e.g. to ensure a valid cart), we need to check for dupes.
if ( wc_has_notice( $msg, $notice_type ) ) {
return;
}
wc_add_notice( $msg, $notice_type );
}
/**
@@ -1001,8 +1031,15 @@ class WC_Coupon extends WC_Legacy_Coupon {
$err = sprintf( __( '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:
/* translators: %s: coupon code */
$err = sprintf( __( 'Sorry, it seems the coupon "%s" is not yours - it has now been removed from your order.', 'woocommerce' ), esc_html( $this->get_code() ) );
// 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 ) ) {
/* translators: %s: coupon code */
$err = sprintf( __( 'Please enter a valid email to use coupon code "%s".', 'woocommerce' ), esc_html( $this->get_code() ) );
} else {
/* translators: %s: coupon code */
$err = sprintf( __( '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 = __( 'Coupon code already applied!', 'woocommerce' );
@@ -1152,4 +1189,26 @@ class WC_Coupon extends WC_Legacy_Coupon {
$this->set_amount( $info[3] ?? 0 );
$this->set_free_shipping( $info[4] ?? false );
}
/**
* Returns alternate error messages based on context (eg. Cart and Checkout).
*
* @param int $err_code Message/error code.
*
* @return array Context based alternate error messages.
*/
public function get_context_based_coupon_errors( $err_code = null ) {
switch ( $err_code ) {
case self::E_WC_COUPON_NOT_YOURS_REMOVED:
return array(
/* translators: %s: coupon code */
'cart' => sprintf( __( 'Please enter a valid email at checkout to use coupon code "%s".', 'woocommerce' ), esc_html( $this->get_code() ) ),
/* translators: %s: coupon code */
'checkout' => sprintf( __( 'Please enter a valid email to use coupon code "%s".', 'woocommerce' ), esc_html( $this->get_code() ) ),
);
default:
return array();
}
}
}