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?

Ansar Husain
3,4791 gold badge15 silver badges32 bronze badges
asked Aug 27, 2018 at 6:14
2
  • where you want to get those data? Commented 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 Commented 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;
} 
?>
answered Aug 27, 2018 at 6:27
7
  • Hi @ Raj, I want to get and display image also along with name and SKU. Commented Aug 27, 2018 at 7:14
  • @DileepKumar Have you tried $product->getImage(); or $product->getMediaGalleryImages() for displaying image? Commented Aug 27, 2018 at 7:17
  • Hi @ Raj, it is displaying URL path but I want to display the image? Commented Aug 27, 2018 at 7:18
  • @DileepKumar Then you can add that url path in <img src="image_url"/> tag. Commented Aug 27, 2018 at 7:19
  • Hi @ Raj , I have added this code <img src="<?php $product->getImage(); ?>"> but image not displaying Commented 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;
}
answered Aug 27, 2018 at 6:29
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(); ?>
answered Aug 27, 2018 at 6:36
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();
?>
answered Aug 27, 2018 at 6:30
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.'"/>';
 }
?>
answered Aug 27, 2018 at 7:28
1
  • Hi @Sukumar Gorai, I need your help. Could you please help me? Commented 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); 
}
?>
answered Sep 19, 2022 at 21:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.