问题描述
我试图限制Woocommerce商店上的客户只能从1个供应商订购产品。一次。我正在定义供应商通过称为供应商的定制分类法。我正在尝试的代码出于某种原因只是限制了所有内容。
function filter_woocommerce_add_to_cart_validation($ passed,$ product_id,$ quantity,$ variation_id = null, $ variations = null){
//如果通过
if($ passed){
//如果添加产品时购物车不为空
if( WC()->购物车-> is_empty()){
//设置变量
$ current_product_tag_ids = array();
$ in_cart_product_tag_ids = array();
//通过product_id获取当前产品类别
$ current_product_tag_ids = wc_get_product_term_ids($ product_id,'supplier');
//遍历购物车项以检查产品类别
foreach(WC()-> cart-> get_cart()as $ cart_item){
//获取产品通过购物车商品的商品ID中的商品类别来分类购物车中的商品
}
//删除重复的值
$ in_cart_product_tag_ids = array_unique($ in_cart_product_tag_ids,SORT_NUMERIC);
//比较
$ compare = array_diff($ current_product_tag_ids,$ in_cart_product_tag_ids);
//结果不为空
if(!empty($ compare)){
wc_add_notice('该产品与其他供应商一起使用。请仅向1个供应商订购时间,错误);
$ passed = false;
}
}
}
返回$ pass;
}
add_filter('woocommerce_add_to_cart_validation','filter_woocommerce_add_to_cart_validation',10,5);
我并不是要限制每个供应商使用1种产品,而是要限制它们,以便他们只能每个订单从1个供应商处订购产品。一旦他们将供应商供应商1的产品添加到购物篮中,例如,他们将无法添加供应商1以外的其他任何供应商的产品。
我在上一篇文章中尝试使用类别而不是自定义分类法,但我需要将它们分开,因此我们不限于类别。可以在以下位置找到此帖子:
以下内容将允许从一个供应商网站添加到购物车中仅限:
add_filter( woocommerce_add_to_cart_validation, only_one_supplier_by_order,10、2);
函数only_one_supplier_by_order($ passed,$ product_id){
if(WC()-> cart-> is_empty())
return $ passed;
$ taxonomy =供应商;
$ term_ids = wp_get_post_terms($ product_id,$ taxonomy,[‘fields’=>‘ids’]);
//遍历购物车项目
foreach(WC()-> cart-> get_cart()as $ cart_item){
if(!has_term($ term_ids, $ taxonomy,$ cart_item ['product_id'])){
//显示自定义通知
wc_add_notice(__('该产品与其他供应商一起使用。请一次仅从1个供应商处订购。 '),'错误');
返回false; //避免将其添加到购物车
}
}
返回$ pass;
}
现在要确保客户无法与购物车中的其他供应商结账,您可以添加此:
//要确保(避免结帐)
add_action('woocommerce_check_cart_items','only_one_supplier_by_order_check');
function only_one_supplier_by_order_check(){
$ taxonomy =‘供应商’;
$ term_names = []; //初始化
//遍历购物车项目
foreach(WC()-> cart-> get_cart()as $ cart_item){
$ terms = wp_get_post_terms( $ cart_item ['product_id'],$ taxonomy);
if(!empty($ terms)){
$ term = reset($ terms);
$ term_names [$ term-> term_id] = $ term->名称;
}
}
//仅允许购物车中的一个供应商(避免结帐多个
if(count($ term_names)> 1){
//显示自定义通知
wc_add_notice(__('购物车中只允许同时提供一个供应商的商品'),'error');
}
}
代码进入您的活动子主题(或活动主题)的functions.php文件中。 / p>
I'm trying to restrict customers on my Woocommerce store to only be allowed to order products from 1 "supplier" at a time. I am defining the "Supplier" by a custom taxonomy called "supplier". The code I am trying is just restricting everything for some reason.
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// If passed
if ( $passed ) {
// If cart is NOT empty when a product is added
if ( !WC()->cart->is_empty() ) {
// Set vars
$current_product_tag_ids = array();
$in_cart_product_tag_ids = array();
// Get current product categories via product_id
$current_product_tag_ids = wc_get_product_term_ids( $product_id, 'supplier' );
// Loop through cart items checking for product categories
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Get product categories from product in cart via cart item product id
$in_cart_product_tag_ids = array_merge( $in_cart_product_tag_ids, wc_get_product_term_ids( $cart_item['product_id'], 'product_cat' ) );
}
// Removes duplicate values
$in_cart_product_tag_ids = array_unique( $in_cart_product_tag_ids, SORT_NUMERIC );
// Compare
$compare = array_diff( $current_product_tag_ids, $in_cart_product_tag_ids );
// Result is NOT empty
if ( !empty ( $compare ) ) {
wc_add_notice( 'This product is with a different supplier. Please only order from 1 supplier at a time.', 'error' );
$passed = false;
}
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
I'm not trying to limit it to 1 product per supplier, I'm trying to restrict them so they can only order products from 1 supplier per order. Once they've added a product to their basket from the supplier "Supplier 1" for example, they then won't be able to add a product from any other supplier other than "Supplier 1".
I made a previous post trying to use categories instead of a custom taxonomy but I need them to be separate so we're not limited on categories. This post can be found here: One Category Per Order Woocomerce
The following will allow adding to cart items from one "supplier" only:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_supplier_by_order', 10, 2 );
function only_one_supplier_by_order( $passed, $product_id ) {
if ( WC()->cart->is_empty() )
return $passed;
$taxonomy = 'supplier';
$term_ids = wp_get_post_terms( $product_id, $taxonomy, ['fields' => 'ids']);
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if( ! has_term( $term_ids, $taxonomy, $cart_item['product_id'] ) ) {
// Displaying a custom notice
wc_add_notice( __('This product is with a different supplier. Please only order from 1 supplier at a time.'), 'error' );
return false; // Avoid add to cart
}
}
return $passed;
}
Now to be sure that customer can't checkout with different suppliers in cart, you can add this:
// To be sure (avoiding checkout)
add_action( 'woocommerce_check_cart_items', 'only_one_supplier_by_order_check' );
function only_one_supplier_by_order_check() {
$taxonomy = 'supplier';
$term_names = []; // Initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$terms = wp_get_post_terms( $cart_item['product_id'], $taxonomy );
if( ! empty($terms) ) {
$term = reset($terms);
$term_names[$term->term_id] = $term->name;
}
}
// Allow only one supplier in cart (avoid checkout for more than one
if( count( $term_names ) > 1 ) {
// Displaying a custom notice
wc_add_notice( __('Only items from one supplier at the time are allowed in cart'), 'error' );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
这篇关于仅通过一种自定义分类法限制购物车中的WooCommerce产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!