【问题标题】:Set custom quantity arguments to products from specific category in WooCommerce在 WooCommerce 中为特定类别的产品设置自定义数量参数
【发布时间】:2020-11-17 11:21:01
【问题描述】:

我正在尝试为我的 woocommerce 商店中的特定类别设置最低订购数量。我写了一段代码,但似乎购物车中的最低数量适用于所有类别,而不仅仅是我设置的类别(“nettoyage”)......我做错了什么?

这是我的代码(来自我的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;
}

function sww_check_category_for_minimum()
{
    // set the minimum quantity in the category to purchase
    $min_quantity = 3;

    // set the id of the category for which we're requiring a minimum quantity
    $category_id = 40;

    // get the product category
    $product_cat = get_term($category_id, 'product_cat');
    $category_name = '<a href="' . get_term_link($category_id, 'product_cat') . '">' . $product_cat->name . '</a>';

    // get the quantity category in the cart
    $category_quantity = sww_get_category_quantity_in_cart($category_id);

    if ($category_quantity < $min_quantity) {
        // render a notice to explain the minimum
        wc_add_notice(sprintf('You must order at least 3 products from the %2$s category to be able to order!', $min_quantity, $category_name), 'error');
    }
}
add_action('woocommerce_check_cart_items', 'sww_check_category_for_minimum');

//Returns the quantity of products from a given category in the WC cart
function sww_get_category_quantity_in_cart($category_id)
{
    // get the quantities of cart items to check against
    $quantities = WC()->cart->get_cart_item_quantities();

    // start a counter for the quantity of items from this category
    $category_quantity = 0;

    // loop through cart items to check the product categories
    foreach ($quantities as $product_id => $quantity) {

        $product_categories = get_the_terms($product_id, 'product_cat');

        // check the categories for our desired one
        foreach ($product_categories as $category) {

            // if we find it, add the line item quantity to the category total
            if ($category_id === $category->term_id) {
                $category_quantity += $quantity;
            }
        }
    }

    return $category_quantity;
}

【问题讨论】:

    标签: php wordpress woocommerce taxonomy-terms product-quantity


    【解决方案1】:

    改用以下方法(不需要任何检查)

    // General quantity settings
    add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
    function custom_quantity_input_args( $args, $product ){
        if ( has_term( array('nettoyage'), 'product_cat', $product->get_id() ) ) {
            if( ! is_cart() ) {
                $args['input_value'] = 3; // Starting value
            }
            $args['min_value'] = 3; // Minimum value
            $args['max_value'] = 10; // Maximum value
        }
        return $args;
    }
    
    // For Ajax add to cart button (define the min value)
    add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
    function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
        if ( has_term( array('nettoyage'), 'product_cat', $product->get_id() ) ) {
            $args['quantity'] = 3; // Min value
        }
        return $args;
    }
    
    // For product variations (define the min value)
    add_filter( 'woocommerce_available_variation', 'custom_available_variation_min_qty', 10, 3);
    function custom_available_variation_min_qty( $data, $product, $variation ) {
        if ( has_term( array('nettoyage'), 'product_cat', $product->get_id() ) ) {
            $args['min_qty'] = 3; // Min value
        }
    
        return $data;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。测试和工作

    注意:需要为每个所需产品设置“nettoyage”类别。如果没有,它将无法正常工作。

    【讨论】:

    • 完美运行,非常感谢队友! (而且更干净)
    猜你喜欢
    • 2021-05-08
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 2016-07-16
    • 2018-08-19
    • 2018-01-25
    • 2023-04-08
    相关资源
    最近更新 更多