• Меняю поля в форме оформления заказа в зависимости от доставки с помощью фрагментов.

    //Меняем набор полей в зависимости от способа доставки
    add_filter('woocommerce_checkout_fields', 'override_checkout_fields');
    function override_checkout_fields($fields)
    {
        global $woocommerce;
        $chosen_methods = $woocommerce->session->get('chosen_shipping_methods');
        $fields['order']['order_comments']['label'] = 'Комментарии к заказу:';
        $fields['order']['order_comments']['placeholder'] = '';
        unset($fields['billing']['billing_address_2']);
        $fields['billing']['billing_state']["required"] = false;
        if ($chosen_methods[0] !== 'edostavka-package-stock:8:136' && $chosen_methods[0] !== 'rpaefw_post_calc:5') {
            $fields['billing']['billing_postcode']["required"] = false;
            unset($fields["billing"]["billing_postcode"]);
            unset($fields["billing"]["billing_state"]);
            $fields['billing']['billing_city']["required"] = false;
            unset($fields["billing"]["billing_city"]);
            $fields['billing']['billing_address_1']["required"] = false;
            unset($fields["billing"]["billing_address_1"]);
        }
        return $fields;
    }
    //обновляем форму
    add_filter('woocommerce_update_order_review_fragments', 'update_form_billing', 99);
    function update_form_billing($fragments)
    {
        global $woocommerce;
        $checkout = $woocommerce->checkout();
        $fields = $checkout->get_checkout_fields('billing');
        ob_start();
        echo '<div class="woocommerce-billing-fields__field-wrapper">';
        echo '<h3 class="checkout-header__h3 checkout__address">Введите контактные данные получателя</h3>';
        foreach ($fields as $key => $field) {
            if (isset($field['postcode'], $fields[$field['postcode']])) {
                $field['postcode'] = $checkout->get_value($field['postcode']);
            }
            if (isset($field['state'], $fields[$field['state']])) {
                $field['state'] = $checkout->get_value($field['state']);
            }
            if (isset($field['city'], $fields[$field['city']])) {
                $field['city'] = $checkout->get_value($field['city']);
            }
            if (isset($field['address_1'], $fields[$field['address_1']])) {
                $field['address_1'] = $checkout->get_value($field['address_1']);
            }
        }
        echo '<input type="hidden" name="billing_buyer-type" id="billing_buyer-type" value="' . $buyer_type . '" />';
        $art_add_update_form_billing              = ob_get_clean();
        $fragments['.woocommerce-billing-fields'] = $art_add_update_form_billing;
    
        return $fragments;
    }
    //Обновляем поля адреса
    $(document).on('change', 'input[name^=shipping_method]', function() {
        $(document.body).on('updated_checkout updated_shipping_method', function(event, xhr, data) {
            $('.woocommerce-shipping-totals, .subtotal-form__wrapper').addClass('processing').block({
                message: null,
                overlayCSS: {
                    background: '#fff',
                    opacity: 0.6
                }
            });
            $('.woocommerce-billing-fields__field-wrapper').html(xhr.fragments['.woocommerce-billing-fields']);
            $(document).find('select#billing_city').select2();
            $('.woocommerce-shipping-totals, .subtotal-form__wrapper').removeClass('processing').unblock();
        });
    });

    Однако, после загрузки фрагмента поле выбора города становится пустым.
    Как его заново проинициализировать?

    Страница, с которой нужна помощь: [войдите, чтобы увидеть ссылку]

  • Тема «Проинициализировать поле выбора города» закрыта для новых ответов.