Commit 77ad8f53 authored by VIGHNESH SADAGOPAL's avatar VIGHNESH SADAGOPAL Committed by Omkar Deshpande
Browse files

Issue #3283878 by Vighnesh., gaurav.kapoor, omkar-pd: Drush Commands for product imports

parent a4ebf244
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -5,3 +5,8 @@ services:
  magento.utils:
    class: Drupal\magento\Services\MagentoUtils
    arguments: ['@config.factory', '@database', '@commerce_price.currency_importer', '@entity_type.manager', '@commerce_product.attribute_field_manager', '@logger.factory', '@magento.client', '@commerce_stock.service_manager']
  magento.import:
    class: \Drupal\magento\Commands\ImportProduct
    tags:
      - { name: drush.command }
    arguments: ['@magento.client','@magento.utils']
+104 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\magento\Commands;

use Drush\Commands\DrushCommands;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\magento\Services\MagentoClient;
use Drupal\magento\Services\MagentoUtils;

/**
 * Class ImportProduct.
 *
 * @category Commands
 * @package  Drupal\magento\Commands
 * @author   Vighnesh Sadagopal <vighnesh.sadagopal@qed42.com>
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @link     https://www.drupal.org/project/magento
 */
class ImportProduct extends DrushCommands
{
    /**
     * Magento Client.
     * 
     * @var \Drupal\magento\Client\MagentoClient
     */
    protected $magentoClient;

    /**
     * Magento Utils.
     * 
     * @var \Drupal\magento\Services\MagentoUtils
     */
    protected $magentoUtils;
    
    /**
     * Constructor.
     *
     * @param \Drupal\magento\Services\MagentoClient $magentoClient
     *   Magento Client.
     * @param \Drupal\magento\Services\MagentoUtils  $magentoUtils
     *  Magento Utils.
     * 
     * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
     * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
     * @throws \Drupal\Core\Entity\EntityStorageException
     * 
     * @return \Drupal\magento\Commands\ImportProduct
     */
    public function __construct(
        MagentoClient $magentoClient,
        MagentoUtils $magentoUtils
    ) {
        $this->magentoClient = $magentoClient;
        $this->magentoUtils = $magentoUtils;
    }
    /**
     * Import Magento Product Singly with entering SKU from magento
     * 
     * @param string $sku
     * @param string $options
     * 
     * @command magento:import
     * @aliases magento-import
     * @options arr Will import product in the basis of array with spaces.
     * 
     * @usage drush magento:import <sku>
     * 
     * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
     * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
     * @throws \Drupal\Core\Entity\EntityStorageException
     * 
     * @return string
     */
    public function importProduct($sku , $options = ['arr' => false])
    {
        if($options['arr']) {
            try{
                $this->output->writeln('The Product is getting imported ....');
                $skus = (explode(" ", $sku));
                foreach ($skus as $key => $value){
                    $currencyCode = $this->magentoClient->getCurrencyCode();
                    $product = $this->magentoClient->getProduct($skus[$key]);
                    $this->magentoUtils->createProduct($product, $currencyCode);
                }
                $this->logger()->success('Successfully imported');
            }
            catch (EntityStorageException $e) {
                $this->logger()->error($e->getMessage());
            }
        }
        else{
            try{
                $this->output->writeln('The Product is getting imported ....');
                $currencyCode = $this->magentoClient->getCurrencyCode();
                $product = $this->magentoClient->getProduct($sku);
                $this->magentoUtils->createProduct($product, $currencyCode);
                $this->logger()->success('Successfully Imported');
            }
            catch (EntityStorageException $e) {
                $this->logger()->error($e->getMessage());
            }
        }
    }
}