我正在尝试根据此帖Magento: Product data extraction for product feed构建Feed生成器。因为我的问题是产品的类别和子类别。 我的代码:
<?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();
如果我做var_dump($ title_temp。&#39;类别:&#39;。$ 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>';
}
我正在考虑核心结果:
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
我还需要准备导出到csv的数据,我这样做:
$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);
?>
问题在于maincat_ac和subcat_ac没有取代核心数据。
答案 0 :(得分:0)
解决问题的方法
$cats=[];
foreach($categoryCollection as $cat){
$cats[]=$cat->getData()['entity_id'];
}
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
if($subcats == ''){
$maincat = $subcats = $_cat->getName();
}else {
$subcats .= ">".$_cat->getName();
}
}
可能不是最佳解决方案。如果您有其他想法请分享。 问候