我正在尝试将最低订单金额设置为 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' );
}
}
谢谢亲切的问候, 迷宫
我感受到你的痛苦:)但是一旦你看到完成的代码,下次你将能够处理任何“条件逻辑”功能。
这应该有效:
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'
);
}
}
}