Disable other product categories for a cart item from specific category in Woocommerce(禁用 Woocommerce 中特定类别的购物车项目的其他产品类别)
问题描述
我正在开发一个网上商店,有不同的方面,第一个是普通商店,第二个是夜间啤酒服务.我已经搜索过但找不到我要找的东西;
I'm developing a webshop, with different aspects, the first is a regular shop, and the second is a night beer service. i've searched but can't find what i am looking for;
首先,夜间啤酒服务是一个特定类别,不应与常规商品一起订购(如果类别 'beerservice'
在购物车中,请禁用所有其他类别以添加到购物车).
First the night beer service is a specific category that shouldn't be ordered with the regular items (if category 'beerservice'
is in the cart, disable all other categories to be added to the cart).
此选项也需要反之亦然(其他情况),因此如果将常规商品添加到购物车,则应禁用 'beerservice'
类别以添加到购物车购物车.
This option also needs to work vice-versa (the other wat around), so if a regular item is added to the cart, the category of 'beerservice'
should be disabled to add to the cart.
我正在使用我找到的这个答案代码:
当来自特定产品类别的商品在 Woocommerce 的购物车中时禁用购物
I am using this answer code that I have found:
Disable shopping when an item from a specific product category is in cart in Woocommerce
这部分完成了工作,但只能从同一类别添加多个产品,反之亦然.我已经替换了这段代码
That does the job partially, but it's only impossible to add multiple products from the same category, and is doesn't work vice-vera. I have replaced in this code
我还想添加一条特定于时间的消息,例如超过 TIME 订购的订单将不会送达"(没有研究过这个),但如果不是很麻烦.
I also would like to add a time specific message like, 'Order ordered past TIME won't be delivered'(didn't research this) but if it isn't to much trouble.
推荐答案
此代码将检查父产品类别,因此对于定义的产品类别,它将:
This code will check for parent product categories, so for a defined product category, it will:
- 当定义的类别在购物车中时,避免将其他产品类别添加到购物车.
- 将定义类别中的产品添加到购物车时,它将从其他产品类别中删除购物车项目.
改进的代码:
// Custom conditional function that checks for parent product categories
function has_parent_term( $product_id ) {
// HERE set your targeted product category SLUG
$category_slug = 'beerservice'; // <==== <==== <==== <==== <==== <==== <====
// Convert category term slug to term id
$category_id = get_term_by('slug', $category_slug, 'product_cat')->term_id;
$parent_term_ids = array(); // Initializing
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
} else {
$parent_term_ids[] = $term->term_id;
}
}
return in_array( $category_id, $parent_term_ids );
}
// Avoid add to cart others product categories when "beerservice" is in cart
add_filter( 'woocommerce_add_to_cart_validation', 'specific_category_avoid_add_to_cart_others', 20, 3 );
function specific_category_avoid_add_to_cart_others( $passed, $product_id, $quantity) {
if( WC()->cart->is_empty() || has_parent_term( $product_id ) ) {
return $passed;
}
foreach( WC()->cart->get_cart() as $cart_item ){
if( has_parent_term( $cart_item['product_id'] ) ) {
wc_add_notice( __('Alert message 1 (avoid add to cart)', 'woocommerce' ), 'error' ); // Display a custom error notice
return false; // Avoid add to cart
}
}
return $passed;
}
// Remove other items when our specific product is added to cart
add_action( 'woocommerce_add_to_cart', 'conditionally_remove_other_products', 20, 4 );
function conditionally_remove_other_products ( $cart_item_key, $product_id, $quantity, $variation_id ){
if( has_parent_term( $product_id ) ) {
foreach( WC()->cart->get_cart() as $item_key => $cart_item ){
if( ! has_parent_term( $cart_item['product_id'] ) ) {
WC()->cart->remove_cart_item( $item_key );
wc_add_notice( __('Alert message 2 (Item removed from cart)', 'woocommerce' ), 'error' ); // Display a custom error notice
}
}
}
}
代码位于您的活动子主题(活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (active theme). Tested and works.
相关:当 Woocommerce 的购物车中有特定产品类别的商品时禁用购物
这篇关于禁用 Woocommerce 中特定类别的购物车项目的其他产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:禁用 Woocommerce 中特定类别的购物车项目的其他产
基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01