Wednesday, 29 November 2017

Magento Programatically Export All Products Data Into Custom CSV Files.

It's simple to create custom csv in magento . follow the steps to create custom csv. 

Create php page like 'get_product_export.php' in magento install root directory. and paste the below code.


<?php

ini_set('memory_limit','2048M');
ini_set('max_execution_time',36000000);
    require_once 'app/Mage.php';
Mage::app("default");
$_helper = Mage::helper('catalog/output');

$filename ='';
$filename ='export_all_products.csv';

$_productCollection = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*');

$file = fopen($filename, 'w'); 

    function getProductcatid($ProductId){
   
        $productload = Mage::getModel('catalog/product')->load($ProductId);
        $cats = $productload->getCategoryIds();
        foreach ($cats as $category_id) {
            $_cat .= $category_id.',' ;
           
        }
        return  substr($_cat,0,-1);
    }

    function getAttrLabel($name, $attr_val, $_product){
    if(!empty($attr_val)){
$attr_val = explode(" ", $attr_val);
$attr_names = [];
foreach ($attr_val as $attr_id) {
$attr = $name;
$attr = $_product->getResource()->getAttribute($attr);
if ($attr->usesSource()) {
    $attr_info = $attr->getSource()->getOptionText($attr_id);
    $attr_info = implode(",", $attr_info);
    $AttributeMaterial = $attr_info;
    return $attr_info;
}
else{
return "";
}
}
}
else{
return "";
}
    }

foreach ($_productCollection as $_product):  
    $product_id = $_product->getId();
    $status="FALSE";
    $name= $_product->getName();
$name=str_replace("&","&amp;",$name);
$ProductId= $_product->getEntityId();
$ProductGroup= $_product->getSku();
$ProductNameDE= $name;
$desc1=strip_tags($_product->getDescription());
$desc1=str_replace("&nbsp;"," ",$desc1);
$desc1=str_replace("&","&amp;",$desc1);
$ProductLongDescriptionDE= $desc1;

$Category= getProductcatid($ProductId);
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

$imageurl="https://xxxxxx.xxx/media/catalog/product";
$Inventory=$stock->getQty();;
$ProductVAT="";
$Warranty="";
$GTIN="";
$DeliveryMethod1="";
$ProductBrandName="";

$AttributeGrosse=$_product->getGroesse();
$AttributeRingBreite="";
$AttributeRingBreite = getAttrLabel('ring_breite', $_product->getRingBreite(), $_product);
$AttributeHalskettenlange= getAttrLabel('halskette_laenge', $_product->getHalskettenlange(), $_product);
$AttributeAnhangerBreite= getAttrLabel('anhaenger_breite', $_product->getAnhangerBreite(), $_product);
$AttributeBreite= getAttrLabel('breite', $_product->getBreite(), $_product);
$AttributeLange= getAttrLabel('lange', $_product->getLange(), $_product);
$AttributeArmbandtyp= getAttrLabel('armband_typ', $_product->getArmbandtyp(), $_product);
$AttributeFarbe= getAttrLabel('farbe', $_product->getFarbe(), $_product);
$AttributeMaterial= getAttrLabel('material', $_product->getMaterial(), $_product);
$AttributeSteinart= getAttrLabel('steine', $_product->getSteinart(), $_product);
$AttributeVerschlussart="";
$AttributeGeschlecht="";
$ProductImageURL1 = $imageurl.$_product->getImage();

$gallery_images = Mage::getModel('catalog/product')->load($product_id)->getMediaGalleryImages();
$items = array();
foreach($gallery_images as $g_image) {
    $items[] = $g_image['url'];
}

$ProductImageURL1="";
$ProductImageURL2="";
$ProductImageURL3="";
$ProductImageURL4="";
$ProductImageURL5="";
$ProductImageURL6="";

if(isset($items[0])){
$ProductImageURL1 = $items[0];
}
if(isset($items[1])){
$ProductImageURL2 = $items[1];
}
if(isset($items[2])){
$ProductImageURL3 = $items[2];
}
if(isset($items[3])){
$ProductImageURL4 = $items[3];
}
if(isset($items[4])){
$ProductImageURL5 = $items[4];
}
if(isset($items[5])){
$ProductImageURL6 = $items[5];
}

$ProductSalesPrice = $_product->getSpecialPrice();
$ProductOriginalPrice = $_product->getPrice();

    if($_product->getStatus()=="1"){$status="TRUE";}
   
$ProductPublished = $status;


$data[] = array($ProductId, $ProductGroup, $ProductNameDE, $ProductLongDescriptionDE, $Category, $Inventory, $ProductVAT,
$Warranty, $GTIN, $DeliveryMethod1, $ProductBrandName, $AttributeGrosse, $AttributeRingBreite, $AttributeHalskettenlange,
$AttributeAnhangerBreite, $AttributeLange, $AttributeBreite, $AttributeLange, $AttributeArmbandtyp, $AttributeFarbe,
$AttributeMaterial, $AttributeSteinart, $AttributeVerschlussart, $AttributeGeschlecht, $ProductImageURL1, $ProductImageURL2,
$ProductImageURL3, $ProductImageURL4, $ProductImageURL5, $ProductImageURL6, $ProductSalesPrice,
$ProductOriginalPrice, $ProductPublished); 
 
endforeach; 
// print_r($data);

fputcsv($file, array('ProductId','ProductGroup','ProductNameDE','ProductLongDescriptionDE','Category','Inventory',
'ProductVAT','Warranty','GTIN','DeliveryMethod 1','ProductBrandName','Attribute Grösse','Attribute Ring Breite',
'Attribute Halskettenlänge','Attribute Anhänger Breite','Attribute Länge','Attribute Breite','Attribute Länge',
'Attribute Armbandtyp','Attribute Farbe','Attribute Material','Attribute Steinart','Attribute Verschlussart',
'Attribute Geschlecht','ProductImageURL 1','ProductImageURL 2','ProductImageURL 3','ProductImageURL 4',
'ProductImageURL 5','ProductImageURL 6','ProductSalesPrice','ProductOriginalPrice','ProductPublished')); 

foreach($data as $row){
   fputcsv($file, $row);     
}

// header('Content-Type: application/csv');
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'"');
exit;
 
  echo "Done";


?>


Thanks,
Kuldeep singh

No comments:

Post a Comment