PrestaShop Developer Conference
PrestaShop Developer Conference
Paris, France - November 6, 2024
Let's talk code, commerce and open source.

Notice: You are browsing the documentation for PrestaShop 9, which is currently in development.

You might want to read the documentation for the current version, PrestaShop 8. Read the current version of this page

Commands

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

./bin/console

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

If you load and use PrestaShop Core legacy classes such as an ObjectModel within a Command context, you might run into issues. This is a known limitation of the Commands. Removing this limitation is being explored for future PS versions.

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 in the module

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

Setup composer

First you need to setup your composer file, you will find more info about it in Setup composer

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\Command. Let’s create ExportCommand file:

<?php
// your-module/src/Command/ExportCommand.php
namespace YourCompany\YourModule\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_company.your_module.export_command:
        class: YourCompany\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 4.4.

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