1

Trying to load a block for a store

$cms_block = Mage::getModel('cms/‌​block')->setStoreId($‌​storeId)->load($block‌​);

and printing it I obtain two different arrays.

If the block is enabled the result is an array full of informations ([block_id], [title], [is_active] etc.)

Mage_Cms_Model_Block Object
(
    [_cacheTag:protected] => cms_block
    [_eventPrefix:protected] => core_abstract
    [_eventObject:protected] => object
    [_resourceName:protected] => cms/block
    [_resource:protected] => 
    [_resourceCollectionName:protected] => cms/block_collection
    [_dataSaveAllowed:protected] => 1
    [_isObjectNew:protected] => 
    [_data:protected] => Array
        (
            [block_id] => 214
            [title] => Home slide 1 - ES
            [identifier] => block_slide1
            [content] => <a href="{{store url='about-magento-demo-store'}}">

    <img src="{{media url="wysiwyg/promo-homepage/slide1-fr.jpg"}}" alt="" />

</a>
            [creation_time] => 2016-08-17 08:41:53
            [update_time] => 2016-08-17 13:33:04
            [is_active] => 1
            [store_id] => Array
                (
                    [0] => 5
                )

            [stores] => Array
                (
                    [0] => 5
                )

        )

    [_hasDataChanges:protected] => 
    [_origData:protected] => Array
        (
            [block_id] => 214
            [title] => Home slide 1 - ES
            [identifier] => block_slide1
            [content] => <a href="{{store url='about-magento-demo-store'}}">

    <img src="{{media url="wysiwyg/promo-homepage/slide1-fr.jpg"}}" alt="" />

</a>
            [creation_time] => 2016-08-17 08:41:53
            [update_time] => 2016-08-17 13:33:04
            [is_active] => 1
            [store_id] => Array
                (
                    [0] => 5
                )

            [stores] => Array
                (
                    [0] => 5
                )

        )

    [_idFieldName:protected] => block_id
    [_isDeleted:protected] => 
    [_oldFieldsMap:protected] => Array
        (
        )

    [_syncFieldsMap:protected] => Array
        (
        )

)

but if the block is disabled these data are not available:

Mage_Cms_Model_Block Object
(
    [_cacheTag:protected] => cms_block
    [_eventPrefix:protected] => core_abstract
    [_eventObject:protected] => object
    [_resourceName:protected] => cms/block
    [_resource:protected] => 
    [_resourceCollectionName:protected] => cms/block_collection
    [_dataSaveAllowed:protected] => 1
    [_isObjectNew:protected] => 
    [_data:protected] => Array
        (
            [store_id] => 1
        )

    [_hasDataChanges:protected] => 
    [_origData:protected] => Array
        (
            [store_id] => 1
        )

    [_idFieldName:protected] => block_id
    [_isDeleted:protected] => 
    [_oldFieldsMap:protected] => Array
        (
        )

    [_syncFieldsMap:protected] => Array
        (
        )

)

So, how can I load the info for a disabled CMS blocks?

I need to set them to enabled status.

0

3 Answers 3

3

Tried and tested, this should do the trick. Change store ID, block code, and the on/off switch as needed.

<?php

$storeId     = 1; // change store ID as appropriate
$blockCode   = 'home-decor'; // change to match your block's identifier code
$onOffSwitch = 0; // want to make block active or inactive?

/** @var Mage_Cms_Model_Resource_Block_Collection $collection */
$collection = Mage::getModel('cms/block')->getCollection();
$collection->addStoreFilter($storeId);
$collection->addFieldToFilter('identifier',$blockCode);

$block = $collection->getFirstItem();
if($id = $block->getId()){
    $block->load($id);
    $block->setData('is_active', $onOffSwitch);
    $block->save();
}

Alternative solution:

<?php 

$storeId     = 1; // change store ID as appropriate
$blockCode   = 'home-decor'; // change to match your block's identifier code
$onOffSwitch = 0; // want to make block active or inactive?

/** @var Mage_Cms_Model_Block $model */
$model    = Mage::getModel('cms/block');
$resource = $model->getResource();
$table    = $resource->getMainTable();
$idField  = $resource->getIdFieldName();

$adapter  = Mage::getSingleton('core/resource')->getConnection('core_write');

