add_action('woocommerce_checkout_update_order_review', 'set_session_shipping_method');
function set_session_shipping_method($posted_data) {
$data = [];
parse_str($posted_data, $data);
if (isset($data['shipping_method'][0])) {
WC()->session->set('custom_shipping_method', $data['shipping_method'][0]);
}
}
add_action('woocommerce_after_order_notes', 'custom_shipping_block');
function custom_shipping_block() {
wc_get_template('checkout/my-custom-shipping.php');
}
add_action('woocommerce_checkout_update_order_review', 'refresh_session_on_checkout');
function refresh_session_on_checkout() {
WC()->session->set('shipping_method', null);
WC()->cart->calculate_totals();
}
add_action('woocommerce_checkout_create_order', 'validate_shipping_method_before_create_order');
function validate_shipping_method_before_create_order($order) {
$chosen_shipping = WC()->session->get('shipping_method');
if (empty($chosen_shipping) || $chosen_shipping !== $order->get_shipping_method()) {
wc_add_notice(__('Проблема с доставкой. Пожалуйста, выберите способ доставки снова.'), 'error');
}
}
add_action('woocommerce_checkout_update_order_review', 'log_shipping_method');
function log_shipping_method($data) {
$chosen_shipping = isset($data['shipping_method'][0]) ? $data['shipping_method'][0] : 'none';
error_log('Selected shipping method: ' . $chosen_shipping);
}