0

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.

CLI_Controller.php

<?php

namespace WMS\Controllers;

use WMS\Controllers\CLI\CLI_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());
        });
    }
}

Product_Controller.php

<?php

namespace WMS\Controllers\CLI;

use WMS\Controllers\WMSApi\APIService\Items;

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()) {
            $loop->the_post();
            global $product;

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

                print_r($item);
            } else {
                echo 'Product missing SKU number';
            }
        }

        $items->import($item);
        wp_reset_query();
    }
}

So when I run this command its giving this error

PHP Fatal error: Uncaught Error: Class 'WMS\Controllers\WMSApi\APIService\Items' not found in /var/www/frontend/wp-content/plugins/wordpress-woocommerce-plugin/src/Controllers/CLI/Product_Controller.php:16 Stack trace: #0 /var/www/frontend/wp-content/plugins/wordpress-woocommerce-plugin/src/Controllers/CLI_Controller.php(14): WMS\Controllers\CLI\CLI_Products->execute()

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?

4
  • You’re not importing a file that contains that missing class. Are you using an autoloader? Sep 4, 2021 at 12:34
  • Its including in my plugin file, there are autoloading, I can access for the class and namespaces in normal way but not inside the CLI so its pretty weird. Sep 4, 2021 at 12:39
  • are you sure that's how add_command is supposed to work? It's expecting a callable value the same way add_filter or add_action works but that's not what you're giving it, you're not giving it something to call, it's directly calling them which is very wrong, this usage is incorrect. Also you should never modify other plugins to make changes, all those changes will be destroyed when the plugin gets updated
    – Tom J Nowell
    Sep 4, 2021 at 13:19
  • Mabey I did it wrong, I'm build a custom plugin, and inside here I need a CLI command to work with, the problem is I can't get access to the code normally but not inside the CLI I have created. So I need a way to get access to my own code classes inside a CLI command as i can share with the rest of my plugin. Sep 4, 2021 at 13:26

1 Answer 1

0

I found the issue, and I discover I missing a "vendor" folder in my root of the plugin and inside composer.json i missing a auto-loader of classes.

So what there fix my issue is update my composer.json file and then run a composer install command after inside my plugin folder.

{
    ...
    "require": {
        "php": ">=7.4",
        "guzzlehttp/guzzle": "^7.0"
    },
    "autoload": {
        "psr-4": {
            "WMS\\": "src"
        }
    }
}

So my CLI code work as default, its was a configuration error from my side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.