简体   繁体   中英

Update order status from custom statuses to completed in WooCommerce everyday at 23.00 if meta_key = Delivery Date and meta_value = this day

I am trying to make a Function, that automaticly updates the Post status to Completed if the status is wc-ishoej-afhentning, wc-roskilde-afhent, wc-koege-afhentning, wc-soeborg-afhent or wc-birkeroed-afhent. But it can only change the status, if a metakey(Delivery date) in postmeta is == the meta value of the Delivery date is == The current date.

This is an image of the values from every order in the database数据库中的值 I believe the format is j F, Y

I am really hoping someone could help me.. i have some different codes, which i have been looking at trying to figure it out myself, but i seem to get nowhere.

First code below, this one i found on stackoverflow, but can seem to figure out how to change it into what i need.

add_action( 'admin_init', 'update_order_status_on_monday' );

function update_order_status_on_monday() {

    if ( date( 'D', strtotime( 'now' ) ) === 'Mon' && !get_transient( '_updated_order_status_on_monday' ) ) {

        $processing_orders   = wc_get_orders( $args              = array(
            'numberposts'    => -1,
            'post_status'    => 'wc-processing',
        ) );

        if ( !empty( $processing_orders ) ) {
            foreach ( $processing_orders as $order )
                $order->update_status( 'completed' );
        }
        set_transient( '_updated_order_status_on_monday', true );
    } elseif ( date( 'D', strtotime( 'now' ) ) !== 'Mon' ) {
        delete_transient( '_updated_order_status_on_monday' );
    }
 }

The second code, i one i am currently using to change to the custom statuses, which i have added here below

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_ishoej_order' );
function custom_woocommerce_auto_ishoej_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }
    
    if( get_post_meta($order_id, 'Pickup Location', true ) == "Ishøj" ) {

    $order = wc_get_order( $order_id );
    $order->update_status( 'wc-ishoej-afhentning' );
        
    } elseif ( get_post_meta($order_id, 'Pickup Location', true ) == "Roskilde" ) {

    $order = wc_get_order( $order_id );
    $order->update_status( 'wc-roskilde-afhent' );
        
    } elseif ( get_post_meta($order_id, 'Pickup Location', true ) == "Køge" ) {

    $order = wc_get_order( $order_id );
    $order->update_status( 'wc-koege-afhentning' );
        
    } elseif ( get_post_meta($order_id, 'Pickup Location', true ) == "Søborg" ) {

    $order = wc_get_order( $order_id );
    $order->update_status( 'wc-soeborg-afhent' );
        
    } elseif ( get_post_meta($order_id, 'Pickup Location', true ) == "Birkerød" ) {

    $order = wc_get_order( $order_id );
    $order->update_status( 'wc-birkeroed-afhent' );
    }
}

This is how it should be done. Go through the code carefully. I have added comments for you to understand what every function does.

/*
 * Trigger the code when everything is loaded.
 * */
add_action( 'wp_loaded','start_custom_code' );


/*
 * Set a cron job for daily Midnight.
 * */
function start_custom_code() {
    if ( ! wp_next_scheduled( 'bks_delivery_to_complete' ) ) {
        wp_schedule_event( (strtotime('midnight') + (3600 * 23)) , 'daily', 'bks_delivery_to_complete' );
    }
}

// Plug the cron job with function 'bks_mark_order_complete_if_delivered_today'
add_action( 'bks_delivery_to_complete', 'bks_mark_order_complete_if_delivered_today' );


/*
 * This function selects order with wc-processing status and with orders
 * having meta key 'Delivery Date' set as current date.
 * */
function bks_mark_order_complete_if_delivered_today(){

    $args = array(
        'status'        => array( 'wc-processing'), // Add any other statuses order of which you want.
        'limit'         => -1,
        'delivery_date' => date("d M, Y"), // Use Current date to fetch Orders.
    );

    $orders = wc_get_orders( $args );

    foreach ($orders as $order){
        $order->update_status( 'completed' );
        $order->save();
    }
};

/* This functions add a new key 'delivery_date'
 * which enables user to pass date to check against
 * Meta Data 'Delivery Date'.
 * */
function bks_handle_custom_query_var( $query, $query_vars ) {
    if ( ! empty( $query_vars['delivery_date'] ) ) {
        $query['meta_query'][] = array(
            'key' => 'Delivery Date',
            'value' => esc_attr(date("d M, Y") ),
        );
    }
    return $query;
}
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'bks_handle_custom_query_var', 10, 2 );

Things to keep in mind:-

  • We are using wordpress cron job , and according to the doc WP-Cron does not run constantly as the system cron does; it is only triggered on page load. WP-Cron does not run constantly as the system cron does; it is only triggered on page load. and as the code is set to run at 11 O`Clock every night if somone doesn't visit your website the cron job will not run. And once the date passes, the code will be applicable for other day. Solution to this is to use system crons. [ Google to set system crons intead of wordpress cron, they are more reliable. ]
  • According to your question your meta tag is Delivery Date , it should be strictly followed or else because an error won't select the order and it will be missed. so Deliver Date ❌ (with two spaces) wont work Deliver date ❌ (Small d for date). I guess you understand what I am saying.
  • Date has to strictly follow "d M, Y" format. Which means today is 05 Jun, 2021 not 5 Jun, 2021 . It is a string comparision. If the meta data is set using a code I don't think there could be a problen.
  • I couldn't understand the statuses that you suggested so I have just used processing for now. You can add others in bks_mark_order_complete_if_delivered_today() function.

The code is tested and WORKS.

Install WP CRON plugin to check your cron. You can test by hitting Run Now.

This question is worth looking at when working with wp_cron https://wordpress.stackexchange.com/a/179774/204925

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM