Home > Software design >  Get Order Ids in a WooCommerce Plugin
Get Order Ids in a WooCommerce Plugin

Time:08-17

Trying to update order post meta in testmain(), I can update it when I manually assign a order id, but struggling to figure out how to get order ids, where am I going wrong?

<?php
/**
 * Plugin Name: MyTestPlugin
 */

defined('ABSPATH') or die('Bye');
if (in_array( 'woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))){
    if (! wp_next_scheduled('my_test_cron')){
        wp_schedule_event(time(), 'hourly', 'my_test_cron');
    }

    if (! class_exists('MyTest')) :
    class MyTest {
        public function __construct(){
            add_filter('manage_edit-shop_order_columns', array($this, 'my_manage_edit_shop_order_columns'));
            add_action('manage_posts_custom_column', array($this, 'my_manage_posts_custom_column')); 
            add_action('my_test_cron', array($this, 'testmain'));
            $this->pluginPath = dirname(__FILE__);
        }
            
        public function testmain(){
        //  $the_id = '9001'; // <-- ORDER ID
            $the_data = 'Sample Data';
            update_post_meta(get_the_ID(), '_my_test', $the_data);
        }

        public function my_manage_edit_shop_order_columns($col_th){
            return wp_parse_args(array('_my_test' => 'My Test'), $col_th);    
        }
        public function my_manage_posts_custom_column($column_id){
            if($column_id  == '_my_test')
                echo  get_post_meta(get_the_ID(), '_my_test', true);
        }
    }
    $MyTest = new MyTest();
    endif;
}

CodePudding user response:

I was going the wrong way about trying to update the meta data.

Solved.

    public function testmain(){
        $args = array(
            'limit'  => -1,
        );
        $orders = wc_get_orders( $args );
        foreach ($orders as $order){
            $order->update_meta_data('_my_test', 'Sample Data');
            $order->save();
        }
    }
  • Related