【问题标题】:Set minimum order quantity by category (Woocommerce)按类别设置最小订单数量(Woocommerce)
【发布时间】:2020-11-26 15:42:25
【问题描述】:

我的商店有不同的产品类别。我有一个“nettoyage”类别,只有当购物车中“nettoyage”项目的数量大于 3 时,我才允许客户订购。

现在,我已将默认数量设置为 3,并且不允许用户在产品页面上转到下方。但我想要的是允许他们每个添加 1 个,并允许他们订购,只要购物车中的“nettoyage”产品数量 > 3。我如何重构此代码来做到这一点?

现在,我的 functions.php 中有以下代码:

add_filter('woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 10, 2);

function bloomer_woocommerce_quantity_changes($args, $product)
{

    if (!is_cart()) {
        if (is_singular('product') && (has_term('nettoyage', 'product_cat'))) {

            $args['input_value'] = 3; // Start from this value (default = 1)
            $args['max_value'] = 10; // Max quantity (default = -1)
            $args['min_value'] = 3; // Min quantity (default = 0)
            $args['step'] = 1; // Increment/decrement by this value (default = 1)

        }
    }

    return $args;
}

add_filter('woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2);
function min_qty_filter_callback($args, $product)
{
    $categories = array('Noten'); // The targeted product category(ies)
    $min_qty    = 3; // The minimum product quantity

    $product_id = $product->is_type('simple') ? $product->get_parent_id() : $product->get_id();

    if (has_term($categories, 'product_cat', $product_id)) {
        $args['min_value'] = $min_qty;
    }
    return $args;
}

// On shop and archives pages
add_filter('woocommerce_loop_add_to_cart_args', 'min_qty_loop_add_to_cart_args', 10, 2);
function min_qty_loop_add_to_cart_args($args, $product)
{
    $categories = array('nettoyage'); // The targeted product category
    $min_qty    = 3; // The minimum product quantity

    $product_id = $product->get_id();

    if (has_term($categories, 'product_cat', $product_id)) {
        $args['quantity'] = $min_qty;
    }
    return $args;
}

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

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $cart_item_key) {
        $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($categories, '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("Le service livraison nettoyage n'est valable qu'à partir de %s paires!", $min_item_qty, $item_quantity), 'error');
            break; // Stop the loop
        }
    }
}

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    终于找到了解决办法。对于那些感兴趣的人,这里是自定义函数:

    add_action('woocommerce_check_cart_items', 'custom_set_min_total');
    function custom_set_min_total()
    {
        if (is_cart() || is_checkout()) {
    
            global $woocommerce, $product;
            $i = 0;
    
            foreach ($woocommerce->cart->cart_contents as $product) :
                $minimum_cart_product_total = 3;
    
                if (has_term('nettoyage', 'product_cat', $product['product_id'])) :
                    $total_quantity += $product['quantity'];
                endif;
    
            endforeach;
    
            foreach ($woocommerce->cart->cart_contents as $product) :
                if (has_term('nettoyage', 'product_cat', $product['product_id'])) :
                    if ($total_quantity < $minimum_cart_product_total && $i == 0) {
                        wc_add_notice(
                            sprintf(
                                '<strong>A Minimum of %s products is required from the nettoyage category before checking out.</strong>'
                                    . '<br />Current number of items in the cart: %s.',
                                $minimum_cart_product_total,
                                $total_quantity
                            ),
                            'error'
                        );
                    }
                    $i++;
                endif;
            endforeach;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 2020-12-02
      • 2012-05-04
      • 1970-01-01
      • 2021-05-08
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      相关资源
      最近更新 更多