通过编程为WooCommerce中的特定可变产品设置最小,最大和步长数量 [英] Set minimum, maximum and step quantity programmatically for specific variable products in WooCommerce

查看:35
本文介绍了通过编程为WooCommerce中的特定可变产品设置最小,最大和步长数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 WooCommerce购物车数量不会改变购物车更新后回答代码以自定义一些产品数量参数(作为最小,最大和步进参数).

I use WooCommerce cart quantity won't change after cart update answer code to customize some product quantity arguments (as min, max and step arguments).

现在我有一个产品ID为27525的可变产品,我只想将另一条规则应用于此产品,
数量规则详细信息:最小(默认)数量[strong] 1 ,最大数量 24 和步骤 1 .

Now I have a variable product with product id 27525, and I want to apply another rule to this product only,
Quantity rule details: min(default) qty 1, max qty 24 and step 1.

我尝试使用下面的代码,问题是,
如果未选择任何变化,则默认/最小数量为25,该规则与常规数量设置相同,
如果我选择一个可用的变体,则默认数量为24.

I tried to use the code below, the problem is,
if no variations are selected, the default/min quantity will be 25, the rule is the same as the general quantity settings,
If I choose an available variation, the default quantity will be 24.

add_filter( 'woocommerce_available_variation', 'variation_quantity_input_args', 10, 2 );
function variation_quantity_input_args( $args, $product ) {
    $sample_product_id = array(27525);

    if (in_array( $product->get_id(), $sample_product_id)) {
        $args['min_qty'] = 1;
        $args['max_qty'] = 24;
        return $args;
    }
}

如何更改代码并将规则应用于变量产品27525?

How to change the code and apply the rules to variable product 27525?

$args['min_qty'] = 1; //default and min qty
$args['max_qty'] = 24; // max qty
$args['step'] = 1; // Seems won't work use woocommerce_available_variation, but I want to change the step to 1

推荐答案

The woocommerce_available_variation hook has the WC_Product_Variation product object as its third parameter and not the variable product.

使用 woocommerce_available_variation 钩子,您无法设置产品步骤.

可用参数是以下( WooCommerce /includes/class-wc-product-variable.php 的源代码>文件@version 3.0.0):

The available arguments are the following (source code of the WooCommerce /includes/class-wc-product-variable.php file @version 3.0.0):

return apply_filters(
    'woocommerce_available_variation',
    array(
        'attributes'            => $variation->get_variation_attributes(),
        'availability_html'     => wc_get_stock_html( $variation ),
        'backorders_allowed'    => $variation->backorders_allowed(),
        'dimensions'            => $variation->get_dimensions( false ),
        'dimensions_html'       => wc_format_dimensions( $variation->get_dimensions( false ) ),
        'display_price'         => wc_get_price_to_display( $variation ),
        'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),
        'image'                 => wc_get_product_attachment_props( $variation->get_image_id() ),
        'image_id'              => $variation->get_image_id(),
        'is_downloadable'       => $variation->is_downloadable(),
        'is_in_stock'           => $variation->is_in_stock(),
        'is_purchasable'        => $variation->is_purchasable(),
        'is_sold_individually'  => $variation->is_sold_individually() ? 'yes' : 'no',
        'is_virtual'            => $variation->is_virtual(),
        'max_qty'               => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
        'min_qty'               => $variation->get_min_purchase_quantity(),
        'price_html'            => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
        'sku'                   => $variation->get_sku(),
        'variation_description' => wc_format_content( $variation->get_description() ),
        'variation_id'          => $variation->get_id(),
        'variation_is_active'   => $variation->variation_is_active(),
        'variation_is_visible'  => $variation->variation_is_visible(),
        'weight'                => $variation->get_weight(),
        'weight_html'           => wc_format_weight( $variation->get_weight() ),
    ),
    $this,
    $variation
);

要设置产品变型步骤,您将需要使用

To set the step to product variations you will need to use the woocommerce_quantity_input_args hook.

如果您想对所有产品版本设置相同的步骤,则可以使用此挂钩.

可用参数是以下( WooCommerce /includes/wc-template-functions.php 文件的源代码@version 2.5.0):

The available arguments are the following (source code of the WooCommerce /includes/wc-template-functions.php file @version 2.5.0):

$defaults = array(
    'input_id'     => uniqid( 'quantity_' ),
    'input_name'   => 'quantity',
    'input_value'  => '1',
    'classes'      => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
    'max_value'    => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),
    'min_value'    => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),
    'step'         => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),
    'pattern'      => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
    'inputmode'    => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
    'product_name' => $product ? $product->get_title() : '',
    'placeholder'  => apply_filters( 'woocommerce_quantity_input_placeholder', '', $product ),
);

如果要管理产品步骤,最小数量和最大数量作为产品的自定义元,请参阅@LoicTheAztec的答案:

If you want to manage the product step, the minimum quantity and the maximum quantity as a custom meta of the product, see @LoicTheAztec's answer:

要回答您的问题,如果要使所有父变量产品ID等于27525的产品变体具有以下值:

To answer your question, if you want all product variations that have the parent variable product id equal to 27525 to have the following values:

  • 最小数量:1
  • 最大数量:24
  • 步骤:1
  • Min quantity: 1
  • Max quantity: 24
  • Step: 1

您可以使用以下功能:

它可以在产品页面,购物车和循环中使用.

It works in the product page, in the cart and in the loop.

// set the min and max quantity for each product variation (based on the variable product id)
add_filter( 'woocommerce_available_variation', 'variation_quantity_input_args', 10, 3 );
function variation_quantity_input_args( $args, $product, $variation ) {
    // set variable product ids
    $sample_product_id = array( 27525 );
    // if the selected variation belongs to one of the variable product ids
    // set the min and max quantity
    if ( in_array( $variation->get_parent_id(), $sample_product_id ) ) {
        $args['min_qty'] = 1;
        $args['max_qty'] = 24;
        return $args;
    }
    return $args;
}


// set the step for specific variable products (and for each product variation)
add_filter( 'woocommerce_quantity_input_args', 'set_step_for_specific_variable_products', 10, 2 );
function set_step_for_specific_variable_products( $args, $product ) {
    // set variable product ids
    $sample_product_id = array( 27525 );
    
    // on the product page
    if ( ! is_cart() ) {
        // if the id of the variable product is in the array
        if ( in_array( $product->get_id(), $sample_product_id ) ) {
            $args['input_value'] = 1;
            $args['min_value'] = 1;
            $args['max_value'] = 24;
            $args['step'] = 1;
        }
    // in the cart
    } else {
        // if the id of the product variation belongs to a variable product present in the array
        if ( in_array( $product->get_parent_id(), $sample_product_id ) ) {
            $args['min_value'] = 1;
            $args['step'] = 1;
        }
    }
    return $args;
}

该代码已经过测试,可以正常工作.将其添加到活动主题的functions.php中.

这篇关于通过编程为WooCommerce中的特定可变产品设置最小,最大和步长数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