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?
6 Answers
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;
}
?>
-
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?– RajAug 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.– RajAug 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
<?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;
}
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(); ?>
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();
?>
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.'"/>';
}
?>
<?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);
}
?>