php - plugin - woocommerce_form_field




WooCommerce-Überschreibt den Abrechnungsstatus und die Postleitzahl für bestehende Checkout-Felder (2)

Dies ist der vollständige Weg für die Abrechnung von Status- und Rechnungs-Postleitzahl-Überschreibungen, wobei der Abrechnungsselektor mit Optionen beibehalten wird.

Hier ist der Code der voll funktionsfähige und getestete Code:

  1. Deaktivierung der Felder für den Abrechnungsstatus und die Postleitzahl
add_filter( 'woocommerce_checkout_fields' , 'partial_unsetting_checkout_fields' );
function partial_unsetting_checkout_fields( $fields ) {
     unset($fields['billing']['billing_state']);
     unset($fields['billing']['billing_postcode']);

     return $fields;
}
  1. Benutzerdefinierte Abrechnungsstatus- und Postleitzahl-Checkout-Felder erneut einfügen
add_filter( 'woocommerce_default_address_fields' , 'art_override_default_address_fields' );
function art_override_default_address_fields( $address_fields ) {

  // @ for state
    $address_fields['billing_state']['type'] = 'select';
    $address_fields['billing_state']['class'] = array('form-row-wide');
    $address_fields['billing_state']['required'] = true;
    $address_fields['billing_state']['label'] = __('State', 'my_theme_slug');
    $address_fields['billing_state']['placeholder'] = __('Enter state', 'my_theme_slug');
    $address_fields['billing_state']['default'] ='Choice 1';
    $address_fields['billing_state']['options'] = array(
        'option_1' => 'Choice 1',
        'option_2' => 'Choice 2',
        'option_3' => 'Choice 3'
    );

    // @ for postcode
    $address_fields['billing_postcode']['type'] = 'text';
    $address_fields['billing_postcode']['class'] = array('form-row-wide');
    $address_fields['billing_postcode']['required'] = true;
    $address_fields['billing_postcode']['label'] = __('Postcode', 'my_theme_slug');
    $address_fields['billing_postcode']['placeholder'] = __('Enter your postcode', 'my_theme_slug');


    return $address_fields;
}

Natürlich geht das auf function.php Datei Ihres aktiven Kinderthemas oder Themas

Offizielle Referenz: WooThemes - Anpassen von Checkout-Feldern mit Aktionen und Filtern

Hinweis zur Eigenschaft 'class'

Es gibt 2 Möglichkeiten, damit umzugehen:

  • Das Feld ist alleine in einer Zeile (Breite 100%), Sie verwenden: 'form-row-wide'
  • Es gibt 2 Felder nebeneinander in derselben Zeile, die Sie verwenden:
    • 'form-row-first' für das erste Feld
    • 'form-row-last' für das zweite Feld

Ich kann nicht den Weg finden, den Rechnungsstatus und die Postleitzahl zu ändern.

Wie kann ich die anderen Teile bestehender Rechnungsfelder wie Rechnungsstatus und Postleitzahl bearbeiten?

Dies ist, was ich in der functions.php Datei in meinem Kindthema habe (ich habe den Code enthalten, der den Abrechnungsteil betrifft):

    <?php
function my_custom_checkout_field( $checkout ) {
  global $wpdb;
  $check_zone  = $wpdb->get_results("select area_name from brick_area where id='".$_SESSION['area']."'",ARRAY_A);

                if(!empty($check_zone)){
                    $check_zoneid = $check_zone['0'];
                }


    woocommerce_form_field( 'my_field_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Delivery Area'),
        'placeholder'   => __('Area'),
        'readonly'      =>'readonly',
        'default'       => $check_zoneid['area_name']
        ), $checkout->get_value( 'my_field_name' ));


    woocommerce_form_field( 'my_expected_date', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'required'      => true,
        'label'         => __('Expected Delivery Date'),
        'placeholder'   => __('Enter expected delivery date.'),
        ), $checkout->get_value( 'my_expected_date' ));

   /*woocommerce_form_field( 'my_expected_time', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'required'      => true,
        'label'         => __('Expected Delivery Time'),
        'placeholder'   => __('Enter expected delivery time.'),
        ), $checkout->get_value( 'my_expected_time' ));*/

    woocommerce_form_field( 'site_contact_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'required'      => true,
        'label'         => __('Site Contact Person Name'),
        'placeholder'   => __('Enter site contact person name.'),
        ), $checkout->get_value( 'site_contact_name' ));    

    woocommerce_form_field( 'site_contact_phone', array(
        'type'          => 'tel',
        'class'         => array('my-field-class form-row-wide'),
        'required'      => true,
        'label'         => __('Site Contact Phone Number'),
        'placeholder'   => __('Enter site contact phone number.'),
        ), $checkout->get_value( 'site_contact_phone' ));       

}
        $fields['billing']['billing_city']['default'] = $_SESSION['cn'];
        add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
        function custom_override_default_address_fields( $address_fields ) {

            // we are changing here billing_state field to required
            $fields['billing']['billing_state']['required'] = true;

            return $address_fields;
        }
        /*$fields['billing']['my_field_name']['default'] = $check_zoneid['area_name'];
        $fields['billing']['my_field_name']['label'] = 'Area';*/

        return $fields;
        }
 ?>      

Vielen Dank


//-------------------------- OVERRIDING BILLING STATE FIELD -------------------------------// 

    <?php
    //Removing previous one by using unset
            add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

            // Our hooked in function - $fields is passed via the filter!
            function custom_override_checkout_fields( $fields ) {
                 unset($fields['billing']['billing_state']);

                 return $fields;
            }


            add_filter( 'woocommerce_default_address_fields' , 'art_override_default_address_fields' );

            function art_override_default_address_fields( $address_fields ) {

              // @ for state
                $address_fields['Billing_State']['type'] = 'text';
                $address_fields['Billing_State']['class'] = array('form-row-wide');
                $address_fields['Billing_State']['required'] = true;
                $address_fields['Billing_State']['label'] = __('State', 'my_theme_slug');
                $address_fields['Billing_State']['placeholder'] = __('Enter state', 'my_theme_slug');


                $address_fields['Billing_State']['default'] =hgfdkda;

                 return $address_fields;
            }
        ?>