0

I want to use WP CLI to delete the WooCommerce orders.

I have 3 arguments ( product_id,start_date,end_date ). How do I check all 3 arguments are passed in command or not ?

How do I do something like this ?

if ( ! empty( ALL THE ARGS ) ) {
      WP_CLI::success( "Success" );
} else {
      WP_CLI::error( "args missing" );
}

Below is my code.

$delete_woo_orders = function( $args,$assoc_args ) {

      WP_CLI::line( $assoc_args['product_id'] );
      WP_CLI::line( $assoc_args['start_date'] );
      WP_CLI::line( $assoc_args['end_date'] );

};
WP_CLI::add_command( 'delete_woo_orders', $delete_woo_orders );

Here is my command : wp delete_woo_orders --product_id=1 --start_date="some_date" end_date="some_date"

Parthavi Patel
  • 739
  • 11
  • 28

1 Answers1

2

You can try below code :-

if( defined( 'WP_CLI' ) && WP_CLI ) {
  function delete_order ( $args, $assoc_args ) {
     global $wpdb; 
     if( $assoc_args['product_id'] && $assoc_args['start_date'] && 
       $assoc_args['end_date'] ){ 
        WP_CLI::success( "all args passed" ); // Success Message
     }else{
        WP_CLI::error( "args missing" );  // Failed Message
     }
  }
  WP_CLI::add_command( 'delete_woo_orders', 'delete_order' );
}

I have little bit modified your code and checked the $assoc_args have value or not and showing a success and error message.

Hims V
  • 310
  • 1
  • 9