I just tried to add a "Delivery Date" to WooCommerce checkout process based on this and this threads.
This is the related code to print delivery date on order meta and view it on Thank you page and also admin order page:
//Shipping (Delivery) 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';
echo '<div id="datepicker-wrapper">';
$today = strtotime('today');
$tomorrow = strtotime('tomorrow');
$dayAfterTomorrow = strtotime('+2 days');
woocommerce_form_field( $field_id, array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Choose a date'),
'placeholder' => __('Select delivery date'),
'required' => true, // Or false
'options' => array(
'' => 'Select',
date( 'yyyy-mm-dd', $today ) => date( get_option('date_format'), $today ),
date( 'yyyy-mm-dd', $tomorrow ) => date( get_option('date_format'), $tomorrow ),
date( 'yyyy-mm-dd', $dayAfterTomorrow ) => date( get_option('date_format'), $dayAfterTomorrow ),
)));
echo '<br></div>';
}
// Save field
add_action( 'woocommerce_checkout_create_order', 'save_datepicker_custom_field_value', 10, 2 );
function save_datepicker_custom_field_value( $order, $data ){
$field_id = 'my_datepicker';
$meta_key = '_'.$field_id;
if ( isset($_POST[$field_id]) && ! empty($_POST[$field_id]) ) {
$date = esc_attr($_POST[$field_id]);
$order->update_meta_data( $meta_key, $date ); // Save date as order meta data
$note = sprintf(__("Chosen date for Thank you page: %s.", "woocommerce"), $date );
$note = isset($data['order_comments']) && ! empty($data['order_comments']) ? $data['order_comments'] . '. ' . $note : $note;
// Save date on customer order note
$order->set_customer_note( $note );
}
}
It prints choosed date but in a wrong format like: 20202020-1212-2424
How can I modify this bug?
You're using the wrong format on your date() functions.
Have a look at date/time format docs.
In your case, yyyy-mm-dd:
y A two digit representation of a year
m Numeric representation of a month, with leading zeros
d Day of the month, 2 digits with leading zeros
Hence, 20202020-1212-2424.
You probably meant 2020-12-24, which should be Y-m-d:
Y A full numeric representation of a year, 4 digits
Related
I have creatd a website for a restaurant delivery service using WordPress and WooCommerce.
Basically when the customer is
checking out, he/she can choose when they want the food to be delivered. I want the select box to contain time intervals of 15 mins
ranging from the current time until the close time.
The restaurant is open for deliveries between 11:30 to 22:00. The first option I
want is to be "As soon as possible", and then the next option is an hour later (rounded to nearest 15mins), and then 15 mins each time.
So basically something like this:
(Suppose current time is 13:55)
As soon as possible
15:00
15:15
15:30
15:45
...and so on until close time (23:00)
Here is what I have tried so far but it only displayed whole time range. What should I do to disable past hours?
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'delivery_time', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => ( '<span class="gew-lz">Desired Delivery Time</span>'),
'options' => array(
'sofort' => 'As soon as possible',
'12:00' => '12:00',
'12:30' => '12:30',
'13:00' => '13:00',
'13:30' => '13:30',
'14:00' => '14:00',
'17:30' => '17:30',
'18:00' => '18:00',
'18:30' => '18:30',
'19:00' => '19:00',
'19:30' => '19:30',
'20:00' => '20:00',
'20:30' => '20:30',
'21:00' => '21:00',
'21:30' => '21:30',
'22:00' => '22:00',
)
),
$checkout->get_value( 'delivery_time' ));
}
All I want to disable/deactive all the past hours. When displaying on front end checkout page.
Your help will be highly appreciated.
Use WordPress current_time() function which retrieves the current time based on specified type.
From that point on you can further customize your code to suit your needs, so you get:
function action_woocommerce_before_order_notes( $checkout ) {
// Open and close time
$open_time = strtotime('11:30');
$close_time = strtotime('22:00');
// Current time
$current_time = current_time( 'timestamp' );
// Closed
if( $current_time > $close_time || $current_time <= $open_time ) {
// Default value
$options['closed'] = __( 'Closed', 'woocommerce');
} else {
// Default value
$options[''] = __( 'As soon as possible', 'woocommerce');
// As soon as possible
$asa_possible = strtotime( '+1 hour', $current_time );
// Round to next 15 minutes (15 * 60 seconds)
$asa_possible = ceil( $asa_possible / ( 15 * 60 ) ) * ( 15 * 60);
// Add a new option every 15 minutes
while( $asa_possible <= $close_time && $asa_possible >= $open_time ) {
$value = date( 'H:i', $asa_possible );
$options[$value] = $value;
// Add 15 minutes
$asa_possible = strtotime( '+15 minutes', $asa_possible );
}
}
// Add field
woocommerce_form_field( 'delivery_time', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => __('Desired delivery time', 'woocommerce' ),
'options' => $options,
), $checkout->get_value( 'delivery_time' ));
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
I am working on my WordPress / Woocommerce website.
How can I add a constant value, which would always be the first value in the drop down list which consists of times (hour:minute). This constant value at the top should say "As soon as possible".
Here is the code which generates the form itself:
public function time_select( $checkout ) {
echo '<div id="local-pickup-time-select"><h2>' . __( 'Delivery Time', $this->plugin_slug ) . '</h2>';
woocommerce_form_field( 'local_pickup_time_select', array(
'type' => 'select',
'class' => array( 'local-pickup-time-select-field form-row-wide' ),
'label' => __( 'Delivery Time', $this->plugin_slug ),
'required' => true,
'options' => self::create_hour_options()
), $checkout->get_value( 'local_pickup_time_select' ));
self::create_hour_options();
echo '</div>';
}
public function pickup_time_select_translatable( $value ) {
$value = preg_replace('/(\d)_(\d)/','$1:$2', $value);
$value = explode('_', $value);
$return = __( $value[0], $this->plugin_slug ). ' ' .$value[1];
return $return;
}
public function create_hour_options() {
// Make sure we have a time zone set
$offset = get_option( 'gmt_offset' );
$timezone_setting = get_option( 'timezone_string' );
if ( $timezone_setting ) {
date_default_timezone_set( get_option( 'timezone_string', 'America/New_York' ) );
}
else {
$timezone = timezone_name_from_abbr( null, $offset * 3600, true );
if( $timezone === false ) $timezone = timezone_name_from_abbr( null, $offset * 3600, false );
date_default_timezone_set( $timezone );
}
// Get days closed textarea from settings, explode into an array
$closing_days_raw = trim( get_option( 'local_pickup_hours_closings' ) );
$closing_days = explode( "\n", $closing_days_raw );
$closing_days = array_filter( $closing_days, 'trim' );
// Get delay, interval, and number of days ahead settings
$delay_minutes = get_option( 'local_pickup_delay_minutes', 60 );
$interval = get_option( 'local_pickup_hours_interval', 30 );
$num_days_allowed = get_option( 'local_pickup_days_ahead', 1 );
// Setup time variables for calculations
$today_name = strtolower( date( 'l' ) );
$today_date = date( 'm/d/Y' );
// Create an empty array for our dates
$pickup_options = array();
//Translateble days
__( 'Monday', $this->plugin_slug );
__( 'Tuesday', $this->plugin_slug );
__( 'Wednesday', $this->plugin_slug );
__( 'Thursday', $this->plugin_slug );
__( 'Friday', $this->plugin_slug );
__( 'Saturday', $this->plugin_slug );
__( 'Sunday', $this->plugin_slug );
// Add empty option
$pickup_options[''] = __( 'Select time', $this->plugin_slug );
// Loop through all days ahead and add the pickup time options to the array
for ( $i = 0; $i < $num_days_allowed; $i++ ) {
// Get the date of current iteration
$current_day_name = date( 'l', strtotime( "+$i days" ) );
$current_day_name_lower = strtolower( $current_day_name );
// Get the day's opening and closing times
$open_time = get_option( 'local_pickup_hours_' . $current_day_name_lower . '_start', '10:00' );
$close_time = get_option( 'local_pickup_hours_' . $current_day_name_lower . '_end', '19:00' );
// Today
$tStart = strtotime( $open_time );
$tEnd = strtotime( $close_time );
$tNow = $tStart;
$current_time = time();
// Date format based on user settings
$date_format = get_option('time_format');
$date_format_key = preg_replace("/[^\w]+/", "_", $date_format);
// If Closed today or today's pickup times are over, don't allow a pickup option
if ( ( in_array( $today_date, $closing_days ) || ( $current_time >= $tEnd ) ) && $num_days_allowed == 1 ) {
// Set drop down text to let user know store is closed
$pickup_options['closed_today'] = __( 'Closed today, please check back tomorrow!', $this->plugin_slug );
// Hide Order Review so user doesn't order anything today
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
}
else {
// Create array of time options to return to woocommerce_form_field
// Today
if ( $i == 0) {
// Check if it's not too late for pickup
if ( $current_time < $tEnd ) {
// Fix tNow if is pickup possible today
if ( $i == 0 ) {
$todayStart = $tStart;
$delayStart = strtotime("+$delay_minutes minutes", $current_time);
while ( $todayStart <= $delayStart ) {
$todayStart = strtotime("+$interval minutes", $todayStart);
}
$tNow = $todayStart;
}
while ( $tNow <= $tEnd ) {
$day_name = __( 'Today', $this->plugin_slug );
$option_key = $current_day_name . date( $date_format_key, $tNow );
$option_value = $day_name . ' ' . date( $date_format, $tNow );
$pickup_options[$option_key] = $option_value;
$tNow = strtotime( "+$interval minutes", $tNow );
}
}
// Other days
} else {
if ( !empty($open_time) && !empty($close_time )) {
while ( $tNow <= $tEnd ) {
$day_name = __( $current_day_name, $this->plugin_slug );
$option_key = $current_day_name . date( $date_format_key, $tNow );
$option_value = $day_name . ' ' . date( $date_format, $tNow );
$pickup_options[$option_key] = $option_value;
$tNow = strtotime( "+$interval minutes", $tNow );
}
}
}
}
} // end for loop
if ( count($pickup_options) == 1) {
// Set drop down text to let user know store is closed
$pickup_options['closed_today'] = __( 'Closed today, please check back tomorrow!', $this->plugin_slug );
// Hide Order Review so user doesn't order anything today
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
}
return $pickup_options;
}
The final result would look something like this Drop-down list:
As soon as possible
Tuesday 7:30 pm
Tuesday 8:00 pm
Tuesday 8:30 pm
There is many ways to do it depending if the key value you want for "As soon as possible" choice has to be dynamic. This is done in your first function with array_merge() this way:
public function time_select( $checkout ) {
echo '<div id="local-pickup-time-select"><h2>' . __( 'Delivery Time', $this->plugin_slug ) . '</h2>';
// The default empty value and your translatable sentence to be included
$array_to_insert = array(
'' => __( 'Select your delivery time', $this->plugin_slug ),
'as_soon' => __( 'As soon as possible', $this->plugin_slug )
);
// Getting your calculated hours options array
$options = self::create_hour_options();
// Merging both arrays in one
$options = array_merge( $array_to_insert, $options );
woocommerce_form_field( 'local_pickup_time_select', array(
'type' => 'select',
'class' => array( 'local-pickup-time-select-field form-row-wide' ),
'label' => __( 'Delivery Time', $this->plugin_slug ),
'required' => true,
'options' => $options,
), $checkout->get_value( 'local_pickup_time_select' ));
echo '</div>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. you will get that:
I have noticed in your code that your //Translateble days (monday to sunday) are not set in individual variables or in an array…
Why can't I retrieve the data from a Woocommerce checkout field to update subscription data as the order processes?
I want to update the next renewal date based on the customers' frequency request on checkout. Here is my code:
1) Set up the checkout field:
add_action( 'woocommerce_after_order_notes', 'hgf_custom_checkout_field' );
function hgf_custom_checkout_field( $checkout ) {
global $woocommerce;
woocommerce_form_field( 'frequency', array(
'type' => 'select',
'required' => true,
'class' => array('form-row-wide'),
'label' => __('Change this any time, just email.'),
'options' => array(
'blank' => __( 'Select frequency preference', 'woocommerce' ),
1 => __( 'Every Week', 'woocommerce' ),
2 => __( 'Every Other Week' , 'woocommerce' ),
3 => __( 'Once Per Month' , 'woocommerce' )
)
), $checkout->get_value( 'frequency' ));
echo '</div>';
}
2) Update the order meta. This creates two custom fields on the customer order, as well as updates the billing interval on the subscription:
add_action( 'woocommerce_checkout_update_order_meta', 'hgf_checkout_field_update_order_meta' );
function hgf_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['frequency'] ) ) {
update_post_meta( $order_id, 'frequency', $_POST['frequency'] );
update_post_meta( $order_id, 'billing_interval', $_POST['frequency'] );
}
}
3) Update the renewal date
For the last part, I can update the renewal date with a dummy date of my choosing (for instance: $renewal date = "2018-12-15", but when I try to get it to read the 'billing_interval' field, it reverts to the else default.
add_action( 'woocommerce_checkout_create_subscription', 'hgf_next_renewal' );
function hgf_next_renewal( $subscription, $timezone='site' ) {
$billing_interval = get_post_meta( get_the_ID(), 'billing_interval', true);
if( $billing_interval == 2 ) {
$renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-10-01" ) ) /* date( 'Y-m-d H:i:s', strtotime( "+2 weeks" ) ) */ ;
} if( $billing_interval == 4 ) {
$renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-12-01" ) ) /* date( 'Y-m-d H:i:s', strtotime( "+4 weeks" ) ) */ ;
} else {
$renewal_date = date( 'Y-m-d H:i:s' ) ) /* date( 'Y-m-d H:i:s', strtotime( $renewal_date) ) */ ;
}
$subscription->update_dates( array(
'next_payment' => $renewal_date,
) );
return $subscription;
}
This is all the different ways I have tried:
1) $billing_interval = $subscription->get_billing_interval();
2) $billing_interval = get_post_meta( $subscription->get_ID(), 'billing_interval', true);
3) $billing_interval = get_post_meta( $order_id, 'billing_interval', true);
4) $subscriptions = wcs_get_users_subscriptions( $user_id );
foreach ( $subscriptions as $sub ) {
$billing_interval = get_post_meta( $sub->get_order_number(), '_billing_interval', true);
}
5) $billing_interval = $checkout->get_value( 'frequency' );
Does anyone know why I can't retrieve a value from my checkout field while in the 'woocommerce_checkout_create_subscription' action?
As you are saving your 'billing_interval' custom field in the Order meta data, you can't get this custom field through the subscription object or ID… You need to get first the Parent Order ID.
To get the parent Order ID from the subscription object you need to use the WC_Order method get_parent_id()
add_action( 'woocommerce_checkout_create_subscription', 'hgf_next_renewal' );
function hgf_next_renewal( $subscription, $timezone='site' ) {
// MISSING!: The parent order ID to set below
$order_parent_id = $subscription->get_parent_id();
$billing_interval = get_post_meta( $order_parent_id, 'billing_interval', true);
if( $billing_interval == 2 ) {
$renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-10-01" ) ) /* date( 'Y-m-d H:i:s', strtotime( "+2 weeks" ) ) */ ;
} elseif( $billing_interval == 4 ) {
$renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-12-01" ) ) /* date( 'Y-m-d H:i:s', strtotime( "+4 weeks" ) ) */ ;
} else {
$renewal_date = date( 'Y-m-d H:i:s', strtotime( "2017-09-01" ) ) /* date( 'Y-m-d H:i:s' ) */ ;
}
$subscription->update_dates( array(
'next_payment' => $renewal_date,
) );
return $subscription;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
I haven't tested your code for real, but it doesn't throw any error and should work for you this time.
I am trying to filter (show) Gravity Forms Entries on the front end of my site between two certain dates. One being today (the current present date) and a custom date added within the backend of the website.
So only entries that were submitted between the custom date and the present date.
I have two variables storing these dates:
$new_start_date is the custom date, and
$end_date is the present days' date.
My code is outputting 200 entries but some of which are before the custom start date.
Below is my query code:
<?php
//Get the Form ID to get the entries from and store it within a varibale
$form_id = GFAPI::get_form(2);
// Get Dates
$date_page_id = get_field( 'update_dates_page_link', 'options', false, false );
$start_date = get_field( 'oak_submission_date', $date_page_id ); // date of the last submission date
$end_date = date( 'Y-m-d', time() ); //get today's date
// Convert start_date to match end_date format
$new_start_date = DateTime::createFromFormat( 'd/m/Y', $start_date );
$new_start_date = $new_start_date->format( 'Y-m-d' );
$search_criteria = array(
'status' => 'active',
'start_date' => $new_start_date,
'end_date' => $end_date,
'field_filters' => array(
array(
'key' => '53', // Trust name field
'value' => 'Oak Trust',
),
array(
'key' => '49', // Grant Made field
'value' => 'No',
),
),
);
$sorting = null;
$paging = array( 'offset' => 0, 'page_size' => 200 );
$entries = GFAPI::get_entries( $form_id, $search_criteria, $sorting, $paging );
// Code that outputs to the screen
foreach ( $entries as $entry) :
echo '<li>';
echo 'ID: '. $entry[68] .' : ' . $entry[2] .' : Submission Date: '. $entry[54];
echo '</li>';
endforeach;
GFAPI::get_form(2) doesn't return the Form ID it returns the Form. Try this:
$entries = GFAPI::get_entries( 2, $search_criteria, $sorting, $paging );
I've successfully created the dropdown menu to auto populate the appropriate information from an Advanced Custom Field I created called 'date' on site http://albertson.staging.wpengine.com/seminars/.
Followed along with the instructions here:
http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields
The only issue I'm having is how to display the date in a "pretty" format. You can see that the date is all numbers (20140129) instead of 01/28/2014
To display the date appropriately in the seminar sections above (border red) I use:
<?php if( get_field('date')): ?>
<?php
$date = get_field('date');
// $date = 19881123 (23/11/1988)
// extract Y,M,D
$y = substr($date, 0, 4);
$m = substr($date, 4, 2);
$d = substr($date, 6, 2);
// create UNIX
$time = strtotime("{$d}-{$m}-{$y}");
// format date (November 11th 1988)
echo date('M d', $time);
?>
How do I pass this same information within the Gravity Forms Function I created to get the date to display nicely? Below is my function for Gravity Forms so far.
add_filter('gform_pre_render_4', 'populate_dates');
function populate_dates($form){
foreach($form['fields'] as &$field){
if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-dates') === false)
continue;
// you can add additional parameters here to alter the posts that are retreieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$events = get_posts(array(
'post_type' => 'seminars',
'orderby' => 'date',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $currentdate,
'type' => 'DATE',
)),
'meta_key' => 'date',
));
// update 'Select a Post' to whatever you'd like the instructive option to be
$choices = array(array('text' => 'Select a Date', 'value' => ' '));
foreach($events as $post){
$choices[] = array('text' => $post->date, 'value' => $post->date);
}
$field['choices'] = $choices;
}
return $form;
}
Sounds like you're looking for PHP's date function. We convert the date string to a timestamp via strtotime() and then use date() to format the date as you wish.
$formatted_date = date( 'm/d/Y', strtotime( $post->date ) );
In your code example:
foreach( $events as $post ){
$formatted_date = date( 'm/d/Y', strtotime( $post->date ) );
$choices[] = array('text' => $formatted_date, 'value' => $post->date );
}