You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce特定品类全局限购异常:购物车无法阻止超量

解决WooCommerce/Flatsome大麻籽限购2包的问题

1. 前端限制购物页数量操作(阻止超额增减)

Flatsome的商品数量控件由前端JS主导,默认不会触发后端验证,所以需要针对目标品类单独限制数量上限。

步骤1:在子主题functions.php中加载自定义JS

// 注册并加载数量限制脚本
add_action('wp_enqueue_scripts', 'custom_hemp_seeds_quantity_limit');
function custom_hemp_seeds_quantity_limit() {
    if (is_product()) {
        $product = wc_get_product(get_the_ID());
        // 替换123为大麻籽产品的分类ID,单产品限制可改用$product->get_id() == 456
        if ($product->get_category_ids() && in_array(123, $product->get_category_ids())) {
            wp_enqueue_script(
                'hemp-seeds-qty-limit',
                get_stylesheet_directory_uri() . '/js/hemp-qty-limit.js',
                array('jquery'),
                '1.0',
                true
            );
            // 传递最大数量参数到前端
            wp_localize_script('hemp-seeds-qty-limit', 'hempQtyParams', array(
                'maxQty' => 2
            ));
        }
    }
}

步骤2:创建前端JS文件限制数量

在子主题目录下新建js/hemp-qty-limit.js,写入以下代码:

jQuery(document).ready(function($) {
    // 监听+按钮点击
    $(document).on('click', '.quantity .plus', function(e) {
        const $input = $(this).siblings('input.qty');
        const currentQty = parseInt($input.val());
        if (currentQty >= hempQtyParams.maxQty) {
            e.preventDefault();
            e.stopPropagation();
            $input.val(hempQtyParams.maxQty);
            alert('大麻籽最多可购买2包');
        }
    });

    // 监听输入框手动输入
    $(document).on('change', '.quantity input.qty', function() {
        const currentQty = parseInt($(this).val());
        if (currentQty > hempQtyParams.maxQty) {
            $(this).val(hempQtyParams.maxQty);
            alert('大麻籽最多可购买2包');
        } else if (currentQty < 1) {
            $(this).val(1);
        }
    });
});

2. 后端强制修正购物车数量(解决更新/结算时的漏洞)

之前的验证仅提示错误但未强制修正数量,需要直接修改购物车条目并给出提示:

// 更新购物车时强制修正超额数量
add_action('woocommerce_update_cart_action_cart_updated', 'force_hemp_seeds_qty_limit');
function force_hemp_seeds_qty_limit() {
    if (is_cart()) {
        foreach (WC()->cart->get_cart() as $key => $item) {
            $product = $item['data'];
            if ($product->get_category_ids() && in_array(123, $product->get_category_ids())) {
                if ($item['quantity'] > 2) {
                    WC()->cart->set_quantity($key, 2);
                    wc_add_notice('大麻籽最多只能购买2包,已自动调整数量', 'error');
                }
            }
        }
    }
}

// 结算前最后检查并修正
add_action('woocommerce_check_cart_items', 'final_hemp_seeds_qty_check');
function final_hemp_seeds_qty_check() {
    foreach (WC()->cart->get_cart() as $key => $item) {
        $product = $item['data'];
        if ($product->get_category_ids() && in_array(123, $product->get_category_ids())) {
            if ($item['quantity'] > 2) {
                WC()->cart->set_quantity($key, 2);
                wc_add_notice('大麻籽最多只能购买2包,已自动调整数量', 'error');
            }
        }
    }
}

3. 关键注意事项

  • 将代码中的123替换为你大麻籽产品对应的分类ID,单产品限制可改为$product->get_id() == 产品ID。
  • 测试前清除Flatsome和WooCommerce的缓存,确保JS和PHP代码生效。
  • 建议在产品页面显眼位置添加限购提示,符合荷兰合规要求。

内容的提问来源于stack exchange,提问作者mjCode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 23:28:05