简体   繁体   English

防止客户从 Woocommerce 中的 3 个以上供应商订购

[英]Prevent customers from ordering from more than 3 Vendors in Woocommerce

I have found similar solutions to this, for example: "How to limit orders to one category".我找到了类似的解决方案,例如:“如何将订单限制为一个类别”。 I have tried to modify the code but it's not specific enough.我试图修改代码,但它不够具体。

In my case, each Vendor is defined by a product attribute value, 8 Terms at all.就我而言,每个供应商都由一个产品属性值定义,总共有 8 个术语。 If the cart contains products from more than 3 different Terms, I need to set up a message that says "Sorry, you may only order from 3 different Vendors at a time".如果购物车包含来自 3 个以上不同条款的产品,我需要设置一条消息,显示“抱歉,您一次只能从 3 个不同的供应商处订购”。

This is what I'm using as a starting point:这是我用作起点的内容:

add_action( 'woocommerce_add_to_cart', 'three_vendors' );
function three_vendors() {

  if ATTRIBUTE = SELECT A VENDOR; NUMBER OF TERMS > 3 {

   echo "Sorry! You can only order from 3 Vendors at a time.”;

   }
}

The middle line is me filling in the blanks using non-php language.中间一行是我用非php语言填空。

I am looking for a way to define the amount of variations in the cart.我正在寻找一种方法来定义购物车中的变化量。 If this is not possible to do with Attributes, I am open to use Categories instead.如果这不能用属性来做,我愿意使用类别来代替。

Does anyone know how to do this?有谁知道如何做到这一点?

Update: Is not possible to manage variations with woocommerce_add_to_cart_valisation hook更新:无法使用woocommerce_add_to_cart_valisation挂钩管理变体

Instead we can use a custom function hooked in woocommerce_add_to_cart filter hook, removing the last added cart item when more than 3 vendors (items) are in cart:相反,我们可以使用挂在woocommerce_add_to_cart过滤器钩子中的自定义函数,当购物车中有超过 3 个供应商(项目)时删除最后添加的购物车项目:

// Remove the cart item and display a notice when more than 3 values for "pa_vendor" attibute.
add_action( 'woocommerce_add_to_cart', 'no_more_than_three_vendors', 10, 6 );

function no_more_than_three_vendors( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {

    // Check only when there is more than 3 cart items
    if ( WC()->cart->get_cart_contents_count() < 4 ) return;

    // SET BELOW your attribute slug… always begins by "pa_"
    $attribute_slug = 'pa_vendor'; // (like for "Color" attribute the slug is "pa_color")

    // The current product object
    $product = wc_get_product( $variation_id );
    // the current attribute value of the product
    $curr_attr_value = $product->get_attribute( $attribute_slug );

    // We store that value in an indexed array (as key /value)
    $attribute_values[ $curr_attr_value ] = $curr_attr_value;

    //Iterating through each cart item
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        // The attribute value for the current cart item
        $attr_value = $cart_item[ 'data' ]->get_attribute( $attribute_slug );

        // We store the values in an array: Each different value will be stored only one time
        $attribute_values[ $attr_value ] = $attr_value;
    }
    // We count the "different" values stored
    $count = count($attribute_values);

    // if there is more than 3 different values
    if( $count > 3 ){
        // We remove last cart item
        WC()->cart->remove_cart_item( $cart_item_key );
        // We display an error message
        wc_clear_notices();
        wc_add_notice( __( "Sorry, you may only order from 3 different Vendors at a time. This item has been removed", "woocommerce" ), 'error' );
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

This code is tested and should works for you.此代码经过测试,应该适合您。 It will check for the attribute values regarding your specific attribute and will remove last added cart item for more than 3 attribute values.它将检查与您的特定属性相关的属性值,并将删除超过 3 个属性值的最后添加的购物车项目。

The only annoying thing is that I can't remove the classic added to cart notice for the moment.唯一令人讨厌的是,我暂时无法删除经典添加到购物车的通知。 I will try to find another way…我会尝试寻找另一种方式......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM