最低购物车数量(WooCommerce中的特定产品除外) [英] Minimum cart amount except for specific products in WooCommerce
问题描述
我只允许在我的网站上出售价值不低于15欧元的商品,但希望对一种产品进行例外处理。如果有人知道如何帮助我,我将非常感激。最小订购值的编码如下。有谁知道我可以如何通过产品ID来排除一个产品?
I only permit oders with a minimum value of 15€ on my site, but want to make an exception for one product. I would really appreciated if someone knows how to help me on this. The coding for the minimum order value is below. Anyone know how I can adapt this to exclude one product via a product 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除外:
Updated - The following code will avoid checkout under a defined minimal cart amount except for a define product 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文件中。经过测试并可以正常工作。
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
这篇关于最低购物车数量(WooCommerce中的特定产品除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!