1

I'm using magento 2.2.5 , i want to make a virtual product programmatically using UpgradeSchema , but i alws got an error

Upgrading data.. Area code is not set

when i run bin/magento setup:upgrade

here's my UpgradeSchema.php

<?php


namespace Lime\Merchant\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        if (version_compare($context->getVersion(), "1.0.1", "<")) {
          // $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          // $state = $objectManager->get('Magento\Framework\App\State');
          // if(!$state->getAreaCode()) {
            $state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
          }
          $this->createVirtualProduct();
        }
    }

    public function createVirtualProduct()
    {
      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $_product = $objectManager->create('Magento\Catalog\Model\Product');
      $_product->setName('E-Book');
      $_product->setTypeId('virtual');
      $_product->setAttributeSetId(4);
      $_product->setSku('ebook');
      $_product->setWebsiteIds(array(1));
      $_product->setVisibility(4);
      $_product->setPrice(1000);
      $_product->setStockData(array(
          'use_config_manage_stock' => 0, //'Use config settings' checkbox
          'manage_stock' => 0, //manage stock
          'is_in_stock' => 1, //Stock Availability
          'qty' => 100000, //qty
          'tax_class_id' => 0
          )
      );

      $_product->save();
    }
}
1

Try using UpgradeData instead of UpgradeSchema.

Your UpgradeData.php looks like:

<?php
namespace Lime\Merchant\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    private $productFactory;

    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;

    /**
     * UpgradeData constructor.
     *
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
        \Magento\Catalog\Model\ProductFactory $productFactory,
        ProductRepositoryInterface $productRepository
    ) {
        $this->productFactory = $productFactory;
        $this->productRepository = $productRepository;
    }

    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        if ($context->getVersion() && version_compare($context->getVersion(), '1.0.1') < 0 ) {
            $this->createVirtualProduct();
        }

        $setup->endSetup();
    }

    public function createVirtualProduct()
    {
        /** @var \Magento\Catalog\Model\Product $product */
        $product = $this->productFactory->create();
        $product->setName('E-Book');
        $product->setTypeId('virtual');
        $product->setAttributeSetId(4);
        $product->setSku('ebook');
        $product->setWebsiteIds(array(1));
        $product->setVisibility(4);
        $product->setPrice(1000);
        $product->setStockData(array(
                'use_config_manage_stock' => 0, //'Use config settings' checkbox
                'manage_stock' => 0, //manage stock
                'is_in_stock' => 1, //Stock Availability
                'qty' => 100000, //qty
                'tax_class_id' => 0
            )
        );

        $this->productRepository->save($product);
    }
}

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.