Monday, November 11, 2013

Custom Script to limit and Export product details in Magento

The following script shows how to limit and the export product details in magento to csv/xls format..



<?php 
ob_start();
require_once('./../app/Mage.php');

function test($cats)
{
$category="";
foreach ($cats as $category_id) 
       {
$cat = Mage::getModel('catalog/category')->load($category_id) ;
$category.=($cat->getName()).",";
}
return $categories = substr($category,0,-1);
}

Mage::app();

$count = 10;  // to get value of 10 records
$startAfter = 0; // to start from 0
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToSelect('*')->getSelect()->limit($count,$startAfter);                   /      //limit(COUNT,NEXT_RECORD_OF )

//header for the table names

$csvHeader =array("id", "SKU", "Product Name", "category", "price", "Special Price", "Special Price From", "Special Price To", "image", "store", "Description", "short Description", "Created at", "Updated at", "Weight", "Product Type", "Url path", "Url Key", "Color", "Manufacturing Country", "Quantity", "Status", "Visibility", "Meta Title", "Meta Description", "Meta Keywords", "tax class", "Min Sale Qty", "Max Sale Qty", "Manufacturer suggested retail price");

foreach($csvHeader as $head)
{
$header.= $head."\t ";
}

$data="";
foreach ($products as $product)
{
        $id = $product->getId();
$sku = $product->getSku();
$names = $product->getName();
$store = $product->getStore()->getName();
$images = $product->getImageUrl();
$prices = $product->getPrice();
$sPrice = $product->getSpecialPrice();
$specialTo = $product->getSpecialToDate();
$specialFrom = $product->getSpecialFromDate();
$desc = preg_replace('/[\n\r]+/', ',', trim($product->getDescription()));
$sDescrip = preg_replace('/[\n\r]+/', ',', trim($product->getShortDescription()));
$creation = $product->getCreatedAt();
$updatedAt = $product->getUpdatedAt();
$type = $product->getTypeID();
$weight = $product->getWeight();
$urlKey = $product->getUrl_key();
$url = $product->getProductUrl();
$color = $product->getAttributeText('color');
$manu = $product->getAttributeText('manufacturer');
$status = $product->getAttributeText('status');
$visibility = $product->getAttributeText('visibility');
$metaTitle = $product->getMetaTitle();
$metaDesc = $product->getMetaDescription();
$metaKeywords = $product->getMetaKeyword();
$taxClassId = $product->getTaxClassId();
$qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
$productQuantity = Mage::getModel("cataloginventory/stock_item")->loadByProduct($product->getId());
$minSaleQty = $productQuantity->getMinSaleQty();
$maxSaleQty = $productQuantity->getMaxSaleQty();
$msrp = $product->getMsrp();
$cats = $product->getCategoryIds();
$categories = test($cats);

$data.=$id."\t".$sku ."\t".$names ."\t".$categories ."\t".$prices ."\t".$sPrice ."\t".$specialFrom ."\t".$specialTo ."\t".$images ."\t".$store ."\t".$desc ."\t".$sDescrip ."\t".$creation ."\t".$updatedAt ."\t".$weight ."\t".$type ."\t".$url ."\t".$urlKey ."\t".$color ."\t".$manu ."\t".$qty ."\t".$status ."\t".$visibility ."\t".$metaTitle ."\t".$metaDesc ."\t".$metaKeywords ."\t".$taxClassId ."\t".$minSaleQty ."\t".$maxSaleQty ."\t".$msrp ."\t".$cate."\n";
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=reports.xls"); //filename: report.xls
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
ob_flush();
?>

No comments:

Post a Comment

I`m Possible

I`m Possible
"Better To Die On Your Feet Than To Live On Your Knees"