2

I don't know how to implement this task. I want to get product name, SKU, and image in magento2. Can you please help me?

2
  • where you want to get those data?
    – Amit Bera
    Aug 27, 2018 at 6:19
  • Hi @ Amit Bera, I have created one php file in the magento2 root folder. This is my required file path localhost/magento2test/example.php Aug 27, 2018 at 6:21

6 Answers 6

5

I think you can fetch all product data using following code. Just put this code in your php file example.php. Hope it works.

 <?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$params =  $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$mediaurl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->load();

foreach ($collection as $product){
     echo 'Name  =  '.$product->getName().'<br>';
     echo 'Sku  =  '.$product->getSku().'<br><br>';
     $imagepath = $mediaurl.'catalog/product'.$product->getImage();
     $img = '<img src='.$imagepath.' />';
     echo $img;
}  

?>
8
  • Hi @ Raj, I want to get and display image also along with name and SKU. Aug 27, 2018 at 7:14
  • @DileepKumar Have you tried $product->getImage(); or $product->getMediaGalleryImages() for displaying image?
    – Raj
    Aug 27, 2018 at 7:17
  • Hi @ Raj, it is displaying URL path but I want to display the image? Aug 27, 2018 at 7:18
  • @DileepKumar Then you can add that url path in <img src="image_url"/> tag.
    – Raj
    Aug 27, 2018 at 7:19
  • Hi @ Raj , I have added this code <img src="<?php $product->getImage(); ?>"> but image not displaying Aug 27, 2018 at 7:38
1
<?php 
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

try
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product_Id = 5;
    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_Id);

    //product name 
    echo $product->getName();

    //product Sku
    echo $product->getSku();

    //Product Image
    if($product->getImage() != '')
    {
        echo $product->getImage();
    }
}
catch(\Exception $e)
{
    echo $e->getMessage();
    exit;
}
1

SKU:

<?php $block->escapeHtml($_product->getSku()) ?>

Product Name:

<?php
      $_productNameStripped = $block->stripTags($_product->getName(), null, true);
?>

Image:

<?php 
     $_helper = $this->helper('Magento\Catalog\Helper\Output'); 
     $img = $this->helper('Magento\Catalog\Helper\Image')
                    ->init($product, 'product_base_image')
                    ->constrainOnly(TRUE)
                    ->keepAspectRatio(TRUE)
                    ->keepTransparency(TRUE)
                    ->keepFrame(FALSE)
                    ->resize(320, 240);
?>

Use below line wherever you want your image to be displayed:

<?php echo $img->getUrl(); ?>
0

Just put below code in your file example.php

<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();
$store = $obj->get('Magento\Store\Model\StoreManagerInterface')->getStore();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

/** get product data by product id ****/
$productId = 1;
$product = $obj->get('Magento\Catalog\Model\ProductRepository')
               ->getById($productId);

echo "Name :".$product->getName();
echo "Sku :". $product->getSku();
echo "Image : ".$store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
?>
0

You can manage to work this by using following code:

<?php
    use Magento\Framework\App\Bootstrap;
    require __DIR__ . '/app/bootstrap.php';
    $params = $_SERVER;
    $bootstrap = Bootstrap::create(BP, $params);
    $objectManager = $bootstrap->getObjectManager();
    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');
    $store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();

    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollection->create()->addAttributeToSelect('*')->load();

    foreach ($collection as $product){
        $name = $product->getName();
        $sku = $product->getSku();
        $imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();
        $image = '<img src="'.$imageUrl.'"/>';
    }
?>
1
  • Hi @Sukumar Gorai, I need your help. Could you please help me? Mar 7, 2019 at 5:35
0
   <?php  
use Magento\Framework\App\Bootstrap;
include('./app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
 
$objectManager = Magento\Framework\App\ObjectManager::getInstance();
getAllProducts($objectManager);

function getAllProducts($objectManager) {

$header[]=array('sku');
    $objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollection->create()->addAttributeToSelect('*')->load();
    $app_state = $objectManager->get('\Magento\Framework\App\State');
    $app_state->setAreaCode('frontend');
    $counter = 1;
    foreach ($collection as $product){
            $id=$product->getId();
            $sku=$product->getSku();
            $header[]=array($sku);
            echo $counter++.PHP_EOL;
    }
    $fp = fopen("sku.csv","w"); 
    foreach ($header as $line) {
        fputcsv($fp, $line);
    }
    fclose($fp);      
}
?>

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.