Deny checkout for specific cart items in Woocommerce based on product categories(根据产品类别拒绝在WooCommerce中结账的特定购物车项目)
本文介绍了根据产品类别拒绝在WooCommerce中结账的特定购物车项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基于Allow checkout only when a product of a mandatory category is in cart,我尝试制作自己的代码样本,以呈现通知并防止结账,如果购物车 仅包含特定类别的产品。它在预防和错误通知方面起作用,但是在添加其他产品时,它仍然拒绝结账。/**
* Renders a notice and prevents checkout if the cart
* only contains products in a specific category
*/
//add_action( 'woocommerce_check_cart_items', 'brown_wc_prevent_checkout_for_category' );
function brown_wc_prevent_checkout_for_category() {
// set the slug of the category for which we disallow checkout
$categories = array('drinks','extra-accompaniments');
foreach ($categories as $category => $value) {
// get the product category
$product_cat = get_term_by( 'slug', $category, 'product_cat' );
// sanity check to prevent fatals if the term doesn't exist
if ( is_wp_error( $product_cat ) ) {
return;
}
// check if this category is the only thing in the cart
if ( brown_wc_is_category_alone_in_cart( $category ) ) {
// render a notice to explain why checkout is blocked
wc_add_notice( sprintf( 'Please choose atleast one bento.', $category ), 'error' );
}
}
}
/**
* Checks if a cart contains exclusively products in a given category
*
* @param string $category the slug of the product category
* @return bool - true if the cart only contains the given category
*/
function brown_wc_is_category_alone_in_cart( $category ) {
//When the cart is empty, remove warning message that I have set for when the snippet takes effect
if (!WC()->cart->is_empty()) {
// All the code
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// if a product is not in our category, bail out since we know the category is not alone
if ( ! has_term( $category, 'product_cat', $cart_item['data']->id ) ) {
return false;
}
}
// if we're here, all items in the cart are in our category
return true;
} else {
return false; // Assume you'd want false here, since the cart is empty
}
}
推荐答案
您的代码中有一些错误会导致它无法成功工作。您的代码和要求与我的previous answer完全不同。
您必须为通知设置所需的按钮URL和文本,如"便当"产品类别链接和按钮名称。
代码如下:
// Conditional function that check if the non mandatory product categories are in all cart items
function has_not_mandatory_category(){
// DEFINE HERE the your non mandatory product categories (Ids, slugs or names)
$categories = array('drinks','extra-accompaniments');
// Iterrating each item in cart and detecting…
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id']; // <== This is the right way (working for product variations too)
if ( ! has_term( $categories, 'product_cat', $product_id ) )
return false;
}
return true;
}
// Display a message and prevent checkout if the non mandatory product categories are not in all cart items
add_action( 'woocommerce_check_cart_items', 'prevent_checkout_display_notice' ); // for cart and checkout
function prevent_checkout_display_notice() {
// DEFINE HERE the return button URL and title
$button_url = '#';
$button_title = 'Go there';
// Display message if the non mandatory product categories are not in all cart items
if ( has_not_mandatory_category() )
wc_add_notice(
sprintf( __( 'Please choose at least one bento. <a class="button" href="%s">%s</a>', 'your_theme_domain'),
$button_url,
$button_title
), 'error'
);
}
代码放在活动子主题(或活动主题)的函数.php文件中。
已测试和工作的…
您应该执行相反的…操作,而不是针对非强制性产品类别
这篇关于根据产品类别拒绝在WooCommerce中结账的特定购物车项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:根据产品类别拒绝在WooCommerce中结账的特定购物车


基础教程推荐
猜你喜欢
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01