0

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();
    }
}
1
  • You have set areacode in your construct. Why did you comment that code? please uncomment it and check. – Jaimin Sutariya Jan 29 '18 at 17:50
1
use Magento\Framework\App\State;

protected $_appState;


public function __construct(
    ...
    State $appState
    ....
) {
    ....
    $this->_appState = $appState;
    ....
}

Now in your code set area which want (i.e frontend or adminhtml) for example,

  $this->_appState->setAreaCode('adminhtml');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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