Can you help me on how to create a php script to display all products with special price including it's sku.

  • Write more about it.
    – Sohel Rana
    Apr 3, 2017 at 3:46
  • Hi Sohel, I'd like to display only the products with special price with a value of 0 only and display also it's SKU so that I can track them down. Thanks Apr 3, 2017 at 3:54

3 Answers 3


Use these Code for your requirement

use \Magento\Framework\App\Bootstrap;

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');

$productCollectionFactory = $objectManager
$productcollection = $productCollectionFactory->create()
foreach ($productcollection as $product) {
    if ($product->getSpecialPrice() == 0) {
        echo $product->getId()."<br/>";
        echo $product->getSku()."<br/>";
  • Hi Pranay k, Thanks for this code. I'll try this and let you know if it worked. Thanks again Apr 3, 2017 at 7:00
  • This code works better in my end. Thanks again Apr 3, 2017 at 7:03
  • your Welcome @MazeStricks Apr 3, 2017 at 7:04

Please create a test.php file at your magento2 root directory

Inser below code into test.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');

$productCollection = $obj->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()

foreach ($collection as $product){

     echo 'SKU:  '.$product->getSku().'<br>';   
     echo 'Name:  '.$product->getName().'<br>';
     echo 'Price:    '.$product->getPrice().'<br>';
     echo 'Special price:    '.$product->getSpecialPrice().'<br>';
     echo '----------------------------<br>';       


now run URL: like : http://yourdomain/test.php


error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
ini_set('display_errors', 1);
$products = Mage::getModel("catalog/product")->getCollection();
$fp = fopen('getId.csv', 'w');
$csvHeader = array("id","sku","images");
fputcsv($fp, $csvHeader, ",");
foreach ($products as $product) {
    $id = $product->getId();
    $sku = $product->getSku();
    $gallery_images = $product->getMediaGalleryImages();
    $items = array();
    foreach($gallery_images as $g_image) {
        $items[] = $g_image['url'];
    fputcsv($fp, array($id,$sku,implode(',', $items)), ",");

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.