Вот что мне нужно:
- Если клиент выбирает дату доставки в будний день, сумма заказа должна превышать 130 долларов США. Если сумма заказа меньше 130 долларов США, в форме оформления заказа должна появиться ошибка, а кнопка отправки будет отключена.
- Если клиент выберет дата доставки выходной день, сумма заказа должна превышать 200 долларов США. Если сумма заказа меньше 200 долларов США, в форме оформления заказа должна появиться ошибка, а кнопка отправки будет неактивна.
Код, который я сейчас использую:
Код: Выделить всё
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(
__(
'[b]A minimum total purchase amount of %s is required for the selected delivery day.[/b]',
'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' );
}
}
}
Код отлично работает в будние дни.
Когда я выбираю выходные и пытаюсь сделать заказ на сумму менее 200 долларов США (в частности, менее 150 долларов США), я получаю сообщение об ошибке: «Для выбранного дня доставки требуется минимальная общая сумма покупки в размере 150,00 долларов США».
Однако, когда я заказываю от 150 до 200 долларов США (например, 170 долларов США), ошибка не отображается, и Я могу продолжить оформление заказа.
Кто-нибудь может объяснить, почему это происходит и как это исправить?
Подробнее здесь: https://stackoverflow.com/questions/792 ... kday-vs-we