php 购物车批量添加,php-一次将多个项目添加到WooCommerce购物车

我有3个要添加到购物车中的不同商品的ID.

我可以使用https://url.com/shop/cart/?add-to-cart=3001,但是当我想添加3个项目时,我做不到.我可以添加任何功能/脚本来将此功能添加到购物网站吗?

我试图添加&加入购物车并尝试添加新值后,但是GET被覆盖了吗?:

https://url.com/shop/cart/?add-to-cart=3001\u0026amp;add-to-cart=2002\u0026amp;add-to-cart=1001

解决方法:

我找到了答案!

只需将以下脚本添加到主题的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之间放置逗号.

感谢dsgnwrks提供解决方案.

标签:woocommerce,web,php

来源: https://codeday.me/bug/20191118/2024513.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值