在WooCommerce中,我实现了@jtsternberg的WooCommerce: Allow adding multiple products to the cart via the add-to-cart query string以允许一次添加多个产品,但是我收到了许多客户的抱怨,他们实际上试图使用包含多个产品的链接之一.
对于初学者,如果客户单击结帐,然后单击浏览器的“后退”按钮,则所有项目数量都会增加.通过在添加购物车行为完成后将用户重定向到去除了所有其他参数的购物车URL来解决此问题,但这并不理想.
我真正想要的是先检查商品是否在购物车中,然后仅在购物车中不存在时才添加到购物车中.有人做过类似的事情吗?
工作更新:
我最后修改了@jtsternberg中的代码,以使用完全独立的参数名称,以避免与默认的购物车行为冲突.然后,通过将行为包装在检查中以查看新参数是否存在,我可以在下面使用@LoicTheAztec的建议代码.这是完整的部分:
function custom_product_link() {
if (empty( $_REQUEST['multi-product-add'])) {
return;
}
$product_ids = explode( ',', $_REQUEST['multi-product-add'] );
foreach ( $product_ids as $product_id ) {
$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 );
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 );
}
}
if ( wc_notice_count( 'error' ) === 0 ) {
// If has custom URL redirect there
if ( $url = apply_filters( 'woocommerce_add_to_cart_redirect', false ) ) {
wp_safe_redirect( $url );
exit;
} elseif ( get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes' ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
}
function check_product_added_to_cart( $passed, $product_id, $quantity) {
if (!empty( $_REQUEST['multi-product-add'])) {
foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
// if products are already in cart:
if( $cart_item['product_id'] == $product_id ) {
// Set the verification variable to "not passed" (false)
$passed = false;
// (Optionally) Displays a notice if product(s) are already in cart
// wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'This product is already in your cart.', 'woocommerce' ), 'error' );
// Stop the function returning "false", so the products will not be added again
return $passed;
}
}
}
return $passed;
}
add_action( 'wp_loaded', 'custom_product_link', 15 );
add_action( 'woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3 );
解决方法:
就像您正在使用的代码中一样,其中具有woocommerce_add_to_cart_validation过滤器挂钩,您可以在自定义的挂钩函数中使用它来检查购物车中是否已经稀有产品,例如:
add_action( 'woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3 );
function check_product_added_to_cart( $passed, $product_id, $quantity) {
foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
// if products are already in cart:
if( $cart_item['data']->get_id() == $product_id ) {
// Set the verification variable to "not passed" (false)
$passed = false;
// (Optionally) Displays a notice if product(s) are already in cart
wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'This product is already in your cart.', 'woocommerce' ), 'error' );
// Stop the function returning "false", so the products will not be added again
return $passed;
}
}
return $passed;
}
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中.
代码已经过测试,通常可以与您的自定义一起使用…
For product quantities, you can use the
$quantity
argument with$cart_item['quantity']
in some conditions…