4

我使用WooCommerce 购物车数量在购物车更新答案代码后不会改变来自定义一些产品数量参数(如最小、最大和步长参数)

现在我有一个产品 id 为 27525 的变量产品,我想只对这个产品应用另一个规则,
数量规则详细信息: min(default) qty 1, max qty24和 step 1

我尝试使用下面的代码,问题是,
如果没有选择变体,默认/分钟数量将为 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
4

1 回答 1

3

woocommerce_available_variation钩子将productWC_Product_Variation 对象作为其第三个参数,而不是变量 product

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

可用的参数如下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钩子。

如果您想为所有产品变体设置相同的步骤,您可以使用此挂钩。

可用的参数如下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

您可以使用以下功能:

它适用于产品页面、购物车和循环中。

// 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。

于 2021-02-27T11:45:43.717 回答