1

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 '17 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 – MazeStricks Apr 3 '17 at 3:54
2

Use these Code for your requirement

<?php
use \Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productcollection = $productCollectionFactory->create()
                        ->addAttributeToSelect('*')
                        ->load();

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 – MazeStricks Apr 3 '17 at 7:00
  • This code works better in my end. Thanks again – MazeStricks Apr 3 '17 at 7:03
  • your Welcome @MazeStricks – Learing_Coder Apr 3 '17 at 7:04
1

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

Inser below code into test.php

<?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');

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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