更新时间:2023-12-05 09:31:28
您根本不需要运行Ajax.WooCommerce在自动更改默认字段后刷新结帐页面.您需要以下代码:
You do not need to run Ajax at all. WooCommerce refreshes the checkout page after changing the default fields automatically. The following code is what you need:
add_action( 'woocommerce_review_order_before_submit', 'ywp_conditional_delivery_field' );
function ywp_conditional_delivery_field() {
$state_code = 'VIC';
if( $selected_state_code = WC()->customer->get_billing_state() ) {
$country_code = WC()->customer->get_billing_country();
$state_name = WC()->countries->get_states( $country_code )[$state_code];
if( WC()->customer->get_billing_state() === $state_code ) {
$field_id = 'my_datepicker';
$today = strtotime( 'today' );
$tomorrow = strtotime( 'tomorrow' );
$dayAfterTomorrow = strtotime( '+2 days' );
woocommerce_form_field( $field_id, array(
'type' => 'select',
'class' => array( 'form-row-wide' ),
'label' => __( 'Delivery Date For Victoria' ),
'placeholder' => __( 'Select Delivery Date For Victoria' ),
'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 ),
)
) );
} 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 ),
)
));
}
}
}
编辑
以下代码可以正常工作,以在您需要的订单注释前显示发货日期:
The following code works correctly to display the shipping date before the order note that you need it:
add_action( 'woocommerce_before_order_notes', 'ywp_conditional_delivery_field' );
function ywp_conditional_delivery_field() {
$today = date( 'd F Y' ,strtotime( 'today' ) );
$tomorrow = date( 'd F Y' ,strtotime( 'tomorrow' ) );
$dayAfterTomorrow = date( 'd F Y' ,strtotime( '+2 days' ) );
?>
<script>
jQuery(document).ready(function($){
$('#billing_state').on('change',function(){
if($(this).val()=='VIC'){
$('#my_non_VIC_datepicker_field').remove();
$('#order_comments_field').prepend('<p class="form-row form-row-wide validate-required" id="my_VIC_datepicker_field" data-priority=""><label for="my_VIC_datepicker" class="">Delivery Date For Victoria <abbr class="required" title="required">*</abbr></label><span class="woocommerce-input-wrapper"><select name="my_VIC_datepicker" id="my_VIC_datepicker" class="select " data-allow_clear="true" data-placeholder="Select Delivery Date For Victoria"><option value="" selected="selected">Select</option><option value="<?php echo $today; ?>" ><?php echo $today; ?></option><option value="<?php echo $tomorrow; ?>"><?php echo $tomorrow; ?></option></select></span></p>');
} else {
$('#my_VIC_datepicker_field').remove();
$('#order_comments_field').prepend('<p class="form-row form-row-wide validate-required" id="my_non_VIC_datepicker_field" data-priority=""><label for="my_non_VIC_datepicker_field" class="">Delivery Date <abbr class="required" title="required">*</abbr></label><span class="woocommerce-input-wrapper"><select name="my_non_VIC_datepicker" id="my_non_VIC_datepicker" class="select " data-allow_clear="true" data-placeholder="Select Delivery Date"><option value="" selected="selected">Select</option><option value="<?php echo $today; ?>" ><?php echo $today; ?></option><option value="<?php echo $tomorrow; ?>"><?php echo $tomorrow; ?></option><option value="<?php echo $dayAfterTomorrow; ?>"><?php echo $dayAfterTomorrow; ?></option></select></span></p>');
}
});
})
</script>
<?php
}
代码进入活动主题/子主题的 functions.php
中.经过测试,可以正常工作.
Code goes in functions.php
of your active theme/child theme. Tested and works.