Export products to csv by script

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$collection = Mage::getModel(‘catalog/product’)
->getCollection()
->addAttributeToSelect(‘*’)
->addUrlRewrite();

// create a file pointer connected to the output stream
header(‘Content-Type: text/csv; charset=utf-8’);
header(‘Content-Disposition: attachment; filename=all_products.csv’);
$output = fopen(‘php://output’, ‘w’);

// output the column headings
fputcsv($output, array(‘id’, ‘sku’, ‘title’, ‘url’, ‘image0′,’image1′,’image2′,’image3′,’status’,’price’,’special_price’,’special_from_date’,’special_to_date’,’qty’,’is_in_stock’,’type’));
foreach($collection as $product) {
//Load the product categories
$categories = $product->getCategoryIds();
//Select the last category in the list
$categoryId = end($categories);
//Load that category
$category = Mage::getModel(‘catalog/category’)->load($categoryId);
// Collect details in variables
$title=$product->getName();
$sku=$product->getSku();
$id=$product->getId();

$url=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).$product->getUrlPath();
//$image=str_replace(‘export.php/’,”,Mage::getBaseUrl()).$product->getUrlPath($category);
$image=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . ‘catalog/product’ . $product->getImage();

//Gallary
//$_product = $product->load($product->getId());
$gallary = [”,”,”,”];
$mediaApi = Mage::getModel(‘catalog/product_attribute_media_api’);
$mediaItems = $mediaApi->items($product->getId());
foreach ($mediaItems as $key=> $images) {
$gallary[$key]=$images[‘url’];
}

$price=$product->getPrice();
$status = ‘Disabled’;
if(‘1’ == $product->getStatus()){
$status = ‘Enabled’;
}
$type=$product->getTypeId();
$sp=$product->getFinalPrice();
$from=$product->getSpecialFromDate();
$to = $product->getSpecialToDate();

$stock_item = Mage::getModel(‘cataloginventory/stock_item’)->loadByProduct($id);
$qty = $stock_item->getQty();
$stock =$stock_item->getIsInStock();
fputcsv($output, array( $id, $sku, $title, $url,$gallary[0],$gallary[1],$gallary[2],$gallary[3], $status,$price,$sp,$from,$to,$qty,$stock, $type));
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s