0

I want to upload a csv file which contains product sku and i want to get product images which are associated with that sku.please help me out.thanks in advance.

1 Answer 1

1

if you want get image product by sku, you could try this way :

first create the custom extension folder Company/Module in your <magento_root_folder>/app/code, so the path will be <magento_root_folder>/app/code/Company/Module

then create registration.php inside Module folder, add this code to registration.php file

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Company_Module',
    __DIR__
);

next steps create folder etc on inside Module folder, the path will to be like this : <magento_root_folder>/app/code/Company/Module/etc, inside etc folder create file module.xml add this code in module.xml :

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_Module" setup_version="1.0.0"/>
</config>

add again folder frontend in etc folder and create again file routes.xml, so the path now will be <magento_root_folder>/app/code/Company/Module/etc/frontend/routes.xml with this code inside routes.xml :

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route frontName="image" id="image">
            <module name="Company_Module"/>
        </route>
    </router>
</config>

now create Controller folder inside Module folder and create again index folder inside Controller folder, so the path will to be like this : <magento_root_folder>/app/code/Company/Module/Controller/Index create new class file inside index folder with Index.php and add this code into Index.php (the path now will be <magento_root_folder>/app/code/Company/Module/Controller/Index/Index.php)

<?php

namespace Company\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }


 public function execute()
 {
    return $this->resultPageFactory->create();
 }

}

create again file Upload.php inside ../Controller/Index folder and then add this code to file

<?php
namespace Company\Module\Controller\Index;

class Upload extends \Magento\Framework\App\Action\Action
{

    protected $_productFactory;
    protected $_storeManager;
    protected $resultPageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_productFactory = $productFactory;
        $this->_storeManager = $storeManager;
        parent::__construct($context);
    }

    public function execute()
    {
        $file =  fopen($_FILES['file']['tmp_name'], "r");
        $header = fgetcsv($file); // get data headers and skip 1st row

        $required_data_fields = 3;

        while ( $row = fgetcsv($file, 3000, ",") ) {

            $data_count = count($row);
            if ($data_count < 1) {
                continue;
            }

            $data = array();
            $data = array_combine($header, $row);
            echo $this->getImagePathFromSku($data['sku'])."<br />";

        }

    }

    public function getImagePathFromSku($sku)
    {
        $product = $this->_productFactory->create()->loadByAttribute('sku', $sku);

        if($product) {
            $mediaurl= $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
            $imagepath = $mediaurl.'catalog/product'.$product->getImage();
            return 'sku : '.$sku.' - image : <img widht="150" height="150" src='.$imagepath.' />';
        } else {
            return;
        }
    }
}

next step create Block\Index folder inside Module folder, and add class file Index.php (the path should be like this : <magento_root_folder>/app/code/Company/Module/Block/Index/Index.php) and add this code inside Index.php

<?php

namespace Company\Module\Block\Index;

class Index extends \Magento\Framework\View\Element\Template
{

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    public function postUrl()
    {
        return $this->getUrl('*/Index/upload');
    }
}

create form for upload csv file, create folder 'view/frontend' inside etc folder, then create layout and templates\index folders the path will to be like these : <magento_root_folder>/app/code/Company/Module/view/frontend/layout and <magento_root_folder>/app/code/Company/Module/view/frontend/templates\index. create layout file image_index_index.xml inside .../view/frontend/layout, add this code :

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceContainer name="content">
            <block class="Company\Module\Block\Index\Index" name="index.index" template="Company_Module::index/index.phtml" />
        </referenceContainer>
    </body>
</page>

add again index.phtml inside ../view/frontend/templates/index/ folder insert with this html code :

<form method="post" action="<?php echo $this->postUrl(); ?>" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="submit" />
</form>

open your cli and go to your magento project root and execute this command

php bin/magento cache:clean

next this command

php bin/magento setup:upgrade

now you can call the script with this url in your browser

http://<your_host>/<your_magento2_folder_site>/image

based your comment it should be :

http://localhost/magento2/image

it will show input file form, you can upload your csv file, I assume your csv format is to be like this :

"sku","name","description","short_description","price","qty"
"test-sku1","Test product name1","Test product description1","Test product short description1","100","20"
"test-sku2","Test product name2","Test product description2","Test product short description2","10","30"

then it will show the image path if the sku product already exist and have image.

Hope this help

24
  • Hi @mrfizh i want to get images on my localhost for testing purpose.i want to run the script as www://localhost/magento2/getimages.php
    – charan
    Commented Oct 4, 2018 at 10:01
  • hi @charan, see my update answer.
    – mrfizh
    Commented Oct 4, 2018 at 10:21
  • [email protected] is ok.but my actual requirement is i want to upload product skus through a csv file and i want to get the product images for those uploaded skus.so i want to run the custom script as www://localhost/magento2/getimages.php.
    – charan
    Commented Oct 4, 2018 at 10:34
  • it's okay, you can see my update answer again. hope it will helps
    – mrfizh
    Commented Oct 4, 2018 at 11:25
  • Hi@mrfizh It is working fine.but it is displaying like this 24-MB01 : /m/b/mb01-blue-0.jpg(SKU and image name).but i want to display the image.pls suggest me the possible solution.
    – charan
    Commented Oct 4, 2018 at 12:54

Your Answer

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

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