gpt4 book ai didi

php - 在 WooCommerce 中设置除产品标签外的最小订单值

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

我目前有一个 wordpress 网站,为我的 WooCommerce 商店设置了最低订单值,在我的functions.php 中设置,它运行良好。我现在想设置一个产品标签作为此规则的异常(exception),并且没有最低订单值(value)……这可能吗?
我实际上正在使用 Set a minimum order amount in WooCommerce答案代码。
不确定更改为“wc 最小订单量”等插件是否可以解决问题,或者是否可以将其添加到现有代码中?
非常感谢任何建议!

最佳答案

要使此代码对特定产品标签无效,请使用以下内容:

add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {

$minimum_amount = 35; // HERE Set minimum cart total amount
$product_tags = array('Lewis'); // HERE set the product tags (term names, slugs or Ids)
$cart_subtotal = WC()->cart->subtotal; // Total (before taxes and shipping charges)
$tag_found = false;

// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product tag
if( has_term( $product_tags, 'product_tag', $cart_item['product_id'] ) ) {
$tag_found = true;
break;
}
}

// Add an error notice is cart total is less than the minimum required
if( $cart_subtotal < $minimum_amount && ! $tag_found ) {
// Display an error message
wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($minimum_amount) ) . '<strong>', 'error' );
}
}
或者,如果您想从购物车小计使用中排除特定产品标签:
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {

$minimum_amount = 35; // HERE Set minimum cart total amount
$product_tags = array('Disc'); // HERE set the product tags (term names, slugs or Ids)
$cart_subtotal = 0; // Total (before taxes and shipping charges)

// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product tag
if( ! has_term( $product_tags, 'product_tag', $cart_item['product_id'] ) ) {
$cart_subtotal += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
}
}

// Add an error notice is cart total is less than the minimum required
if( $cart_subtotal < $minimum_amount ) {
// Display an error message
wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($minimum_amount) ) . '<strong>', 'error' );
}
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。测试和工作。

关于php - 在 WooCommerce 中设置除产品标签外的最小订单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65384609/

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