通过php向Feed添加自定义属性

时间:2012-02-29 10:36:37

标签: php zend-framework magento magento-1.4

我有一家magento商店,我正在使用www.retailtower.com的服务来生成供稿。但是,我需要一个自定义属性才能从THEIR网站过滤。 自定义属性代码称为“is_imported”

我和零售商的人谈过,但他们说他们不能在我的产品中看到这个属性。

我检查了几个月前的一些电子邮件,我注意到为了能够看到我的产品,我必须安装PHP文件,然后他们用它来导入产品。

这是我找到的代码,如何修改它以添加自定义属性?

<?php 
set_time_limit(300);
ini_set('memory_limit', '-1');
include_once 'app/Mage.php';
umask(0);
Mage::app();


       $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToSelect('*');
    $products->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));

       $visibility = array(
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
        );
       $products->addAttributeToFilter('visibility', $visibility);

    $products->load(); 

    $baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

                        //echo "Total Count =: ".count($products);
                        //echo "<pre>";
                        //print_r($products);

      $collection = Mage::getResourceModel('catalog/product_collection');



 $output = '<?xml version="1.0" encoding="utf-8"?>
<products>';
if (count($collection)){ 
//echo "<pre>";
 foreach ($products as $product){
    //var_dump($product->getdata());die;
 $output .= '<product>';
     foreach ($product->getdata() as $key=>$value) {
        if ($key!=='stock_item') {
        //my code start

        $url = $product->getProductUrl();
         if (($key == 'url_path') || ($key =='url_key')){ 
         $value = $url;
         $value = str_replace('/productapi.php','',$value);
         $value = trim ($value);
         } 

        if ($key == 'image'){ 
         $value = $baseUrl."media/catalog/product".$value;
         //$value = str_replace('/productapi.php','',$value);
         //$value = trim ($value);
         }

         if ($key == 'manufacturer'){ 
         //$value = $baseUrl."media/catalog/product".$value;
         $value = Mage::getModel('catalog/product')->load(PRODUCT_ID);
         $value = $value->getAttributeText('manufacturer');
         //$value = "kofi";
         //$value = str_replace('/productapi.php','',$value);
         //$value = trim ($value);
         }

        //$search = array('&','<','>','"','\'','-','—',"'",'(',')','™','®','©');
        //$replace = array('&#38;','&lt;','&gt;','&quot;','&apos;','&#45;','&#x2015;','&#39;','&#40;','&#41;','&trade','&#174;','&copy');
        //$value = str_replace($search,$replace,$value);
        /* $value = str_replace('&','',$value);
         $value = str_replace('</br>','',$value);
         $value = str_replace('<br/>','',$value);
         $value = str_replace('>','',$value);
         $value = str_replace('<','',$value);*/
         $value = "<![CDATA[$value]]>";





         $key = str_replace('"','',$key);
         //my code end

            $output .= '<'.$key.'>'.$value.'</'.$key.'>';

        }


     }
$categories = $product->getCategoryIds();
$output .= '<categories>';
    foreach($categories as $k => $_category_id): 
           $_category = Mage::getModel('catalog/category')->load($_category_id);
             $cat_name = $_category->getName();
             $cat_url =  $_category->getUrl();

             $cat_name = "<![CDATA[$cat_name]]>";
             $cat_url = "<![CDATA[$cat_url]]>";

            $output .= '<category>';
            $output .= '<name>'.$cat_name.'</name>';
            $output .= '<url>'.$cat_url.'</url>';
            $output .= '</category>';        
    endforeach; 
 $output .= '</categories>';
 $output .= '</product>';   
 $url = $product->getProductUrl();
 //$url = str_replace('/productapi.php','',$url);
// $url = trim ($url); 


 }//endforeach;
  $output .= '
  </products>';
}//endif;
header ("Content-Type: text/xml; charset=ISO-8859-1");
print $output;




?> 

1 个答案:

答案 0 :(得分:0)

尝试添加

$products->addAttributeToSelect('is_imported');

$products = Mage::getModel('catalog/product')->getCollection();