在 Woocommerce 3.3 中为某些产品类别或产品 ID 设置最小数量 [英] Set min quantity for some product categories or product IDs in Woocommerce 3.3

查看:65
本文介绍了在 Woocommerce 3.3 中为某些产品类别或产品 ID 设置最小数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 woocommerce 中,我想为购物车中的特定产品设置最小数量.如果值较小,则会出现错误.

例如:只有当购物车中的产品 a、b、c 数量为 2 或更多时,客户才能结帐.

  • a: 2
  • b: 2
  • c: 2

否则会出现错误 - 每件商品的最小订购数量为 2".

这个论坛里有类似的话题,我觉得这段代码可能有用.

 add_filter( 'woocommerce_quantity_input_min', 'woocommerce_quantity_input_min',10, 2 );函数 woocommerce_quantity_input_min( $min, $product ){如果 ( $product-> ) {$min = 2;} 别的 {$min = 3;}返回 $min;}

有人可以完成这项工作吗?

我的真正需求:在我的商店里,我卖了很多手表.有 6 个产品类别:

  • 巴宝莉
  • 丹尼尔·惠灵顿
  • 柴油
  • Emporio Armani
  • 马克·雅各布斯
  • Michael Kors

我需要一个条件,客户只能在购物车页面中的每个产品数量至少为 2 时结帐,而不管它属于哪个类别.

请注意,我们提供添加产品形式存档页面和单个产品页面.如果从存档页面添加,则默认值为 1.

解决方案

更新 2 (现在也适用于存档页面和购物车上的产品变体).

以下代码将为特定产品类别设置最小数量:

//在单个产品页面上add_filter('woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2);函数 min_qty_input_args( $args, $product ) {## ---- 您的设置 ---- ##$product_categories = array('Burberry', 'Daniel Wellington','Diesel'、'Emporio Armani'、'Marc Jacobs'、'Michael Kors');$数量 = 2;## ---- 代码:为特定产品类别设置最小数量---- ##$product_id = $product->is_type('variation') ?$product->get_parent_id() : $product->get_id();if( has_term( $product_categories, 'product_cat', $product_id ) ){$args['min_value'] = $quantity;}返回 $args;}//在档案页面add_filter('woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2);函数 min_qty_loop_add_to_cart_button( $button, $product ) {//仅适用于非可变产品if( $product->is_type( 'variable' ) ) return $button;//出口## ---- 您的设置 ---- ##$product_categories = array('Burberry', 'Daniel Wellington','Diesel'、'Emporio Armani'、'Marc Jacobs'、'Michael Kors');$数量 = 2;## ---- 代码:为特定产品类别设置最小数量---- ##if( has_term( $product_categories, 'product_cat', $product->get_id() ) ){$class = implode( ' ', array_filter( array('按钮','产品类别_' .$product->get_type(),$product->is_purchasable() &&$product->is_in_stock() ?'add_to_cart_button' : '',$product->supports('ajax_add_to_cart') ?'ajax_add_to_cart' : '',) ) );$button = sprintf('<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',esc_url( $product->add_to_cart_url() ),esc_attr( isset( $quantity ) ? $quantity : 1 ),esc_attr( $product->get_id() ),esc_attr( $product->get_sku() ),esc_attr( isset( $class ) ? $class : 'button' ),esc_html( $product->add_to_cart_text() ));}返回 $ 按钮;}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

<块引用>

对于产品标签,您只需将代码中的product_cat"改为product_tag"即可.

<小时><块引用>

附加:对于产品 IDS(ID 数组)

要使其适用于产品 ID,请使用:

//在单个产品页面上add_filter('woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2);函数 min_qty_input_args( $args, $product ) {## ---- 您的设置 ---- ##$product_ids = array('23', '52', '75', '87', '90', '102');$数量 = 2;## ---- 代码:设置特定产品 ID 的最小数量 ---- ##if( in_array( $product->get_id(), $product_ids ) || ( $product->is_type('variation') && in_array( $product->get_parent_id(), $product_ids ) ){$args['min_value'] = $quantity;}返回 $args;}//在档案页面add_filter('woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2);函数 min_qty_loop_add_to_cart_button( $button, $product ) {//仅适用于非可变产品if( $product->is_type( 'variable' ) ) return $button;//出口## ---- 您的设置 ---- ##$product_ids = array('23', '52', '75', '87', '90', '102');$数量 = 2;## ---- 代码:设置特定产品 ID 的最小数量 ---- ##if( in_array( $product->get_id(), $product_ids ) ){$class = implode( ' ', array_filter( array('按钮','产品类别_' .$product->get_type(),$product->is_purchasable() &&$product->is_in_stock() ?'add_to_cart_button' : '',$product->supports('ajax_add_to_cart') ?'ajax_add_to_cart' : '',) ) );$button = sprintf('<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',esc_url( $product->add_to_cart_url() ),esc_attr( isset( $quantity ) ? $quantity : 1 ),esc_attr( $product->get_id() ),esc_attr( $product->get_sku() ),esc_attr( isset( $class ) ? $class : 'button' ),esc_html( $product->add_to_cart_text() ));}返回 $ 按钮;}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

