WooCommerce Mindest subtotal für den Versand als lokale Abholung
Posted: 16 Apr 2025, 07:37
Ich versuche, für alle Versandmethoden einen minimalen Subtotal für alle Versandmethoden zu setzen. Leider funktioniert es nicht, sondern nimmt den Mindestbestellwert mit der lokalen Abholung der Versandmethode nicht an. Sehen Sie, was falsch ist?
Code: Select all
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart', 'wc_minimum_order_amount' );
/**
* Minimum order subtotal amount for checkout (excl shipping and taxes)
*/
function wc_minimum_order_amount() {
$minimum = 75; // cart->get_subtotal(); // Cart subtotal excl. shipping and taxes
$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 minimun is required)
if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id ) {
return; // exit
}
if ( $subtotal < $minimum ) {
$message = sprintf(
esc_html__('Your current order subtotal is %s. To place your order, you must have a minimum amount of %s (excl. shipping and VAT).', 'woocommerce'),
wc_price( $subtotal ),
wc_price( $minimum )
);
if( is_cart() ) {
wc_print_notice( $message, 'error' );
} else {
wc_add_notice( $message, 'error' );
}
}
}