以编程方式为 WooCommerce 中的特定可变产品设置最小、最大和步骤数量

Set minimum, maximum and step quantity programmatically for specific variable products in WooCommerce

我使用 答案代码来自定义一些产品数量参数 (作为最小、最大和步骤参数)

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

我尝试使用下面的代码,问题是,
如果不选择变体,default/min数量为25,规则同一般数量设置,
如果我选择可用的变体,默认数量将为 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?

$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

woocommerce_available_variation 挂钩将 WC_Product_Variation 产品对象 作为其 第三个参数 不是变量产品

With the woocommerce_available_variation hook you cannot set the product step.

可用参数如下WooCommerce /includes/class-wc-product-variable.php 文件的源代码@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
);

要将步骤设置为产品变体,您需要使用 woocommerce_quantity_input_args 挂钩。

If you want to set the same step to all product variations you can use this hook.

可用参数如下WooCommerce /includes/wc-template-functions.php 文件的源代码@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 的回答:

回答

为了回答您的问题,如果您希望父变量产品 ID 等于 27525 的所有产品变体都具有以下值:

  • 最少数量: 1
  • 最大数量: 24
  • 步骤: 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.