Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5554

Create your own console command in PrestaShop 1.7

$
0
0

In this blog, we are going to learn how to create your own console command in PrestaShop. PrestaShop uses the Symfony Console component and provides its own set of commands.

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

./bin/console

You can list all available commands in PrestaShop using the below command:

php bin/console list

If you want to manage your modules via command line, you can write the command in the below format:

php bin/console prestashop:module <action> <module-name> [<file-path>]

Where:

ArgumentDescription
actionAction to execute (Allowed actions: install / uninstall / enable / disable / enable_mobile / disable_mobile / reset / upgrade / configure).
module-nameThe module on which action will be executed
file-pathYML file path for configuration

ie. If you want to disable the newsletter module, you need to write the below command:

php bin/console prestashop:module disable ps_emailsubscription

You will get the below message on the console once this module has been disabled:

Disable action on module ps_emailsubscription succeeded.

Since version 1.7.5, you can create your own console command in PrestaShop using modules. So let’s create a module to list the details of registered customers in the PrestaShop.

Create a console command in the module

Let’s say the module name is “customerconsolecommand” and the module folder structure is:

customerconsolecommand
|
+---config
|       |- index.php
|       |- services.yml
|       
\---src
|    |- index.php
|    |   
|    \---Command
|           |- index.php
|           |- ListCustomersCommand.php
|
|- composer.json
|- customerconsolecommand.php
|- index.php
|- logo.png

Create composer file

First of all, you need to create a composer file ‘composer.json’:

{
    "name": "webkul/customerconsolecommand",
    "license": "AFL-3.0",
    "authors": [
        {
            "name": "Webkul IN",
            "email": "test@webkul.com"
        },
        {
            "name": "Webkul IN"
        }
    ],
    "autoload": {
        "psr-4": {
            "PrestaShop\\Module\\CustomerConsoleCommand\\": "src/"
        },
        "config": {
            "prepend-autoloader": false
        },
        "type": "prestashop-module"
    }
}

Create main module file

Module main file name will be ‘customerconsolecommand.php’:

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

require_once __DIR__.'/vendor/autoload.php';

class CustomerConsoleCommand extends Module
{
    public function __construct()
    {
        $this->name = 'customerconsolecommand';
        $this->author = 'Webkul';
        $this->version = '1.0.0';
        $this->ps_versions_compliancy = array('min' => '1.7.7.0', 'max' => _PS_VERSION_);

        parent::__construct();

        $this->displayName = $this->l('Lists customer via command line');
        $this->description = $this->l('Example of how to create you own console command in PrestaShop');
    }
}

Creation of the command

At this moment, the only requirement is that your PHP file needs to be a class that extends “Symfony\Component\Console\Command”. We need to display output in the table format hence extends “Symfony\Component\Console\Helper\Table” class as well. Extended the “Customer” class to get customer information:

<?php

namespace PrestaShop\Module\CustomerConsoleCommand\Command;

use Customer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ListCustomersCommand extends Command
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        // The name of the command (the part after "bin/console")
        $this->setName('wk:list-customers')
            ->setDescription('List of existing customers');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $customers = Customer::getCustomers();

        if (!$customers) {
            $output->writeln('<comment>No customers available.</comment>');

            return 0;
        }

        $data = array();
        foreach ($customers as $customer) {
            $data[] = array(
                $customer['id_customer'],
                $customer['firstname'],
                $customer['lastname'],
                $customer['email'],
            );
        }

        $table = new Table($output);

        $table->setHeaders(array('id', 'firstname', 'lastname', 'email'))
            ->setRows($data)
            ->render();

        return 0;
    }
}

Registration of the command

In order to make this command available in the console, we register it in the ‘services.yml’ file:

services:
    PrestaShop\Module\CustomerConsoleCommand\Command\ListCustomersCommand:
        class: PrestaShop\Module\CustomerConsoleCommand\Command\ListCustomersCommand
        tags:
            { name: 'console.command' }

Now, install this module in PrestaShop and go to the module folder and run the ‘composer install’ command.

Now, when you run the ‘php bin/console list’ command, the ‘wk:list-customers’ command will be listed:

list-command
List of commands

When we run the ‘wk:list-customers’ command, it lists available customers id, first name, last name, and email in the table format:

php bin/console wk:list-customers
customer-list
Customer lists

That’s all about this blog.

If any issue or doubt please feel free to mention it in the comment section.

I would be happy to help.

Also, you can explore our PrestaShop Development Services & a large range of quality PrestaShop Modules.

For any doubt contact us at support@webkul.com.


Viewing all articles
Browse latest Browse all 5554

Trending Articles