0

I have created a file uploader on my backend admin. I want to update my products using programmatically by using an csv uploaded.

My controller file is

<?php
namespace Tym17\AdminSample\Controller\Adminhtml\SampleTwo;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Backend\App\Action
{
/**
 * @var PageFactory
 */
 protected $resultPageFactory;
 protected $csv;
/**
 * @var scopeConfig
 * Needed to retrieve config values
 */

/**
 * @param Context $context
 * @param PageFactory $resultPageFactory
 */
public function __construct(
    Context $context,
    PageFactory $resultPageFactory,
    ScopeConfigInterface $scopeConfig,
    \Magento\Framework\File\Csv $csv
) {
    parent::__construct($context);
    $this->resultPageFactory = $resultPageFactory;
    $this->scopeConfig = $scopeConfig;
    $this->csv = $csv;
}

/**
* Index Action*
* @return void
*/
public function execute()
{
    if (!isset($file['tmp_name'])) 
        throw new \Magento\Framework\Exception\LocalizedException(__('Invalid file upload attempt.'));

     $csvData = $this->csv->getData($file['tmp_name']);

     foreach ($csvData as $row => $data) {
         if ($row > 0){
             //Start your work
         }
     }
     die();
}
}

I have follow this post https://firebearstudio.com/blog/how-to-read-a-csv-file-convert-it-into-an-array-in-magento-2.html

Any help is appreciated.

1 Answer 1

1

I have written a small script below and could save a product. This is not recommended to use this script on a production environment as you could be spammed and have thousands of product created if your server allows the access to this route. But at least, it should help you further

<?php

namespace Mbs\ProductCreate\Controller\Index;

use Magento\Backend\App\Action\Context;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\HttpGetActionInterface;

class Index extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface
{
    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    protected $csv;
    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    private $productFactory;

    /**
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        ScopeConfigInterface $scopeConfig,
        \Magento\Framework\File\Csv $csv,
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->scopeConfig = $scopeConfig;
        $this->csv = $csv;
        $this->productFactory = $productFactory;
    }

    /**
     * Index Action*
     * @return void
     */
    public function execute()
    {
        $csvData = [
            ['header_col0', 'header_col1'],
            ['test', 'dummy']
        ];

        foreach ($csvData as $row => $data) {
            if ($row > 0){

                $data['sku'] = 'sku123';
                $data['price'] = 34;
                $data['name'] = 'Product 123';
                $data['attribute_set'] = 3;
                $data['tax_class'] = 0;

                $product = $this->productFactory->create();
                $product->setSku($data['sku']);
                $product->setName($data['name']);
                $product->setAttributeSetId($data['attribute_set']);
                $product->setStatus(Status::STATUS_ENABLED);
                $product->setVisibility(Visibility::VISIBILITY_BOTH);
                $product->setTaxClassId($data['tax_class']);
                $product->setTypeId(Type::DEFAULT_TYPE);
                $product->setPrice($data['price']);
                $product->setStockData(
                    array(
                        'use_config_manage_stock' => 0,
                        'manage_stock' => 1,
                        'is_in_stock' => 1,
                        'qty' => 12
                    )
                );
                $product->save();
            }
        }
    }
}
2
  • Thanks for your response i have see that please help me with this question magento.stackexchange.com/questions/320441/… @Herve
    – Prits
    Aug 21, 2020 at 17:33
  • @prits this is called moving the goal posts. If the answer I gave you resolved the question you asked, you might think of approving or giving feedback rather than asking another question.. Aug 23, 2020 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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