i'm trying to build a feed generator based on this post https://stackoverflow.com/questions/17956930/magento-product-data-extraction-for-product-feed. for the time beeing my problem is with the categories and subcategories of the product. my code:
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);
define('SAVE_FEED_LOCATION','feed/compari.csv');
//$objDateTime = new DateTime('NOW');
require 'app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status', 1); //1 is set to select product in stock
$products->addAttributeToFilter('visibility', 4); //4 is set to select active products
$products->addAttributeToSelect('*');
$prodIds=$products->getAllIds();
$product = Mage::getModel('catalog/product');
$counter = 0;
$feedaray=[];
foreach($prodIds as $productId) {
if (++$counter < 500000){
$product->load($productId);
$title_temp = $product->getName();
if (strlen($title_temp) > 255){
$title_temp = str_replace("Supply", "", $title_temp);
$title_temp = str_replace(" ", " ", $title_temp);
} //$title_temp will hold the product name
$maincat = $subcats = '';
$categoryCollection = $product->getCategoryCollection();
if i do a var_dump($title_temp.' categorie: '.$categorie_id);
foreach($categoryCollection as $cat){
$categorie_id=$cat->getData()['entity_id'];
$_cat_ac = Mage::getModel('catalog/category')->load($categorie_id);
if($subcats_ac == ''){
$maincat_ac = $subcats_ac = $_cat_ac->getName();
}else {
$subcats_ac .= ">".$_cat_ac->getName();
}
echo '<pre>';
var_dump($title_temp.' categorie: '.$categorie_id);
echo '</pre>';
}
i'm geting the corect result:
string(59) "product 1 categorie: 3" - prime category
string(60) "product 1 categorie: 37" - sub category
string(45) "product 2 categorie: 4" - prime category
string(46) "product 2 categorie: 21" - sub category
further i need to prepare the data for exporting to csv, and i do it like this:
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$product_data = array();
$product_data['ProductID'] = $productId;
$product_data['ProductName'] = substr(iconv("UTF-8","UTF-8//IGNORE",$title_temp), 0, 255);
$product_data['SKUnumber'] = $product->getSku();
$product_data['PrimaryCategory'] = $maincat_ac; //this is spitting same data for all products
$product_data['SecondaryCategory'] = $subcats_ac; //this is spitting same data for all products
$product_data['ProductURL'] = $StoreURL.$product->getUrlPath(); //$StroeURL is set as a string
$product_data['ProductImageURL'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();
$product_data['ShortProductDescription'] = substr(iconv("UTF-8","UTF-8//IGNORE",$product->getDescription()), 0, 80)."...";
$product_data['LongProductDescription'] = substr(iconv("UTF-8","UTF-8//IGNORE",$product->getDescription()), 0, 2000);
$product_data['SalePrice'] = round($product->getFinalPrice(),2);
$product_data['RetailPrice'] = round($product->getPrice(),2);
$product_data['ManufacturerName'] =$product->getAttributeText('manufacturer');
$product_data['Quantity'] = round($stock->getQty(),2);
$product_data['Currency'] = Mage::app()->getStore()->getCurrentCurrencyCode();
foreach($product_data as $k=>$val){
$bad=array('"',"\r\n","\n","\r","\t");
$good=array(""," "," "," ","");
$product_data[$k] = str_replace($bad,$good,$val);
}
$feedaray[]=$product_data;
echo $counter . " ";
}
}
$handle=fopen(SAVE_FEED_LOCATION, 'w');
foreach ($feedaray as $fields) {
fputcsv($handle, $fields);
}
fclose($handle);
?>
the problem is with the maincat_ac and subcat_ac that are not displaing corect data.