0

I want to create product using controller so how would i hit the controller using shell_exec.

How would i do that ?

My controller file is

<?php
namespace Mageplaza\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
private $scopeConfig;
protected $_productCollectionFactory;
private $productAction;
protected $_dir;
protected $productFactory;
protected $productRepository;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Filesystem\DirectoryList $dir,
    \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Framework\View\Result\PageFactory $pageFactory
    )
{
    $this->productFactory = $productFactory;
    $this->_dir = $dir;
    $this->productRepository = $productRepository;
    $this->_pageFactory = $pageFactory;
    parent::__construct($context);
}

public function execute()
{
  try {
    $product = $this->productFactory->create();
    $product->setSku('sku2');
    $product->setName('Product Name2');
    $product->setDescription("Product Description1");
    $product->setShortDescription("Product Short Description1");
    $product->setWebsiteIds([1]);
    $categories = ["3"]; //create an array of categories which you want to set for the product
    $product->setCategoryIds($categories);
    $product->setWeight(10);
    $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
    $product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH); // To make product visible in both catalog,search
    $product->setPrice("100");
    $product->setAttributeSetId(4); // Attribute set for products
    $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
    $product->setUrlKey("Product URL Key");
    $product->setStockData(
        array(
            'use_config_manage_stock' => 0,
            'manage_stock' => 1,
            'is_in_stock' => 1,
            'qty' => 1
        )
    );
    $product = $this->productRepository->save($product);
    $product->save();
    } catch (Exception $e){
      echo $e->getMessage();
    }
}
}

Actually i have text file( .txt ) which have multiple fields which is used to create product so on hitting the controller directly it might be an issue ( ex. timeout or any other ) because some months ago ( i have worked on wordpress so there will be an timeout issue so there i have used shell and it solves that problem ) so, here i think it will create same issue while creating these product so i want to hit that using shell so the process is run on terminal.

6
  • Why do you wanna hit it from shell. Please provide complete use case. Sep 3, 2020 at 7:37
  • Updated my use case in question please check @AbdulPathan
    – Prits
    Sep 3, 2020 at 8:03
  • I assume there could be more than 1 product in your file which is making you think of timeout issue, which is correct. I would prefer to create a cron job which would read the file & create products. Sep 3, 2020 at 8:12
  • I also thought about that but cron run at particular time on given time and i want to run that function on my button click or on my event and it is not run everyday so with using cron it will run at a given time @AbdulPathan
    – Prits
    Sep 3, 2020 at 8:36
  • Do you upload a file from admin side ? Sep 3, 2020 at 8:48

1 Answer 1

0

Do not use shell_exec, because it's dangerous.

I don't now what you are trying to achieve with your code, but you can create simple PHP script file for example in your root folder: script/productImport.php

<?php

require __DIR__ . './../app/bootstrap.php';

use Magento\Framework\App\Bootstrap;

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

/* @var $indexController \Mageplaza\HelloWorld\Controller\Index\Index */
$indexController = $objectManager->get('Mageplaza\HelloWorld\Controller\Index\Index');

$indexController->execute();

Then you can run your script from CLI by using php scripts/productsImport.php - it will run code inside the execute() function.

4
  • Thanks for your response, I have updated my use case in question please check @dudzio Anyways liked your approach to that but i have a question is i have to run php command manually is it a way to hit that from Magento admin.
    – Prits
    Sep 3, 2020 at 8:05
  • Sorry, I'm not completely understing your goal. The above code runs it in CLI, it doesn't use apach/nginx enginge to run the code, so timeouts - other than thos from MySQL will not affect it :)
    – dudzio
    Sep 3, 2020 at 8:13
  • Thanks for your response basically i want to create products from a text file so it has huge no. of data so my question is how do i run that script code from an event like click on a button. Hope you understand @Dudzio
    – Prits
    Sep 3, 2020 at 8:39
  • I have added your code and run the command on terminal but it gives an error Could not open input file: scripts/productsImport.php please check @dudzio
    – Prits
    Sep 3, 2020 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.