I’m using WooCommerce for a restaurant website and PiWebsolution to select the delivery day. I’m having trouble setting up different minimum order prices based on whether the delivery date is on a weekday or weekend.
Here’s what I need:
-
If the customer selects a delivery date on a weekday, the order must be over $130. If the order is less than $130, the checkout form should show an error, and the submit button will be disabled.
-
If the customer selects a delivery date on the weekend, the order must be over $200. If the order is less than $200, the checkout form should show an error, and the submit button will be disabled.
Can anyone help me achieve this?
Code I’m currently using:
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount_based_on_delivery_day' );
function required_min_cart_subtotal_amount_based_on_delivery_day() {
// Set different minimum amounts for weekdays and weekends
$weekday_minimum = 130; // Minimum amount for weekdays
$weekend_minimum = 200; // Minimum amount for weekends
// Get the delivery date from the 'pi_delivery_date' field
if ( isset( $_POST['pi_delivery_date'] ) ) {
$delivery_date = sanitize_text_field( $_POST['pi_delivery_date'] ); // Get the delivery date from POST
// Check if the delivery date exists
if ( $delivery_date ) {
// Convert the delivery date to day of the week
$delivery_day = date( 'w', strtotime( $delivery_date ) );
// Check the delivery day and assign the corresponding minimum amount
$minimum_amount = ( $delivery_day == 0 || $delivery_day == 6 ) ? $weekend_minimum : $weekday_minimum;
// Get the cart subtotal (before tax and shipping)
$cart_subtotal = WC()->cart->subtotal;
// Add error notice if the total is less than the minimum amount
if ( $cart_subtotal < $minimum_amount ) {
// Display error notice
wc_add_notice(
sprintf(
__(
'<strong>A minimum total purchase amount of %s is required for the selected delivery day.</strong>',
'woocommerce'
),
wc_price( $minimum_amount )
),
'error'
);
}
} else {
// If there is no delivery date, display an error message asking the user to select one
wc_add_notice( __( 'Please select a delivery date.', 'woocommerce' ), 'error' );
}
}
}
Issue:
The code works fine for weekdays.
When I select weekends and try to order below $200 (specifically under $150), I get the error message: “A minimum total purchase amount of $150.00 is required for the selected delivery day.”
However, when I order between $150 and $200 (for example, $170), no error is shown, and I’m able to proceed with the order.
Can anyone explain why this happens and how to fix it please?