Advertisement
Guest User

Untitled

a guest
Jan 31st, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.29 KB | None | 0 0
  1. <?php
  2.  
  3. class Company_Rss_Feed
  4. {
  5.  
  6.     protected $storeViews = array();
  7.     protected $feedPath;
  8.     protected $mediaConfig;
  9.     protected $manufacturers = array();
  10.  
  11.     public function __construct()
  12.     {
  13.         $this->feedPath = Mage::getBaseDir() . DS . 'feed';
  14.         $this->mediaConfig = Mage::getModel('catalog/product_media_config');
  15.        
  16.         foreach (Mage::app()->getStores() as $store) {
  17.             if ((int) $store->getIsActive() && strpos($store->getCode(), 'preview') == false) {
  18.                 array_push($this->storeViews, $store);
  19.             }
  20.         }
  21.        
  22.         $attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'manufacturer');
  23.         if ($attribute->usesSource()) {
  24.             $options = $attribute->getSource()->getAllOptions(false);
  25.             foreach ($options as $id => $option) {
  26.                 $this->manufacturers[$option['value']] = $option['label'];
  27.             }
  28.         }
  29.     }
  30.  
  31.     public function generateXml()
  32.     {
  33.         if (is_array($this->storeViews)) {
  34.             foreach ($this->storeViews as $storeView) {
  35.                 $path = $this->feedPath . DS . $storeView->getCode();
  36.                 $feedFile = 'products.xml';
  37.  
  38.                 $io = new Varien_Io_File();
  39.                 $io->setAllowCreateFolders(true);
  40.                 $io->open(array('path' => $path));
  41.  
  42.                 if ($io->fileExists($path) && ! $io->isWriteable($path)) {
  43.                     Mage::throwException('File "%s" cannot be saved. Please, make sure the directory "%s" is writeable by web server.', $feedFile, $path);
  44.                 }
  45.  
  46.                 $io->streamOpen($feedFile);
  47.                 $io->streamWrite('<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL);
  48.                 $io->streamWrite('<products>' . PHP_EOL);
  49.  
  50.                 $collection = $this->__getProductCollection($storeView);
  51.                 foreach ($collection as $product) {
  52.                     $object = new Varien_Object();
  53.                     $object->setData(array(
  54.                         'name' => $product->getName(),
  55.                         'sku' => $product->getSku(),
  56.                         'category' => $product->getCategoryName(),
  57.                         'brand' => isset($this->manufacturers[$product->getManufacturer()]) ? $this->manufacturers[$product->getManufacturer()] : null,
  58.                         'image' => $this->mediaConfig->getMediaUrl($product->getImage()),
  59.                         'price' => $product->getFinalPrice(),
  60.                         'currency' => $storeView->getCurrentCurrencyCode(),
  61.                         'description' => $product->getDescription(),
  62.                         'url' => $storeView->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) . $product->getCategoryPath() . $product->getUrlPath()
  63.                     ));
  64.                     $io->streamWrite($object->toXml());
  65.                 }
  66.  
  67.                 $io->streamWrite('</products>' . PHP_EOL);
  68.                 $io->streamClose();
  69.             }
  70.         }
  71.     }
  72.  
  73.     private function __getProductCollection($storeView)
  74.     {
  75.         $collection = Mage::getResourceModel('catalog/product_collection');
  76.         $collection
  77.             ->addStoreFilter($storeView->getId())
  78.             ->addAttributeToSelect(array('name', 'price', 'description', 'url_path', 'sku', 'manufacturer', 'image'))
  79.             ->addAttributeToFilter('visibility', Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
  80.             ->addAttributeToFilter('status', Mage::getSingleton('catalog/product_status')->getVisibleStatusIds())
  81.             ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left')
  82.             ->joinTable('catalog_category_flat_store_' . $storeView->getId(), 'entity_id=category_id', array('category_path' => 'url_path', 'category_name' => 'name'), null, 'inner')
  83.             ->groupByAttribute('entity_id');
  84.  
  85.         return $collection;
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement