gpt4 book ai didi

php - 除 WooCommerce 中的特定产品外,最低购物车金额

转载 作者:行者123 更新时间:2023-12-04 15:56:15 25 4
gpt4 key购买 nike

我只允许在我的网站上订购最低值(value)为 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 文件。测试和工作。

enter image description here

关于php - 除 WooCommerce 中的特定产品外,最低购物车金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51511991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com