0

I have the following code which creates a product programmatically but it uses deprecated functions eg the save

Therefore my question is how do you create a product programmatically without using deprecated functions?

public function __construct (
  \Magento\Catalog\Model\ProductFactory $productFactory
)
{
  $this->productFactory = $productFactory;
}

public function createProduct(){
  $simpleProduct1 = $this->productFactory->create();
  $simpleProduct1->setData('sku', 'Test Simple Product 1');
  $simpleProduct1->setData('name', 'Test Simple Product 1');
  $simpleProduct1->setData('website_ids', array(1)); // product can be found in main website
  $simpleProduct1->setData('attribute_set_id', 4);
  $simpleProduct1->setData('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
  $simpleProduct1->setData('visibility', 4);
  $simpleProduct1->setData('price', 12);
  $simpleProduct1->setData('type_id', 'simple');
  $simpleProduct1->setData('stock_data', array(  // set product quantity
   'use_config_manage_stock' => 0,
   'manage_stock' => 1,
   'is_in_stock' => 1,
   'qty' => 100
  ));
  $simpleProduct1->save();
}
1

Try this:

public function __construct (
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Catalog\Model\ProductRepository $productRepository
)
{
    $this->productFactory = $productFactory;
    $this->productRepository = $productRepository;
}

public function createProduct(){
    $simpleProduct1 = $this->productFactory->create();
    $simpleProduct1->setData('sku', 'Test Simple Product 1');
    $simpleProduct1->setData('name', 'Test Simple Product 1');
    $simpleProduct1->setData('website_ids', array(1)); // product can be found in main website
    $simpleProduct1->setData('attribute_set_id', 4);
    $simpleProduct1->setData('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    $simpleProduct1->setData('visibility', 4);
    $simpleProduct1->setData('price', 12);
    $simpleProduct1->setData('type_id', 'simple');
    $simpleProduct1->setData('stock_data', array(  // set product quantity
        'use_config_manage_stock' => 0,
        'manage_stock' => 1,
        'is_in_stock' => 1,
        'qty' => 100
    ));
    $this->productRepository->save($simpleProduct1);
}
1

I say something like this

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Catalog\Api\Data\ProductInterfaceFactory $productInterfaceFactory,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface
    ) {
        $this->logger = $logger;
        $this->productInterfaceFactory = $productInterfaceFactory;
        $this->productRepositoryInterface = $productRepositoryInterface;
    }

    /**
     * Create random product.
     *
     * @param int $websiteId
     *
     * @return \Magento\Catalog\Model\Product\Interceptor
     */
    public function createProduct($websiteId = 1, $typeId = \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
    {

        $quantity = rand(1, 20);
        $product = $this->productInterfaceFactory
            ->create()
            ->setSku('whatever')
            ->setTypeId($typeId)
            ->setWebsiteIds([$websiteId])
            ->setStockData([
                'is_in_stock' => $quantity > 0 ? 1 : 0,
                'qty'         => $quantity,
            ]);

        try {
            $product = $this->productRepositoryInterface->save($product);
            return $product;
        } catch (\Exception $e) {
            $this->logger->critical($e);
        }
    }

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.