我有要添加到购物车的不同商品的 3 个 ID.我可以使用 https://url.com/shop/cart/?add-to-cart=3001
但是当我想添加 3 个项目时,我不能这样做.是否可以添加任何功能/脚本来将此功能添加到我的购物网站?
I have 3 IDs of the different items that I want to add to my shopping cart.
I could use https://url.com/shop/cart/?add-to-cart=3001
but when I want to add 3 items I can't do it. Is there any function/script I can add to add this ability to my shopping website?
我尝试在 add-to-cart
之后添加一个 &
并尝试添加一个新值,但 GET 被覆盖了对吗?:https://url.com/shop/cart/?add-to-cart=3001&add-to-cart=2002&add-to-cart=1001
I tried to add an &
after the add-to-cart
and tried to add a new value but GETs get overridden right?:
https://url.com/shop/cart/?add-to-cart=3001&add-to-cart=2002&add-to-cart=1001
我找到了答案!
只需将以下脚本添加到主题的functions.php:
Simply add the following script to your theme's functions.php:
function woocommerce_maybe_add_multiple_products_to_cart() {
// Make sure WC is installed, and add-to-cart qauery arg exists, and contains at least one comma.
if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
return;
}
// Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
$product_ids = explode( ',', $_REQUEST['add-to-cart'] );
$count = count( $product_ids );
$number = 0;
foreach ( $product_ids as $product_id ) {
if ( ++$number === $count ) {
// Ok, final item, let's send it back to woocommerce's add_to_cart_action method for handling.
$_REQUEST['add-to-cart'] = $product_id;
return WC_Form_Handler::add_to_cart_action();
}
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
$was_added_to_cart = false;
$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->product_type, $adding_to_cart );
/*
* Sorry.. if you want non-simple products, you're on your own.
*
* Related: WooCommerce has set the following methods as private:
* WC_Form_Handler::add_to_cart_handler_variable(),
* WC_Form_Handler::add_to_cart_handler_grouped(),
* WC_Form_Handler::add_to_cart_handler_simple()
*
* Why you gotta be like that WooCommerce?
*/
if ( 'simple' !== $add_to_cart_handler ) {
continue;
}
// For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
}
}
}
// Fire before the WC_Form_Handler::add_to_cart_action callback.
add_action( 'wp_loaded', 'woocommerce_maybe_add_multiple_products_to_cart', 15 );
然后您可以简单地使用 http://shop.com/shop/cart/?add-to-cart=3001,3282
一次添加多个项目.在不同的 ID 之间使用逗号.
And then you can simply use http://shop.com/shop/cart/?add-to-cart=3001,3282
to add multiple items at once. Put a comma between different IDs.
感谢 dsgnwrks 的解决方案.
这篇关于一次将多个项目添加到 WooCommerce 购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!