我正在尝试构建诸如 CSV 格式(Magento 社区 v1.7)之类的产品提要,但是我在提取某些数据时遇到了一些麻烦。我不介意共享代码,因此如下所示,如果有人能让我知道我可以使用什么来获取我无法提取的某些产品价值,我将不胜感激。
我想提取:(我怎样才能得到下面的值)
- 产品类别(我能够提取数组,但它们的值不对应于列出的产品类别,即整个列表具有相同的类别)
- 产品带(文本格式)
- 产品制造商(文本格式)
- 制造商零件编号
- 可用数量
- UPC
- 小图片 url(不是缓存 url)
- 缩略图图像 url(不是缓存 url)
- 产品尺寸(数组或字符串格式)
- 产品颜色(数组或字符串格式)
- 性别(如果指定)
我正在使用的代码如下
define('SAVE_FEED_LOCATION','productfeed.csv');
$objDateTime = new DateTime('NOW');
require 'app/Mage.php';
Mage::app('default');
try{
$handle = fopen(SAVE_FEED_LOCATION, 'w');
$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;
foreach($prodIds as $productId) {
if (++$counter < 50000){
$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 = '';
$cats = $product->getCategoryIds();
//$eee = implode(",",$cats);
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
if($subcats == ''){
$maincat = $subcats = $_cat->getName();
}else {
$subcats .= ">".$_cat->getName();
}
} //creating and setting parent category and other categories
$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; //this is spitting same data for all products
$product_data['SecondaryCategory'] = $subcats; //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); this need to be checked result not as expected
$product_data['RetailPrice'] = round($product->getPrice(),2);
$product_data['Brand'] = $product->getData('brand'); //not working
$product_data['ManufacturerPartNumber'] = ''; //need variable
$product_data['ManufacturerName'] = $product_data['manufacturer']; //not working
$product_data['Quantity'] = ''; //need variable
$product_data['UniversalProductCode'] = $product->getData('upc'); //need variable
$product_data['Currency'] = Mage::app()->getStore()->getCurrentCurrencyCode();
$product_data['Sizes']= '';
$product_data['Colours']= '';
$product_data['Gender']= '';
foreach($product_data as $k=>$val){
$bad=array('"',"\r\n","\n","\r","\t");
$good=array(""," "," "," ","");
$product_data[$k] = str_replace($bad,$good,$val);
}
echo $counter . " ";
$feed_line = implode("|", $product_data)."\r\n";
fwrite($handle, $feed_line);
fflush($handle);
}
}
fclose($handle);
}