Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Company_Rss_Feed
- {
- protected $storeViews = array();
- protected $feedPath;
- protected $mediaConfig;
- protected $manufacturers = array();
- public function __construct()
- {
- $this->feedPath = Mage::getBaseDir() . DS . 'feed';
- $this->mediaConfig = Mage::getModel('catalog/product_media_config');
- foreach (Mage::app()->getStores() as $store) {
- if ((int) $store->getIsActive() && strpos($store->getCode(), 'preview') == false) {
- array_push($this->storeViews, $store);
- }
- }
- $attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'manufacturer');
- if ($attribute->usesSource()) {
- $options = $attribute->getSource()->getAllOptions(false);
- foreach ($options as $id => $option) {
- $this->manufacturers[$option['value']] = $option['label'];
- }
- }
- }
- public function generateXml()
- {
- if (is_array($this->storeViews)) {
- foreach ($this->storeViews as $storeView) {
- $path = $this->feedPath . DS . $storeView->getCode();
- $feedFile = 'products.xml';
- $io = new Varien_Io_File();
- $io->setAllowCreateFolders(true);
- $io->open(array('path' => $path));
- if ($io->fileExists($path) && ! $io->isWriteable($path)) {
- Mage::throwException('File "%s" cannot be saved. Please, make sure the directory "%s" is writeable by web server.', $feedFile, $path);
- }
- $io->streamOpen($feedFile);
- $io->streamWrite('<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL);
- $io->streamWrite('<products>' . PHP_EOL);
- $collection = $this->__getProductCollection($storeView);
- foreach ($collection as $product) {
- $object = new Varien_Object();
- $object->setData(array(
- 'name' => $product->getName(),
- 'sku' => $product->getSku(),
- 'category' => $product->getCategoryName(),
- 'brand' => isset($this->manufacturers[$product->getManufacturer()]) ? $this->manufacturers[$product->getManufacturer()] : null,
- 'image' => $this->mediaConfig->getMediaUrl($product->getImage()),
- 'price' => $product->getFinalPrice(),
- 'currency' => $storeView->getCurrentCurrencyCode(),
- 'description' => $product->getDescription(),
- 'url' => $storeView->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) . $product->getCategoryPath() . $product->getUrlPath()
- ));
- $io->streamWrite($object->toXml());
- }
- $io->streamWrite('</products>' . PHP_EOL);
- $io->streamClose();
- }
- }
- }
- private function __getProductCollection($storeView)
- {
- $collection = Mage::getResourceModel('catalog/product_collection');
- $collection
- ->addStoreFilter($storeView->getId())
- ->addAttributeToSelect(array('name', 'price', 'description', 'url_path', 'sku', 'manufacturer', 'image'))
- ->addAttributeToFilter('visibility', Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
- ->addAttributeToFilter('status', Mage::getSingleton('catalog/product_status')->getVisibleStatusIds())
- ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left')
- ->joinTable('catalog_category_flat_store_' . $storeView->getId(), 'entity_id=category_id', array('category_path' => 'url_path', 'category_name' => 'name'), null, 'inner')
- ->groupByAttribute('entity_id');
- return $collection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement