【问题标题】:Add multiple products to cart if they are not already in cart如果多个产品尚未在购物车中,则将它们添加到购物车
【发布时间】:2017-06-21 10:43:57
【问题描述】:

在 WooCommerce 中,我实施了 @jtsternberg's 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 );

【问题讨论】:

    标签: php wordpress woocommerce cart product


    【解决方案1】:

    在您使用的代码中,您有 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 文件或任何插件文件中。

    代码已经过测试,这通常应该适用于您的自定义......

    对于产品数量,在某些情况下,您可以将 $quantity 参数与 $cart_item['quantity'] 一起使用……

    【讨论】:

    • 哇,这比我的工作要简单得多!不过,我必须进行一些测试,这是否会干扰正常的添加到购物车的行为,例如当用户点击产品时?
    • @bhamrick 您必须测试...我无法判断,因为这取决于您的产品、设置...此过滤器代码只是避免客户将购物车中的相同商品添加到购物车 2 次。一般来说,客户不会将 2 次相同的商品放入购物车,他们会设置/更新数量……所以我认为这不是问题。
    • 谢谢!这很好用。我最终将参数名称从默认的add-to-cart 更改为multi-product-add,然后将foreach 语句包装在if (!empty( $_REQUEST['multi-product-add'])) 块中的代码中。
    • @bhamrick 太好了,这是一个很好的新功能,因为我看到很多次人们要求以某种方式同时添加多个产品......很高兴它成功了!......也许你可以更新你的答案那个代码,我的意思是代码 jtsternberg + 你的定制和最后一个 sn-p。您可以在问题的末尾添加一个带有 Working Update: 标题的分隔符...(只是一个建议,这将对社区有用)。最好在答案中包含您使用的代码,提及作者的学分......
    猜你喜欢
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2018-09-30
    相关资源
    最近更新 更多