Magento – Export/Retrieve products with specific attribute value

If you want to export limited fields using custom filter using code and easier way without using admin panel, below is the steps to approach.
Here id, sku, name, price values are getting, if you want more then you can append below in the field.

Steps

  1. Copy below code and create a file, then save as export_products.php
  2. Upload file to root of magento install
  3. Run http://yourdomain/export_products.php
  4. It will print all values using filter used below on page
  5. Once above records retrieve in table format then copy and paste it in excel and save it as csv file type.
<?php
require_once 'app/Mage.php';
ini_set('display_errors', 1);
umask(0);
Mage::app('default');

set_time_limit(0);
ini_set('memory_limit','1024M');

function empty_pk($data){
if($data!=''){return $data;}
else {return "&nbsp;";}
}

//Instantiate product collection
$collection = Mage::getModel('catalog/product')->getCollection();
//filter for name is 'ABC'
$collection->addAttributeToFilter('name','ABC');
//filter for status = 1 enabled
$collection->addAttributeToFilter('status', array('eq' => 1)); //1 - enabled, 2 - disabled

//Below filter is getting range of entity_id between 100 and 2000
//filter for entity id is greater than (gt) 100
$collection->addFieldToFilter(array(
 array('attribute'=>'entity_id','gt'=>'100')
 ));

//AND filter for entity id is less than (lt) 2000
$collection->addFieldToFilter(array(
 array('attribute'=>'entity_id','lt'=>'2000')
 ));

//A full list of the supported short conditionals (eq,lt, etc.) can be found in the _getConditionSql method in lib/Varien/Data/Collection/Db.php

echo '<table border="1"> ';
echo '<tr>';
echo "<th>id</th>";
echo "<th>sku</th>";
echo "<th>name</th>";
echo "<th>price</th>";
... //add here more field
echo '</tr>';

foreach ($collection as $product_all) {

$sku = $product_all['sku'];
// retrieve product id using sku
$product_id = Mage::getModel('catalog/product')->getIdBySku($sku);
// call product model and create product object
$product    = Mage::getModel('catalog/product');
// Load product using product id
$product->load($product_id);

$pk_entity = empty_pk($product['entity_id']);
$pk_sku = empty_pk($product['sku']);
$pk_name = empty_pk($product['name']);
$pk_price = empty_pk($product['price']);

echo '<tr>';
echo "<td>".$pk_entity."</td>";
echo "<td>".$pk_sku."</td>";
echo "<td>".$pk_name."</td>";
echo "<td>".$pk_price."</td>";
... //print here more field
echo '</tr>';
}
echo '</table>';
?>

Hurray..!! this retrieve desired values from the database easier way out.

Previous Post

Update products prices in Magento easier way

Next Post

Manual cleaning magento database logs phpmyadmin

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top