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

@@ -333,7 +333,20 @@ class WC_Discounts {
$items_to_apply[] = $item_to_apply;
}
return $items_to_apply;
/**
* Filters the items that a coupon should be applied to.
*
* This filter allows you to modify the items that a coupon will be applied to before the discount calculations take place.
*
* @since 8.8.0
* @param array $items_to_apply The items that the coupon will be applied to.
* @param WC_Coupon $coupon The coupon object.
* @param WC_Discounts $this The discounts instance.
*
* @return array The modified list of items that the coupon should be applied to.
*/
return apply_filters( 'woocommerce_coupon_get_items_to_apply', $items_to_apply, $coupon, $this );
}
/**
@@ -939,6 +952,50 @@ class WC_Discounts {
return true;
}
/**
* Ensure coupon is valid for allowed emails or throw exception.
*
* @since 8.6.0
* @throws Exception Error message.
* @param WC_Coupon $coupon Coupon data.
* @return bool
*/
protected function validate_coupon_allowed_emails( $coupon ) {
$restrictions = $coupon->get_email_restrictions();
if ( ! is_array( $restrictions ) || empty( $restrictions ) ) {
return true;
}
$user = wp_get_current_user();
$check_emails = array( $user->get_billing_email(), $user->get_email() );
if ( $this->object instanceof WC_Cart ) {
$check_emails[] = $this->object->get_customer()->get_billing_email();
} elseif ( $this->object instanceof WC_Order ) {
$check_emails[] = $this->object->get_billing_email();
}
$check_emails = array_unique(
array_filter(
array_map(
'strtolower',
array_map(
'sanitize_email',
$check_emails
)
)
)
);
if ( ! WC()->cart->is_coupon_emails_allowed( $check_emails, $restrictions ) ) {
throw new Exception( $coupon->get_coupon_error( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED ), WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
return true;
}
/**
* Get the object subtotal
*
@@ -981,10 +1038,11 @@ class WC_Discounts {
* - 113: Excluded products.
* - 114: Excluded categories.
*
* @since 3.2.0
* @throws Exception Error message.
* @param WC_Coupon $coupon Coupon data.
* @param WC_Coupon $coupon Coupon data.
*
* @return bool|WP_Error
* @throws Exception Error message.
* @since 3.2.0
*/
public function is_coupon_valid( $coupon ) {
try {
@@ -998,9 +1056,10 @@ class WC_Discounts {
$this->validate_coupon_product_categories( $coupon );
$this->validate_coupon_excluded_items( $coupon );
$this->validate_coupon_eligible_items( $coupon );
$this->validate_coupon_allowed_emails( $coupon );
if ( ! apply_filters( 'woocommerce_coupon_is_valid', true, $coupon, $this ) ) {
throw new Exception( __( 'Coupon is not valid.', 'woocommerce' ), 100 );
throw new Exception( __( 'Coupon is not valid.', 'woocommerce' ), WC_Coupon::E_WC_COUPON_INVALID_FILTERED );
}
} catch ( Exception $e ) {
/**
@@ -1012,14 +1071,23 @@ class WC_Discounts {
*/
$message = apply_filters( 'woocommerce_coupon_error', is_numeric( $e->getMessage() ) ? $coupon->get_coupon_error( $e->getMessage() ) : $e->getMessage(), $e->getCode(), $coupon );
$additional_data = array(
'status' => 400,
);
$context_coupon_errors = $coupon->get_context_based_coupon_errors( $e->getCode() );
if ( ! empty( $context_coupon_errors ) ) {
$additional_data['details'] = $context_coupon_errors;
}
return new WP_Error(
'invalid_coupon',
$message,
array(
'status' => 400,
)
$additional_data,
);
}
return true;
}
}