Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento CMS Block Collection, can't filter by store id - addStoreFilter([storeid]) not working

How to filter a cms/block (static block) collection ?

This works for cms page:

$model = Mage::getModel('cms/page');
$collection = $model->getCollection()->addStoreFilter(3);

This does NOT work (returns unfiltered collection):

$model = Mage::getModel('cms/block');
$collection = $model->getCollection()->addStoreFilter(3);

I also tried working with the resource models 'cms/block' and 'cms/block_collection', no results.

Why is Magento that inconsequent ?! Sometimes I really start hating Magento for this. Please help.

like image 900
ebonik Avatar asked Dec 25 '22 14:12

ebonik


2 Answers

What do you exactly mean with unfiltered?

The addStoreFilter has a 2nd parameter to include the admin store as well

addStoreFilter($store, $withAdmin = true)

So if you have any static blocks linked to all stores, these will also be in your collection.

Is this the issue you have?

like image 79
Mike Avatar answered Dec 28 '22 07:12

Mike


Try this code:

$collection = Mage::getModel('cms/block')->getCollection();
$select = $collection->getSelect()->join(
    array('block_store' => $collection->getTable('cms/block_store')),
    'main_table.block_id = block_store.block_id',
    array('store_id')
)->where('block_store.store_id IN (?)', array(8));
foreach ($collection as $block) {
   // here you can use $block 
}

You can do the same for cms/page. Just change

cms/block -> cms/page and cms/block_store -> cms/page_store

like image 45
oleksii.svarychevskyi Avatar answered Dec 28 '22 06:12

oleksii.svarychevskyi