I am geting this error comand "[Magento\Framework\Exception\LocalizedException] Area code is not set "
this is my code which I want to add a product. Tip if a comment " $product->save();" I am not getinng erros and I can see my output.
My code:
<?php
namespace Bachus\BackgroundImport\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Importpr extends Command
{
const NAME_ARGUMENT = "name";
const NAME_OPTION = "option";
/**
* {@inheritdoc}
public function __construct(
\Magento\Framework\App\State $state
) {
$state->setAreaCode('frontend'); // or 'adminhtml', depending on your needs
}
*/
protected function execute(
InputInterface $input,
OutputInterface $output
) {
$name = $input->getArgument(self::NAME_ARGUMENT);
$option = $input->getOption(self::NAME_OPTION);
// parent::__construct();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku($name); // Set your sku here
$product->setName('Sample Simple Product'); // Name of Product
$product->setAttributeSetId(4); // Attribute set id
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setWeight(10); // weight of product
$product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(0); // Tax class id
$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice(100); // price of product
$product->setStockData(
array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => 1,
'qty' => 999999999
)
);
$product->save();
/* */
$output->writeln("Product added sku " . $name);
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName("bachus_cli:importpr");
$this->setDescription("imports-produts");
$this->setDefinition([
new InputArgument(self::NAME_ARGUMENT, InputArgument::OPTIONAL, "Name"),
new InputOption(self::NAME_OPTION, "-a", InputOption::VALUE_NONE, "Option functionality")
]);
parent::configure();
}
}
areacode
in your construct. Why did you comment that code? please uncomment it and check.