【问题标题】:How to limit cart and stay on the current page?如何限制购物车并停留在当前页面?
【发布时间】:2020-08-20 02:39:51
【问题描述】:

我正在使用 WooCommerce 并试图将购物车限制为仅 10 种产品。使用我的functions.php 中的以下代码,它可以完美运行。

function limit_cart_allowed_add_to_cart( $passed, $product_id, $quantity ) {
    $cart_items_count = WC()->cart->get_cart_contents_count();
    $total_count = $cart_items_count + $quantity;
    if ( $cart_items_count >= 10 || $total_count > 10 ) {
        $passed = false;
    }
    return $passed;
}
// Checking and validating when products are added to cart
add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_allowed_add_to_cart', 10, 3 );

function limit_cart_allowed_cart_update( $passed, $cart_item_key, $values, $updated_quantity ) {
    $cart_items_count = WC()->cart->get_cart_contents_count();
    $original_quantity = $values[ 'quantity' ];
    $total_count = $cart_items_count - $original_quantity + $updated_quantity;
    if ( $cart_items_count > 10 || $total_count > 10 ) {
        $passed = false;
    }
    return $passed;
}
// Checking and validating when updating cart item quantities when products are added to cart
add_filter( 'woocommerce_update_cart_validation', 'limit_cart_allowed_cart_update', 10, 4 );

但是,有一个问题,我昨天才注意到它,直到现在我找不到解决方案。

用于将产品添加到购物车的 AJAX 不再有效。 WooCommerce 的设置中有一个选项可以启用 AJAX 添加到购物车功能,它已启用,但由于上面的代码,它不再工作了。

当我尝试将产品添加到购物车并且购物车中的产品已经有 10 个时,它会将我重定向到单个产品页面。不应该是这样,我想保留 ajax 功能,但只是将购物车限制为 10 个产品。

【问题讨论】:

  • 我已经测试了您的代码,它在不禁用 Ajax 添加到购物车的情况下工作,但是当过滤器挂钩 woocommerce_add_to_cart_validation 返回 false(购物车中超过 10 件商品)时,使用 ajax add 时总是重定向到产品单页到购物车。您无能为力,因为这是正常过程。

标签: php wordpress woocommerce


【解决方案1】:

我想将此添加为注释,但为了代码格式,我将其作为答案。

重定向到单个产品页面是因为有一个 将产品添加到购物车时出错。这是默认情况下,它是 编码为这样工作。

woocommerce-ajax.php,你会看到它是如何工作的。

function woocommerce_ajax_add_to_cart() {

    global $woocommerce;
    check_ajax_referer( 'add-to-cart', 'security' );

    $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
    $quantity   = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] );

    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity );

    if ( $passed_validation && $woocommerce->cart->add_to_cart( $product_id, $quantity ) ) {

        do_action( 'woocommerce_ajax_added_to_cart', $product_id );

        if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
            woocommerce_add_to_cart_message( $product_id );
            $woocommerce->set_messages();
        }

        // Return fragments
        woocommerce_get_refreshed_fragments();

    } else {

        header( 'Content-Type: application/json; charset=utf-8' );

        // If there was an error adding to the cart, redirect to the product page to show any errors
        $data = array(
            'error' => true,
            'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id)
        );

        $woocommerce->set_messages();

        echo json_encode( $data );
    }

    die();
}
add_action('wp_ajax_woocommerce_add_to_cart', 'woocommerce_ajax_add_to_cart');

如果$passed_validation 为真,那么它将正常通过ajax 将产品添加到购物车中,现在由于您使用钩子woocommerce_add_to_cart_validationwoocommerce_update_cart_validation 将其设置为假,那么重定向到是正常的单个产品页面以显示错误消息。

我有两种不同的方法来解决这个问题。

  1. 离开它,接受这样一个事实,即一旦将产品添加到购物车时出错,它将重定向到单个产品页面。例如,您可以添加通知消息。

    function limid_cart_allowed_cart_update( $passed, $cart_item_key, $values, $updated_quantity ) {
    
     $cart_items_count = WC()->cart->get_cart_contents_count();
     $original_quantity = $values['quantity'];
     $total_count = $cart_items_count - $original_quantity + $updated_quantity;
    
     if( $cart_items_count > 10 || $total_count > 10 ){
         // Set to false
         $passed = false;
         // Display a message
          wc_add_notice( __( "You can’t have more than 10 items in cart", "woocommerce" ), "error" );
     }
     return $passed;
      }
      // Checking and validating when updating cart item quantities when products are added to cart
     add_filter( 'woocommerce_update_cart_validation', 'limid_cart_allowed_cart_update', 10, 4 );
    
  2. 覆盖 wp_ajax_woocommerce_add_to_cart 挂钩以不返回 URL。

【讨论】:

    【解决方案2】:

    WooCommerce 插件的最小和最大数量允许您设置订单和组中产品成本的限制,以及产品数量、产品变体、订单中的产品和组中的产品的数量限制。您可以将产品和产品变体添加到组中。无限数量的组。

    参考:https://mahbub.me/how-to-limit-min-and-max-quantity-for-woocommerce/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 2017-06-17
      相关资源
      最近更新 更多