【问题标题】:Set a minimum quantity for cart items from specific WooCommerce product category为特定 WooCommerce 产品类别的购物车商品设置最低数量
【发布时间】:2019-08-20 23:16:55
【问题描述】:

在 WooCommerce 中,我正在尝试为特定产品类别的购物车商品设置最低数量。

基于“Minimum cart item quantity for a specific product category in WooCommerce”,这是我的代码尝试:

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
    $category      = 'games'; // The targeted product category
    $min_item_qty  = 4; // Minimum Qty required (for each item)
    $display_error = false; // Initializing

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        $item_quantity = $cart_item['quantity']; // Cart item quantity
        $product_id    = $cart_item['product_id']; // The product ID

        // For cart items remaining to "Noten" producct category
        if( has_term( $category, 'product_cat', $product_id ) && $item_quantity < $min_item_qty ) {
            wc_clear_notices(); // Clear all other notices

            // Add an error notice (and avoid checkout).
            wc_add_notice( sprintf( 'You should at least pick', $min_item_qty ,'products  for'  ,$category,  'category' ), 'error' );
            break; // Stop the loop
        }
    }
}

它不像我想要的那样工作,因为为特定产品类别中的第一个购物车项目设置了最小数量,但不是针对该特定产品类别中的所有项目全局设置。任何帮助表示赞赏。

【问题讨论】:

    标签: php wordpress woocommerce cart custom-taxonomy


    【解决方案1】:

    您需要先统计目标产品类别中的商品数量……然后您可以在商品数量低于定义的最小值时显示错误通知:

    add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
    function wc_min_item_required_qty() {
        $category  = 'Games'; // The targeted product category
        $min_qty   = 4; // Minimum Qty required (for each item)
        $qty_count = 0; // Initializing
    
        // Loop through cart items
        foreach(WC()->cart->get_cart() as $item ) {
            // Count items from the targeted product category
            if( has_term( $category, 'product_cat', $item['product_id'] ) ) {
                $qty_count += $item['quantity'];
            }
        }
    
        // Display error notice avoiding checkout
        if( $qty_count != 0 && $qty_count < $min_qty ) {
            wc_clear_notices(); // Clear all other notices
    
            // Add an error notice (and avoid checkout).
            wc_add_notice( sprintf(
                __("You should pick at least %s items from %s category.", "woocommerce"),
                '<strong>' . $min_qty . '</strong>',
                '<strong>' . $category . '</strong>'
            ), 'error' );
        }
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 先生,此代码有问题,因为如果用户没有来自特定类别的任何产品,则此代码无论如何都会运行,例如我希望如果用户在他的购物车中没有来自特定类别的任何产品代码不应该是执行
    • @yosober 我已经更新了代码……它现在应该可以正常工作了。
    • 我已经实现了它在单个类别上工作的代码。如果我想在多个类别中使用怎么办。对于杂货类,它需要 10 个单位,水果需要 5 个单位,依此类推。
    猜你喜欢
    • 2019-09-18
    • 2020-09-21
    • 2021-07-22
    • 2019-01-27
    • 2018-07-31
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 2021-05-16
    相关资源
    最近更新 更多