/** @var Varien_Db_Select $select */
$select = $adapter->select()->from(array('main_table' => $table), $idField);
$select->join(array('store_table' => $adapter->getTableName('cms_block_store')), "main_table.{$idField} = store_table.{$idField}");
$select->where('main_table.identifier = ?', $blockCode);
$select->where('store_table.store_id = ?', $storeId);
if($id = $adapter->fetchOne($select)) {
    $adapter->update($table, array('is_active' => $onOffSwitch), array("{$idField} = ?" => $id));
}

Additionally, rather than using Mage::log() to see the output of your query, do this instead, as it will show you the final queries after all pieces have been put together.

Go to lib/Varien/Db/Adapter/Pdo/Mysql.php and set the following properties to true.

protected $_debug = false; //change to true
protected $_logAllQueries = false; //change to true

All queries that take place will be logged in var/debug/pdo_mysql.log.

In response to your initial question, it seems that you are not loading the block, or at least properly, because there is no valid reason that it would show less info for a disabled block. I just tested the return data for a block, both enabled and disabled and here are the results.

enter image description here

enter image description here

The info in my screenshots was obtained by using the snippet below.

$blockId = 35;
$block = Mage::getModel('cms/block')->load($blockId);

I should also mention, that when looping through a collection and doing $block->getData() you are not going to see all the data because the object is not fully loaded. You either have to use $collection->addFieldToSelect('*') or load the block within your loop. It is MUCH more efficient to add your fields to the select statement but you may have to join the cms_block_store table to get all data.

13
  • I've already developed a snippet like your. The only difference is addStoreFilter($storeId); .. I don't know why it doesn't add store filter to the query; I've debugged it. So I replace it with this code: $cms_blocks = Mage::getModel('cms/block')->getCollection()->addFieldTo‌​Filter('identifier', array('eq' => $my_identifier)); $select = $cms_blocks->getSelect()->join(array('block_store' => $cms_blocks->getTable('cms/block_store')), 'main_table.block_id = block_store.block_id', array('store_id'))->where('block_store.store_id IN (?)', array($storeId));
    – WaPoNe
    Aug 18, 2016 at 13:06
  • Did you try my snippet unmodified? Works like a charm whether activating or deactivating Aug 18, 2016 at 13:17
  • Yes, I did. I have the issue above described. I'm using my solution (see below).
    – WaPoNe
    Aug 18, 2016 at 13:20
  • Just a thought. Are you debugging by letting the code run fully or are you stopping the code and echoing getSelect()? Aug 18, 2016 at 13:22
  • 1
    @WaPoNe I also just updated the answer to give you a better way to log your queries in the future. It will help you avoid the situation where filters aren't showing up. Aug 18, 2016 at 15:09
2

This is due to ->setStoreId($‌​storeId) filter.

Just use $cms_block = Mage::getModel('cms/‌​block')->load($block‌​); to load CMS block.

You don't need to filter CMS model by store_id because all CMS blocks will have different IDs saved per store.

UPDATE: To save/update CMS block use :

//This will set CMS block Status to Enabled
Mage::getModel('cms/block')->load($block)->setData('is_active', 1)->save();
9
  • I need to specify the store because I have same block identifier for my all four store and I want to set on (enable) only for one store. How can I do this?
    – WaPoNe
    Aug 17, 2016 at 14:27
  • Do you want to SAVE your block (per store) OR you want to show/load the data (per store) event if it's disabled ? Aug 17, 2016 at 14:29
  • I want to SAVE its 'status' (is_active = 1) to enable it.
    – WaPoNe
    Aug 17, 2016 at 14:30
  • for that you don't need to filter cms model by store id because all cms block will have different IDs saved per store. CHECK updated answer Aug 17, 2016 at 14:31
  • 1
    @WaPoNe You have to call the collection of your blocks filtered by identifier AND store id. From that collection, you can derive the actual block id. Then pass that block id into your call to load($blockId) Aug 17, 2016 at 16:49
0

An alternative solution:

$cms_blocks = Mage::getModel('cms/block')->getCollection()
   ->addFieldToFilter('identifier', array('eq' => trim($your_identifier)));
 $select = $cms_blocks->getSelect()
    ->join(array('block_store' => $cms_blocks->getTable('cms/block_store')), 'main_table.block_id = block_store.block_id', array('store_id'))
    ->where('block_store.store_id IN (?)', array($storeId));

$blockId = $cms_blocks->getFirstItem()->getBlockId();

 // Block enabling
 Mage::getModel('cms/block')->load($blockId)->setData('is_active', 1)->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.