在 WooCommerce 购物车中设置全球最低金额,类别餐饮 + 子类别除外
Posted
技术标签:
【中文标题】在 WooCommerce 购物车中设置全球最低金额,类别餐饮 + 子类别除外【英文标题】:Set a global minimum amount in WooCommerce cart except for the category catering + subcategories 【发布时间】:2021-12-21 11:05:52 【问题描述】:我正在尝试将最低订单金额设置为一般 51.50 欧元,除了可以单独添加的类别“餐饮 + 子类别”之外,无论购物车中的金额如何。
到目前为止,我发现了两个代码,它们分别工作得很好。由于错误消息,现在我试图将这两个函数合并为一个函数。 目前由于第一个函数总是有全局错误消息,即使在购物车中找到餐饮产品,通过第二个函数。
/**
* Set a minimum order amount for checkout - works fine
*/
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount()
// Set this variable to specify a minimum order value - 51.50
$minimum = 51.50;
if ( WC()->cart->total < $minimum )
if( is_cart() )
wc_print_notice(
sprintf( '| Ihr Bestellwert muss mindestens %s (inkl.Versandkosten) betragen um eine Bestellung abzugeben. — Ihr aktueller Warenwert liegt bei %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
else
wc_add_notice(
sprintf( 'Ihr Bestellwert muss mindestens %s (inkl.Versandkosten) betragen um eine Bestellung abzugeben. — Ihr aktueller Warenwert liegt bei %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
这是要检查的代码,购物车中是否有餐饮产品,可以正常工作但目前是分开的:
add_action( 'woocommerce_check_cart_items', 'minimum_order_amount_for_subcategories' );
function minimum_order_amount_for_subcategories()
$term_slug = 'catering'; // <== Define the targeted main product category slug
$threshold = 51.50; // <== Define the minimum amount threshold
$taxonomy = 'product_cat'; // WooCommerce product category taxonomy
$subtotal = 0; // Itintializing
$found = false; // Itintializing
// Get the children terms Ids from the main product category term slug
$main_term = get_term_by( 'slug', $term_slug, $taxonomy );
$childen_ids = get_term_children( $main_term->term_id, $taxonomy );
$terms_ids = array_merge( array($main_term->term_id), $childen_ids);
foreach( WC()->cart->get_cart() as $cart_item )
// Check for specific product category children term ids
if ( has_term( $terms_ids, $taxonomy, $cart_item['product_id'] ) )
$found = true; // At least subcategory is found
// Get non discounted subtotal including taxes from subcategories
$subtotal += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
if ( $found && $subtotal < $threshold )
wc_add_notice( sprintf(
__( "Bestellwert muss mindestens %s der Kategorie %s betragen", "woocommerce" ),
wc_price($threshold),
'"<strong>' . ucfirst($term_slug) . '</strong>"'
), 'error' );
谢谢亲切的问候, 迷宫
【问题讨论】:
【参考方案1】:我感觉到你的痛苦 :) 但是一旦你看到完成的代码,下次你就可以处理任何“条件逻辑”功能了。
这应该可行:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount()
// Set this variable to specify a minimum order value - 51.50
$threshold = 51.50;
$term_slug = 'catering';
$taxonomy = 'product_cat'; // WooCommerce product category taxonomy
$subtotal = 0; // Initializing
$found = false; // Initializing
// Get the children terms Ids from the main product category term slug
$main_term = get_term_by( 'slug', $term_slug, $taxonomy );
$children_ids = get_term_children( $main_term->term_id, $taxonomy );
$terms_ids = array_merge( array($main_term->term_id), $children_ids);
foreach( WC()->cart->get_cart() as $cart_item )
// Check for specific product category children term ids
if ( has_term( $terms_ids, $taxonomy, $cart_item['product_id'] ) )
$found = true; // At least subcategory is found
// Get non discounted subtotal including taxes from subcategories
$subtotal += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
if ( $found && $subtotal < $threshold )
if ( is_cart() )
wc_print_notice(
sprintf( '| Ihr Bestellwert muss mindestens %s (inkl.Versandkosten) betragen um eine Bestellung abzugeben. — Ihr aktueller Warenwert liegt bei %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
else
wc_add_notice(
sprintf( 'Ihr Bestellwert muss mindestens %s (inkl.Versandkosten) betragen um eine Bestellung abzugeben. — Ihr aktueller Warenwert liegt bei %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
【讨论】:
以上是关于在 WooCommerce 购物车中设置全球最低金额,类别餐饮 + 子类别除外的主要内容,如果未能解决你的问题,请参考以下文章