Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

I need to create a custom dropdown field but i need the values to be d WordPress

  • SOLVED

I need to create a custom dropdown field but i need the values to be dynamic.

We need to be able to choose the delivery date but these have to automaticaly change every month for a maximum on 1 year

As soon as possible
Next month
June 2022
July 2022
....
April 2023

Etc



woocommerce_form_field( 'devlivery_date', array(
'type' => 'select',
'label' => $label,
'required' => true,
'options' => array(
'As soon as possible' => __( 'As soon as possible', 'wps' ),
'Next month' => __( 'Next month', 'wps' ),
'June 2022' => __( 'June 2022', 'wps' ),
'July 2022' => __( 'July 2022 ', 'wps' ),
'August 2022' => __( 'August 2022', 'wps' ),
'September 2022' => __( 'September 2022', 'wps' ),
'October 2022' => __( 'October 2022', 'wps' ),
'November 2022' => __( 'November 2022', 'wps' ),
'December 2022' => __( 'December 2022', 'wps' ),
'January 2023' => __( 'January 2023', 'wps' ),
'February 2023' => __( 'February 2023', 'wps' ),
'March 2023' => __( 'March 2023', 'wps' ),
'April 2023' => __( 'April 2023', 'wps' ),
)
),

Answers (1)

2022-04-21

Arnav Joy answers:

Can you please explain more?
Do you want the dropdown to remove months that are passed?
Please give us an example.


Roberto Mas comments:

Yes i want to have alway 1 calendar year starting from the current month except the wording for the 2 first month have to be
As soon as possible
Next month
and then we go with the rest of the remaining 10 moths


Arnav Joy comments:

can you please try this coe
$months = array();
$currentMonth = (int)date('m');

$months['As soon as possible'] = 'As soon as possible';
$months['Next month'] = 'Next month';


for ($x = $currentMonth+2; $x < $currentMonth + 13; $x++) {
$months[ date('F Y', mktime(0, 0, 0, $x, 1)) ] = date('F Y', mktime(0, 0, 0, $x, 1));
}




woocommerce_form_field( 'devlivery_date', array(
'type' => 'select',
'label' => $label,
'required' => true,
'options' => $months
),


Roberto Mas comments:

Yes that works :) Thanks. By any chance do you know how to output these in french ?


Arnav Joy comments:

please replace this line

$months[ date('F Y', mktime(0, 0, 0, $x, 1)) ] = date('F Y', mktime(0, 0, 0, $x, 1));

with this

$months[ date('F Y', mktime(0, 0, 0, $x, 1)) ] = __( date('F Y', mktime(0, 0, 0, $x, 1)), 'wps' ) ;