wp cli – Working with CLI and missing my namespace classes inside


I’m very new to WordPress development. I hit this error when i developering my own class and namespace to do the life easier to reuse my code on cross of platforms/frameworks.

I have added my CLI command like this

wp product:sync

And when i run my CLI command whitout my class its run as it shut, I can get access to the internel WordPress functions did not working.



namespace WMSControllers;

use WMSControllersCLICLI_Products;

class CLI
    function init()
        add_action('cli_init', function () {
            $product_cli = new CLI_Products();

            WP_CLI::add_command('product:sync', $product_cli->execute());
            WP_CLI::add_command('stock:sync', $product_cli->hello_world());
            WP_CLI::add_command('order:sync', $product_cli->hello_world());



namespace WMSControllersCLI;

use WMSControllersWMSApiAPIServiceItems;

class CLI_Products
    public function hello_world()
        WP_CLI::line('Hello World!');

    function execute()
        $api_items = new Items();

        $args = array(
            'post_type'      => 'product',
            'posts_per_page' => 10

        $loop = new WP_Query($args);
        while ($loop->have_posts()) {
            global $product;

            if ($product->get_sku()) {
                $item = [
                    'sku' => $product->get_sku(),
                    'description' => get_the_title()

            } else {
                echo 'Product missing SKU number';


So when I run this command its giving this error

PHP Fatal error: Uncaught Error: Class
‘WMSControllersWMSApiAPIServiceItems’ not found in
Stack trace:
#0 /var/www/frontend/wp-content/plugins/wordpress-woocommerce-plugin/src/Controllers/CLI_Controller.php(14):

So its look like the main file did not load success, is there a way or work a round to load custom class/namespaces inside my CLI commands?

fuxia 1 year 2021-09-04T07:22:44-05:00 0 Answers 0 views 0

Leave an answer