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?
add_command
is supposed to work? It's expecting acallable
value the same wayadd_filter
oradd_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