vendredi 25 décembre 2020

different custom field for different states in woocommerce

I want to assign delivery date options for selected state which should involve today and tomorrow for the "Victoria" state and tomorrow and day after tomorrow for any other selected state. I tried this code but it does not work and just display second condition for any selected state:

//Shipping Date


// Add custom checkout datepicker field

 
add_action( 'woocommerce_before_order_notes', 'checkout_display_datepicker_custom_field' );
function checkout_display_datepicker_custom_field( $checkout ) {
    
    $field_id = 'my_datepicker';
    $state_code = 'VIC';

    echo '<div id="datepicker-wrapper" style="width:22%;">';

    $today = strtotime('today');
    $tomorrow = strtotime('tomorrow');
    $dayAfterTomorrow = strtotime('+2 days');
    
 
 if( WC()->customer->get_billing_state() === $state_code ){
     
     woocommerce_form_field(  $field_id, array(
        'type'          => 'select',
        'class'         => array('form-row-wide'),
        'label' => __('Delivery Date'),
        'placeholder'   => __('Select Delivery Date'),
        'required' => true, // Or false
        'options'     => array(
            '' => 'Select',
            date(('d F Y'), $today ) => date(('d F Y'), $today ),
            date(('d F Y'), $tomorrow ) => date(('d F Y'), $tomorrow ),
        )));

     echo '<br></div>';
     
        } else {
        
        woocommerce_form_field(  $field_id, array(
        'type'          => 'select',
        'class'         => array('form-row-wide'),
        'label' => __('Delivery Date'),
        'placeholder'   => __('Select Delivery Date'),
        'required' => true, // Or false
        'options'     => array(
            '' => 'Select',
            date(('d F Y'), $tomorrow ) => date(('d F Y'), $tomorrow ),
            date(('d F Y'), $dayAfterTomorrow ) => date(('d F Y'), $dayAfterTomorrow ),
        )));

     echo '<br></div>';
                 

        }     
}

Aucun commentaire:

Enregistrer un commentaire