php - WooCommerce 中的特定产品除外的最低购物车数量
问题描述
我只允许在我的网站上进行最低价值为 15 欧元的订单,但我想对一种产品进行例外处理。如果有人知道如何帮助我,我将不胜感激。最小订单价值的编码如下。任何人都知道我可以如何调整它以通过产品 ID 排除一个产品?
add_action( 'woocommerce_check_cart_items', 'wc_set_min_total' );
function wc_set_min_total() {
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Setting the minimum cart total
$minimum_cart_total = 15;
$total = WC()->cart->subtotal;
if( $total <= $minimum_cart_total ) {
wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
.'<br />Current cart\'s total: %s %s',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}
解决方案
更新 - 以下代码将避免在定义的最小购物车金额下结帐,但定义的产品 ID 除外:
add_action( 'woocommerce_check_cart_items', 'min_cart_amount' );
function min_cart_amount() {
## ----- Your Settings below ----- ##
$min_amount = 15; // Minimum cart amount
$except_ids = array(37, 53); // Except for this product(s) ID(s)
// Loop though cart items searching for the defined product
foreach( WC()->cart->get_cart() as $cart_item ){
if( in_array( $cart_item['variation_id'], $except_ids )
|| in_array( $cart_item['product_id'], $except_ids )
return; // Exit if the defined product is in cart
}
if( WC()->cart->subtotal < $min_amount ) {
wc_add_notice( sprintf(
__( "<strong>A Minimum of %s is required before checking out.</strong><br>The current cart's total is %s" ),
wc_price( $min_amount ),
wc_price( WC()->cart->subtotal )
), 'error' );
}
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。
推荐阅读
- python - 你能帮忙编辑json吗,我是python的新手。谢谢
- python - 使用 numpy 时的回溯(最近一次通话)
- python - 如何在 Python 中压缩图像?
- php - 使用laravel在mysql中插入多维数组json
- css - 如何覆盖 svg 元素,如
在我的外部 CSS - oauth-2.0 - 如何在 Rust 中使用 google_speech1 将语音发送到文本请求?
- java - 如何在java中显示汉字
- python - 如何为同一组中的每个值分配一个序列号?Python
- c - 我第一次使用链表。希望对如何解决某些问题提出一些批评和建议
- php - getenv() php 7.2 返回 false