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.