Commands

Since version 1.7 of PrestaShop, everyone have access to the PrestaShop console using the following instruction in a terminal:

./bin/console # or ./app/console for PrestaShop < 1.7.3

Since v1.7.5, you can add and provide your own commands into the PrestaShop console using modules.

Let’s see an example of a really common task when we usually use CRON scripts: you want to export your products into an XML file in order to import them into an another platform (a PIM or an ERP).

You could rely on the webservices, but they are not really easy to configure. This is how you can do it using a PrestaShop command.

Create a command into the module

You need to create the file and register it as a “command”.

Creation of the command

At this moment, the only requirement is that you PHP file needs to be a class that extends Symfony\Component\Console\Command. Let’s create ExportCommand file:

// your-module/src/Command/ExportCommand.php
namespace YourNamespace\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ExportCommand extends Command
{
    protected function configure()
    {
        // The name of the command (the part after "bin/console")
        $this->setName('your-module:export');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // Here your business logic.
        $output->write('Export done!');
    }
}

Registration of the command

Now, in order to make this really simple command available in the console, we register it in the services.yml file:

# your-module/config/services.yml
services:
    your_module_export_command:
        class: YourModule\Command\ExportCommand
        tags:
            - { name: 'console.command' }

The command should be now available using ./bin/console your-module:export.

Learn more about the PrestaShop Console

We use the Symfony Console with nothing specific to PrestaShop.

You can learn everything about this component in their documentation in version 3.4.

To sum up, there is a list of useful links: