Add multiple products to cart if they are not already in cart

徘徊边缘 提交于 2019-12-10 18:55:57

问题


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.

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.

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

回答1:


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:

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.

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…



来源:https://stackoverflow.com/questions/42034790/add-multiple-products-to-cart-if-they-are-not-already-in-cart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!