Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.66 KB  |  hits: 175  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Magento - joinField() query to add store_id to products in collection
  2. $_testproductCollection = Mage::getResourceModel('catalog/product_collection');
  3. $_testproductCollection->getSelect()->distinct(true);
  4. $_testproductCollection->addAttributeToSelect('*')->load();
  5.        
  6. $_testproductCollection->joinField('store_id', 'catalog_category_product_index', 'store_id', 'product_id=entity_id', '{{table}}.store_id = 1', 'left');
  7.        
  8. // A shortened example, not tested directly
  9. class Example_Product_List extends Varien_Db_Select {
  10.  
  11.     protected $_resource;
  12.  
  13.     public function __construct($storeId) {
  14.         // Since this isn't a collection we need to get the resource ourselves
  15.         $this->_resource = Mage::getSingleton('core/resource');
  16.         $adapter = $this->_resource->getConnection('core_read');
  17.         parent::__construct($adapter);
  18.  
  19.         $this->from(
  20.             array('catprod'=>$this->getTable('catalog/category_product')),
  21.            'product_id'
  22.         );
  23.         $this->joinInner(
  24.             array('catprodin'=>$this->getTable('catalog/category_product_index'),
  25.             '(catprodin.category_id=catprod.category_id) AND (catprodin.product_id=catprod.product_id)',
  26.             'store_id'
  27.         );
  28.         $this->where('store_id = ?', $storeId);
  29.         $this->group('product_id');
  30.     }
  31.  
  32.     public function getTable($modelEntity)
  33.     {
  34.         return $this->_resource->getTableName($modelEntity);
  35.     }
  36.  
  37. }
  38.  
  39. // Using it in a collection
  40. class Example_Module_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4 {
  41.  
  42.     protected function setStoreId($store) {
  43.         $this->getSelect()->where(array(
  44.             'attribute'=>'product_id',
  45.             'in'=>new Example_Product_list($store->getId())
  46.         );
  47.         return parent::setStoreId($store);
  48.     }
  49. }
  50.  
  51. // Putting it to use, '1' can be any store ID.
  52. $_testproductCollection = Mage::getResourceModel('examplemodule/product_collection');
  53. $_testproductCollection->addStoreFilter(1);
  54. $_testproductCollection->addAttributeToSelect('*')->load();
  55.        
  56. grep '>addStoreFilter' app/code -rsn
  57.        
  58. Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addStoreFilter(Mage::app()->getStore()->getId());
  59.        
  60. $_rootcatID = Mage::app()->getStore()->getRootCategoryId();
  61.  
  62. $_testproductCollection = Mage::getResourceModel('catalog/product_collection')
  63. ->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
  64. ->addAttributeToFilter('category_id', array('in' => $_rootcatID))
  65. ->addAttributeToSelect('*');
  66. $_testproductCollection->load();
  67.  
  68. foreach($_testproductCollection as $_testproduct){
  69.     echo $this->htmlEscape($_testproduct->getName())."<br/>";
  70. };