Warning: You are browsing the documentation for PrestaShop 1.7, which is outdated.
You might want to read an updated version of this page for the current version, PrestaShop 8. Read the updated version of this page
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.
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.
You need to create the file and register it as a “command”.
First you need to setup your composer file, you will find more info about it in Setup composer
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:
<?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!');
}
}
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
.
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: