【问题标题】:Limit the number of cart items in Woocommerce限制 Woocommerce 中的购物车商品数量
【发布时间】:2018-02-10 22:10:28
【问题描述】:

我正在使用 Woocommerce 并需要以下内容:

  1. 由于产品正在销售到另一个国家,该国家的海关只允许总数为 6,所以我需要防止客户订购超过 6 个项目(产品)。

  2. 6 是项目或产品的总数。客户可以订购 1 件产品,数量为 6 或 2 件产品,每件数量为 3。海关只允许物品总数为 6。

  3. 如果购物车中有超过 6 件商品,则会出现警告并阻止客户继续结帐。

这是否可以将购物车商品限制为 6 个并在超出此限制时显示一条消息?

【问题讨论】:

  • 对该国家的海关规定投反对票。

标签: php wordpress woocommerce cart product-quantity


【解决方案1】:

如果您想限制购物车商品,有 2 个操作可以检查和控制:

  • 将产品添加到购物车时(在商店页面和产品页面中)
  • 当购物车页面中的数量更新时

使用 woocommerce_add_to_cart_validation 过滤器挂钩中的自定义函数,将允许您将购物车项目限制为最多 6 个,并在超出此限制时显示自定义消息:

// Checking and validating when products are added to cart
add_filter( 'woocommerce_add_to_cart_validation', 'only_six_items_allowed_add_to_cart', 10, 3 );

function only_six_items_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 >= 6 || $total_count > 6 ){
        // Set to false
        $passed = false;
        // Display a message
         wc_add_notice( __( "You can’t have more than 6 items in cart", "woocommerce" ), "error" );
    }
    return $passed;
}

使用 woocommerce_update_cart_validation 过滤器挂钩中的自定义函数,将允许您控制购物车商品数量更新到您的 6 个购物车商品限制,并在超过此限制时显示自定义消息:

// Checking and validating when updating cart item quantities when products are added to cart
add_filter( 'woocommerce_update_cart_validation', 'only_six_items_allowed_cart_update', 10, 4 );
function only_six_items_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 > 6 || $total_count > 6 ){
        // Set to false
        $passed = false;
        // Display a message
         wc_add_notice( __( "You can’t have more than 6 items in cart", "woocommerce" ), "error" );
    }
    return $passed;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码已经过测试并且可以工作

【讨论】:

  • 这项工作在 WC 3.9.1 上是否可以正常工作?答案是3岁。我会对其进行测试,但只是想知道我是否会遇到一些奇怪的行为。谢谢!
  • @SantiagoValdés 这应该适用于自 WooCommerce 3 以来的所有版本
【解决方案2】:

您可以在验证添加到购物车的产品时添加其他验证参数。 woocommerce_add_to_cart_validation 期望返回 true 或 false 值,具体取决于产品是否可以添加到购物车:

/**
 * When an item is added to the cart, check total cart quantity
 */
function so_21363268_limit_cart_quantity( $valid, $product_id, $quantity ) {

    $max_allowed = 6;
    $current_cart_count = WC()->cart->get_cart_contents_count();

    if( ( $current_cart_count > $max_allowed || $current_cart_count + $quantity > $max_allowed ) && $valid ){
        wc_add_notice( sprint( __( 'Whoa hold up. You can only have %d items in your cart', 'your-plugin-textdomain' ), $max ), 'error' );
        $valid = false;
    }

    return $valid;

}
add_filter( 'woocommerce_add_to_cart_validation', 'so_21363268_limit_cart_quantity', 10, 3 );

【讨论】:

  • 因为你像快速冈萨雷斯,迅捷如闪电……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
  • 2021-05-16
  • 1970-01-01
  • 2018-01-30
  • 2020-04-09
  • 2017-02-22
  • 2019-04-23
相关资源
最近更新 更多