Add functionality to add multiple items and apply coupon from URL

This commit is contained in:
Paul Kang 2024-08-21 15:40:12 -07:00
parent fa6ea00b3c
commit a1193f2567
1 changed files with 63 additions and 9 deletions

View File

@ -788,22 +788,29 @@ class WC_Form_Handler {
* @param bool $url (default: false) URL to redirect to.
*/
public static function add_to_cart_action( $url = false ) {
if ( ! isset( $_REQUEST['add-to-cart'] ) || ! is_numeric( wp_unslash( $_REQUEST['add-to-cart'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! isset( $_REQUEST['add-to-cart'] ) ) {
return;
}
wc_nocache_headers();
// Check for the new format parameter
if ( isset( $_REQUEST['format'] ) && 'meta' === $_REQUEST['format'] ) {
self::add_multiple_items_and_apply_coupon();
return;
}
// Legacy behavior
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( wp_unslash( $_REQUEST['add-to-cart'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
return;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart );
if ( 'variable' === $add_to_cart_handler || 'variation' === $add_to_cart_handler ) {
$was_added_to_cart = self::add_to_cart_handler_variable( $product_id );
} elseif ( 'grouped' === $add_to_cart_handler ) {
@ -813,11 +820,11 @@ class WC_Form_Handler {
} else {
$was_added_to_cart = self::add_to_cart_handler_simple( $product_id );
}
// If we added the product to the cart we can now optionally do a redirect.
if ( $was_added_to_cart && 0 === wc_notice_count( 'error' ) ) {
$url = apply_filters( 'woocommerce_add_to_cart_redirect', $url, $adding_to_cart );
if ( $url ) {
wp_safe_redirect( $url );
exit;
@ -827,7 +834,54 @@ class WC_Form_Handler {
}
}
}
private static function add_multiple_items_and_apply_coupon() {
$product_ids = explode(',', wp_unslash($_REQUEST['add-to-cart']));
$quantities = isset($_REQUEST['quantity']) ? explode(',', wp_unslash($_REQUEST['quantity'])) : array_fill(0, count($product_ids), 1);
$coupon_code = isset($_REQUEST['coupon-code']) ? wp_unslash($_REQUEST['coupon-code']) : '';
$was_added_to_cart = false;
$url = false; // Ensure $url is defined
foreach ($product_ids as $index => $product_id) {
$product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($product_id));
$quantity = isset($quantities[$index]) ? absint($quantities[$index]) : 1;
$adding_to_cart = wc_get_product($product_id);
if (!$adding_to_cart) {
continue;
}
$add_to_cart_handler = apply_filters('woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart);
if ('variable' === $add_to_cart_handler || 'variation' === $add_to_cart_handler) {
$was_added_to_cart = self::add_to_cart_handler_variable($product_id);
} elseif ('grouped' === $add_to_cart_handler) {
$was_added_to_cart = self::add_to_cart_handler_grouped($product_id);
} elseif (has_action('woocommerce_add_to_cart_handler_' . $add_to_cart_handler)) {
do_action('woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url);
} else {
$was_added_to_cart = self::add_to_cart_handler_simple($product_id, $quantity);
}
}
if ($coupon_code && !WC()->cart->has_discount($coupon_code)) {
WC()->cart->add_discount(trim($coupon_code));
}
if ($was_added_to_cart && 0 === wc_notice_count('error')) {
$url = apply_filters('woocommerce_add_to_cart_redirect', $url, $adding_to_cart);
if ($url) {
wp_safe_redirect($url);
exit;
} elseif ('yes' === get_option('woocommerce_cart_redirect_after_add')) {
wp_safe_redirect(wc_get_cart_url());
exit;
}
}
}
/**
* Handle adding simple products to the cart.
*