1

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

2
  • 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

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

$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/>";
    }       
}
3
  • 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
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

0
0
<?php

error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
Mage::app();
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToSelect('*')->load();
$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)), ",");
}
fclose($fp);

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.