简体   繁体   English

如果多个产品尚未装入购物车,请将其添加到购物车

[英]Add multiple products to cart if they are not already in cart

In WooCommerce I've implemented @jtsternberg's WooCommerce: Allow adding multiple products to the cart via the add-to-cart query string to allow adding multiple products at once, but I've received many complaints from customers who actually try to use one of the links containing multiple products. 在WooCommerce中我已经实现了@ jtsternberg的 WooCommerce:允许通过添加到购物车的查询字符串将多个产品添加到购物车,以允许一次添加多个产品,但我收到了许多实际尝试使用其中一个产品的客户的投诉包含多个产品的链接。

For starters, if the customer clicks checkout and then clicks the browser "back" button, all the item quantities increment. 对于初学者,如果客户点击结帐然后点击浏览器“返回”按钮,则所有商品数量都会增加。 I solved this by redirecting the user to the cart URL stripped of any additional parameters after the add-to-cart behavior completes, but it's not ideal. 我通过在添加到购物车行为完成后将用户重定向到剥去了任何其他参数的购物车URL来解决这个问题,但这并不理想。

What I really want is to check if the item is in the cart first and only add to cart if it isn't there already. 我真正想要的是检查物品是否首先在购物车中,如果它已经存在则只添加到购物车。 Has anyone done something similar? 有没有人做过类似的事情?

Working Update: I ended up modifying the code from @jtsternberg to use a completely separate param name in order to avoid conflict with the default add-to-cart behavior. 工作更新:我最终修改了@jtsternberg中的代码,以使用完全独立的参数名称,以避免与默认的添加到购物车行为发生冲突。 Then I was able to use @LoicTheAztec's suggested code below by wrapping the behavior in a check to see if that new param exists. 然后,我可以使用@ LoicTheAztec建议的代码,将行为包装在一个检查中,看看是否存在新的参数。 Here's the full section: 这是完整的部分:

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 );

As in the code you are using you have woocommerce_add_to_cart_validation filter hook inside it, you can use it in a custom hooked function to check if products rare already in cart with something like: 在您使用的代码中,您可以在其中使用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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

Code is tested and this normally should works with your customization… 代码经过测试,这通常适用于您的自定义...

For product quantities, you can use the $quantity argument with $cart_item['quantity'] in some conditions… 对于产品数量,您可以在某些条件下使用$quantity参数和$cart_item['quantity'] ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM