-->

Magento: Product data extraction for product feed

2019-08-30 10:33发布

问题:

I am trying to build a product feed such as CSV format (Magento community v1.7) however I am having some troubles to extract certain data. I don’t mind sharing code so it is as below and will highly appreciate if anyone can let me know what I can use the get certain product values which I am unable to extract.

I want to extract: (how can i get the values for below)

  1. Product Category (I am able to extract the array but they values does not correspond to the category product is listed on i.e. and entire list has the same category)
  2. Band of the product (text format)
  3. Manufacturer of the product (text format)
  4. Manufacturer part number
  5. Available Quantity
  6. UPC
  7. Small image url (not the cache url)
  8. Thumbnail image url (not the cache url)
  9. Sizes of the product (in array or string format)
  10. Colours of the product (in array or string format)
  11. Gender (if assigned)

Code that i am using as below

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);
}