In woocommerce, I would like to set a minimum quantity for specific products in cart. If value is less, then an error appears.

For example: Customer can checkout only if product a, b, c quantity in cart is 2 or more.

  • a: 2
  • b: 2
  • c: 2

Otherwise error appears - "Minimums order quantity for every item is 2".

There are similar topics in this forum, and I thought this code could be useful.

    add_filter( 'woocommerce_quantity_input_min', 'woocommerce_quantity_input_min',10, 2 );
    function woocommerce_quantity_input_min( $min, $product ){
    if ( $product-> ) {
          $min = 2;
    } else {
          $min = 3;
    }
    return $min;
    }

Can someone make this work?

My real needs: In my store I sell many watches. There are 6 product categories:

  • Burberry
  • Daniel Wellington
  • Diesel
  • Emporio Armani
  • Marc Jacobs
  • Michael Kors

I need a condition where a customer can only checkout if every product quantity in cart page is at least 2, regardless of which category it would be.

Note that we offer to add products form archive page and single product page. If added from archive page then default value is 1.

解决方案

Update 2 (It works too now for archive pages and on cart for product variations).

The following code will set a minimal quantity for specific product categories:

// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2 );
function min_qty_input_args( $args, $product ) {

    ## ---- Your settings ---- ##

    $product_categories = array('Burberry', 'Daniel Wellington',
        'Diesel', 'Emporio Armani', 'Marc Jacobs', 'Michael Kors');

    $quantity = 2;

    ## ---- The code: set minimun quantity for specific product categories ---- ##

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

    if( has_term( $product_categories, 'product_cat', $product_id ) ){
        $args['min_value'] = $quantity;
    }

    return $args;
}

// On archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2 );
function min_qty_loop_add_to_cart_button( $button, $product  ) {
    // Only for non variable products
    if( $product->is_type( 'variable' ) ) return $button; // Exit

    ## ---- Your settings ---- ##

    $product_categories = array('Burberry', 'Daniel Wellington',
        'Diesel', 'Emporio Armani', 'Marc Jacobs', 'Michael Kors');

    $quantity = 2;

    ## ---- The code: set minimun quantity for specific product categories ---- ##

    if( has_term( $product_categories, 'product_cat', $product->get_id() ) ){
        $class = implode( ' ', array_filter( array(
            'button',
            'product_type_' . $product->get_type(),
            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
            $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
        ) ) );

        $button = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            esc_attr( $product->get_id() ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $class ) ? $class : 'button' ),
            esc_html( $product->add_to_cart_text() )
        );
    }
    return $button;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

For product tags, you will have just to change 'product_cat' by 'product_tag' in the code.


ADDITION: For product IDS (array of IDs)

To make it work for product Ids instead use:

// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2 );
function min_qty_input_args( $args, $product ) {

    ## ---- Your settings ---- ##

    $product_ids = array('23', '52', '75', '87', '90', '102');

    $quantity = 2;

    ## ---- The code: set minimun quantity for specific product Ids ---- ##

    if( in_array( $product->get_id(), $product_ids ) || ( $product->is_type('variation') && in_array( $product->get_parent_id(), $product_ids ) ) ){
        $args['min_value'] = $quantity;
    }

    return $args;
}

// On archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2 );
function min_qty_loop_add_to_cart_button( $button, $product  ) {
    // Only for non variable products
    if( $product->is_type( 'variable' ) ) return $button; // Exit

    ## ---- Your settings ---- ##

    $product_ids = array('23', '52', '75', '87', '90', '102');

    $quantity = 2;

    ## ---- The code: set minimun quantity for specific product IDs ---- ##

    if( in_array( $product->get_id(), $product_ids ) ){
        $class = implode( ' ', array_filter( array(
            'button',
            'product_type_' . $product->get_type(),
            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
            $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
        ) ) );

        $button = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            esc_attr( $product->get_id() ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $class ) ? $class : 'button' ),
            esc_html( $product->add_to_cart_text() )
        );
    }
    return $button;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在 Woocommerce 3.3 中为某些产品类别或产品 ID 设置最小数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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