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.
Related
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
I would like to call these function to my plugin but I can't do it right, I just want to show the $actions. What is the perfect code to get the result correctly? I want to apply that codes in PHP I home someone can help me to resolve this issue.
This function will show a refund request modal function using the icon when click depends on the order id check this link for the full code https://pastebin.com/WApLdSuw
function wcfmmp_refund_requests_form_html() {
global $WCFM, $WCFMmp, $_POST;
if( isset( $_POST['order_id'] ) && !empty( $_POST['order_id'] ) ) {
$WCFMmp->template->get_template( 'refund/wcfmmp-view-refund-requests-popup.php' );
}
die;
}
public function wcfmmp_refund_orders_actions( $actions, $vendor_id, $order, $the_order ) {
global $WCFM, $WCFMmp;
$order_status = sanitize_title( $the_order->get_status() );
if( in_array( $order_status, apply_filters( 'wcfm_refund_disable_order_status', array( 'failed', 'cancelled', 'refunded', 'pending', 'on-hold', 'request', 'proposal', 'proposal-sent', 'proposal-expired', 'proposal-rejected', 'proposal-canceled', 'proposal-accepted' ) ) ) ) return $actions;
if( !apply_filters( 'wcfm_is_allow_refund_requests', true ) ) return $actions;
if( !apply_filters( 'wcfm_is_allow_paid_order_refund', false ) && ($order->withdraw_status != 'pending') && !in_array( $the_order->get_payment_method(), array( 'wirecard', 'stripe_split' ) ) ) return $actions;
$refund_statuses = explode( ",", $order->refund_statuses );
//if( in_array( 'requested', $refund_statuses ) ) return $actions;
$is_refundeds = explode( ",", $order->is_refundeds );
if( !in_array( 0, $is_refundeds ) ) return $actions;
// Refund Threshold check
$refund_threshold = isset( $WCFMmp->wcfmmp_refund_options['refund_threshold'] ) ? $WCFMmp->wcfmmp_refund_options['refund_threshold'] : '';
if( $refund_threshold ) {
$current_time = strtotime( 'midnight', current_time( 'timestamp' ) );
$date = date( 'Y-m-d', $current_time );
$created_date = date( 'Y-m-d', strtotime($order->created) );
$datetime1 = new DateTime( $date );
$datetime2 = new DateTime( $created_date );
$interval = $datetime2->diff( $datetime1 );
$interval = $interval->format( '%r%a' );
if( ( (int) $interval >= 0 ) && ( (int) $interval > (int) $refund_threshold ) ) return $actions;
}
$actions .= '<a class="wcfmmp_order_refund_request wcfm-action-icon" href="#" data-item="' . $order->item_id . '" data-commission="' . $order->ID . '" data-order="' . $order->order_id . '"><span class="wcfmfa fa-retweet text_tip" data-tip="' . esc_attr__( 'Refund Request', 'wc-multivendor-marketplace' ) . '"></span></a>';
return $actions;
}
For a bookings website I have activated 'Timezones'. The visitor's local time should be displayed. In the booking form everything is working fine. The cart data ($start_time, $end_time) is not being displayed correctly when using the function below.
add_filter( 'woocommerce_get_item_data', 'display_cart_data_wc_bookings', 10, 2 );
function display_cart_data_wc_bookings( $item_data, $cart_item ){
if ( ! empty( $cart_item['booking'] ) ) {
$date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
$time_format = apply_filters( 'woocommerce_bookings_time_format', wc_time_format() );
$start_date = apply_filters( 'woocommerce_bookings_get_start_date_with_time', date_i18n( $date_format, $cart_item['booking']['_start_date'] ) );
$start_time = apply_filters( 'woocommerce_bookings_get_start_date_with_time', date_i18n( $time_format, $cart_item['booking']['_start_date'] ) );
$end_time = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $time_format, $cart_item['booking']['_end_date'] ) );
$persons = $cart_item['booking']['_persons'];
$ID = $cart_item['booking']['_booking_id'];
echo '<dt class="titel datum">' . __('Date', 'woocommerce') . '</dt><dd class="data datum">' . esc_html( $start_date ) . '</dd>';
echo '<dt class="titel tijdvak">' . __('Time', 'woocommerce') . '</dt><dd class="data tijdvak">' . esc_html( $start_time ) . ' - ' . esc_html( $end_time ) . '</dd>';
foreach($persons as $person){
echo '<dt class="titel personen">' . __('Persons', 'woocommerce') . '</dt><dd class="data personen">' . esc_html( $person) . '</dd>';
}
echo '<dt class="titel booking_id">' . __('Booking #', 'woocommerce') . '</dt><dd class="data booking_id">' . esc_html( $ID) . '</dd>';
}
}
The output of $start_time and $end_time should be displayed in the visitor's local time. How can I achieve this?
To enable the user time zone jQuery and ajax are necessary. Here are the following steps:
early enabling user WooCommerce session ( WC Session ),
getting the user browser time zone (time zone from user computer or device),
sending that timezone to php via ajax,
grab that time zone in the user WC Session ,
then use it in your code on date and time php functions as date(), date_i18n(), time()…
Once the user time zone is saved in WC Session, all the related code will be auto disabled (as not needed anymore).
The code:
// Early enable customer WC_Session (if not done)
add_action( 'init', 'wc_session_enabler' );
function wc_session_enabler() {
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
}
// Get the browser user timezone and send it to php (Ajax)
add_action( 'wp_head', 'user_timmezone_js' );
function user_timmezone_js() {
if( ! WC()->session->get('time-zone') ) :
?>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.min.js"></script>
<script type="text/javascript">
jQuery( function($){
if (typeof woocommerce_params === 'undefined')
return false;
var tz = jstz.determine();
$.ajax({
type: "POST",
url: woocommerce_params.ajax_url,
data: ({
'action': 'time_zone',
'timezone': tz.name()
}),
success: function(response) {
console.log('Success: '+response);
}
});
});
</script>
<?php
endif;
}
// function that gets the Ajax data and save it to user WC Session
add_action( 'wp_ajax_time_zone', 'set_session_timezone' );
add_action( 'wp_ajax_nopriv_time_zone', 'set_session_timezone' );
function set_session_timezone() {
if ( isset($_POST['timezone']) && ! empty($_POST['timezone']) ){
WC()->session->set('time-zone', esc_attr($_POST['timezone']) );
echo WC()->session->get('time-zone');
}
die();
}
Code goes in function.php file of your active child theme (or active theme).
Then you will include the following to get and set the time zone:
if( $timezone = WC()->session->get('time-zone') ) {
date_default_timezone_set($timezone);
}
Updated:
Now your code is incorrect when using woocommerce_get_item_data as it's a filter hook and require to return filtered content (not to echo html)…
Try the following instead (untested):
add_filter( 'woocommerce_get_item_data', 'display_cart_data_wc_bookings', 10, 2 );
function display_cart_data_wc_bookings( $item_data, $cart_item ){
if ( isset($cart_item['booking']) && ! empty($cart_item['booking']) && ! is_admin() ) {
// Get and set the user time zone
if( $timezone = WC()->session->get('time-zone') ) {
date_default_timezone_set($timezone);
}
$booking_data = $cart_item['booking'];
$date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
$time_format = apply_filters( 'woocommerce_bookings_time_format', wc_time_format() );
$start_date = apply_filters( 'woocommerce_bookings_get_start_date_with_time', date_i18n( $date_format, $booking_data['_start_date'] ) );
$start_time = apply_filters( 'woocommerce_bookings_get_start_date_with_time', date_i18n( $time_format, $booking_data['_start_date'] ) );
$end_time = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $time_format, $booking_data['_end_date'] ) );
$persons = $booking_data['_persons'];
$booking_id = $booking_data['_booking_id'];
$item_data[] = array(
'key' => __('Date', 'woocommerce'),
'value' => esc_html( $start_date ),
'display' => esc_html( $start_date ),
);
$item_data[] = array(
'key' => __('Time', 'woocommerce'),
'value' => esc_html( $start_time ),
'display' => esc_html( $start_time ),
);
$count = 1;
foreach($persons as $person){
$item_data[] = array(
'key' => __('Person', 'woocommerce') . $count,
'value' => esc_html( $person ),
'display' => esc_html( $person ),
);
$count++;
}
$item_data[] = array(
'key' => __('Booking #', 'woocommerce'),
'value' => esc_html( $booking_id ),
'display' => esc_html( $booking_id ),
);
}
return $item_data;
}
Partially based on: How to detect user's timezone?
Its little bit tricky, what we can do is always in back-end deal with UTC time format and from front-end convert the UTC time to local time
to convert the localtime you can find the help here, hope this way you can archive your goal
#LoicTheAztec: Thanks for the update. Still not working. I've updated the code based on file 'class-wc-booking-cart-manager.php':
add_filter( 'woocommerce_get_item_data', 'display_cart_data_wc_bookings', 10, 2 );
function display_cart_data_wc_bookings( $item_data, $cart_item ){
if ( isset($cart_item['booking']) && ! empty($cart_item['booking']) ) {
// Get and set the user time zone
$booking_data = $cart_item['booking'];
if ( ! empty( $booking_data['_booking_id'] ) ) {
$booking = get_wc_booking( $booking_data['_booking_id'] );
if ( wc_should_convert_timezone( $booking ) ) {
$timezone_data = array(
'name' => get_wc_booking_data_label( 'timezone', $cart_item['data'] ),
'value' => str_replace( '_', ' ', $booking->get_local_timezone() ),
'display' => '',
);
}
}
$date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
$time_format = apply_filters( 'woocommerce_bookings_time_format', wc_time_format() );
$start_date = date_i18n( get_option( 'date_format' ), $booking->get_start( 'view', true ) );
$start_time = date_i18n( get_option( 'time_format' ), $booking->get_start( 'view', true ) );
$end_time = date_i18n( get_option( 'time_format' ), $booking->get_end( 'view', true ) );
$persons = $booking_data['_persons'];
$booking_id = $booking_data['_booking_id'];
$item_data[] = array(
'key' => __('Booking #', 'woocommerce'),
'value' => esc_html( $booking_id ),
'display' => esc_html( $booking_id ),
);
$item_data[] = array(
'key' => __('Date', 'woocommerce'),
'value' => esc_html( $start_date ),
'display' => esc_html( $start_date ),
);
$item_data[] = array(
'key' => __('Start time', 'woocommerce'),
'value' => esc_html( $start_time ),
'display' => esc_html( $start_time ),
);
$item_data[] = array(
'key' => __('End time', 'woocommerce'),
'value' => esc_html( $end_time ),
'display' => esc_html( $end_time ),
);
$count = 1;
foreach($persons as $person){
$item_data[] = array(
'key' => __('Person(s)', 'woocommerce') . $count,
'value' => esc_html( $person ),
'display' => esc_html( $person ),
);
$count++;
}
if ( ! empty( $timezone_data ) ) {
// Add timezone to the end.
$item_data[] = $timezone_data;
}
}
return $item_data;
With this code step 1 to step 4 in your first comment are not necessary. Tested and it works!
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…
I need to change the last modified date and time of all of my blog's posts in WordPress. So far i have....
add_action( 'wp', 'asd' );
function asd()
{
$post_list = get_posts( array(
'post_per_page' => '-1'
) );
foreach ( $post_list as $post ) {
// $posts[] += $post->ID;
$postID = $post->ID;
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
echo $postID . ' ||| ' . $datetime . '<br>';
global $wpdb;
$wpdb->query( "UPDATE `$wpdb->posts` SET `post_modified` = '" . $datetime . "' WHERE ID = " . $postID);
}
}
I get no errors or whatsoever. I am using "echo" for debugging purposes. I have two problems.
I have 6 posts and i get only 5
It seems that no update is happening in the database, for the last modified field
Any help would be appreciated.
you use this code.( paste it in your theme function.php)
add_action('init','change_mypost_date');
function change_mypost_date(){
$args = array(
'post_type' => 'property_listing',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
$current_post = array(
'ID' => get_the_ID(),
'post_modified' => $datetime
);
// Update the post into the database
wp_update_post( $current_post );
}
wp_reset_postdata();
}
}
You are looking to update all of your posts' date to current datetime. So, the solution is without requiring any loops:
add_action( 'init', function () {
$hook = 'run_snippet_daily';
$args = array();
if ( ! wp_next_scheduled( $hook, $args ) ) {
wp_schedule_event( time(), 'daily', $hook, $args );
}
} );
add_action( 'run_snippet_daily', function () {
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
global $wpdb;
$wpdb->query( "UPDATE `$wpdb->posts`
SET `post_date`='{$datetime}',
`post_modified` ='{$datetime}'
WHERE `post_type`= 'post' ");
} );