我想将此代码添加到我的 woocommerce 商店,以便在选择一种特定的运输方式时显示一条消息。 我在此处找到了此代码段,并在不应显示消息时“排除”了所有其他运输方式。但消息总是出现。 :-(
是否有另一种方法可以在未达到最低数量时排除一种运输方式?
非常感谢:-) 迈克尔
add_action('woocommerce_check_cart_items', 'wc_minimum_required_order_amount'); 函数 wc_minimum_required_order_amount() {
// HERE Your settings
$minimum_amount = 50; // The minimum cart total amount
$shipping_method_id = 'shipping_method_0_local_pickup2'; // The targeted shipping method Id (Ausnahme) local_pickup
$shipping_method_id2 = 'shipping_method_0_1318'; // The targeted shipping method Id (Ausnahme) DHL
$shipping_method_id3 = 'shipping_method_0_1657'; // The targeted shipping method Id (Ausnahme) DHL Express
// Get some variables
$cart_total = (float) WC()->cart->total; // Total cart amount
$chosen_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); // Chosen shipping method rate Ids (array)
// Only when a shipping method has been chosen
if ( ! empty($chosen_methods) ) {
$chosen_method = explode(':', reset($chosen_methods)); // Get the chosen shipping method Id (array)
$chosen_method_id = reset($chosen_method); // Get the chosen shipping method Id
}
// If "Local pickup" shipping method is chosen, exit (no minimum is required)
if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id ) {
return; // exit
}
// If "DHL" shipping method is chosen, exit (no minimum is required)
if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id2 ) {
return; // exit
}
// If "DHL Express" shipping method is chosen, exit (no minimum is required)
if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id3 ) {
return; // exit
}
// Add an error notice is cart total is less than the minimum required
if ( $cart_total < $minimum_amount ) {
wc_add_notice( sprintf(
__("The minimum required order amount is %s (your current order amount is %s).", "woocommerce"), // Text message
wc_price( $minimum_amount ),
wc_price( $cart_total )
), 'error' );
}
}