2

I have created a cron that in configured date enables or disables some CMS blocks.

It works fine only for disabling action and not for enabling. It's so strange because I call the same method:

Mage::getModel('cms/block')->setStoreId($storeId)->load($block)->setData('is_active', 1)->save();

to enable DOESN'T WORK

Mage::getModel('cms/block')->setStoreId($storeId)->load($block)->setData('is_active', 0)->save();

to disable WORKS

What is wrong?

0

2 Answers 2

1

Try this and let me know it its work :

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

OR

Mage::getModel('cms/block')->getCollection()->addFieldToFilter('identifier','my_block_id')->addStoreFilter($store, true)->load()->setData('content', 'Example content')->save();
1
  • Thanks for your help but the first advice works but I need to load block for specific store; the second one doesn't work. Meanwhile I discovered a strange behavior: trying to load a block for a store $cms_block = Mage::getModel('cms/block')->setStoreId($storeId)->load($blocks_to_active[$row]); and printing it I obtain two different array. If the block is enabled the result is an array full of information ([block_id], [title], [is_active] etc.) but if the block is disabled these data are not available. So, how can I load the info for a disasbled CMS block? Commented Aug 17, 2016 at 13:43
1
    $cmsBlock = Mage::getModel('cms/block')->getCollection()
        ->addStoreFilter($store->getId())
        ->addFieldToSelect('*')
        ->addFieldToFilter('identifier', $identifier)
        ->setPageSize(1)
        ->getFirstItem();

You need to use a collection to safely load cms blocks in Magento 1 (and 2 IIRC).

This is because Mage_Cms_Model_Resource_Block::_getLoadSelect does an is_active=1 check if you try and load it by store scope.

protected function _getLoadSelect($field, $value, $object)
{
    $select = parent::_getLoadSelect($field, $value, $object);

    if ($object->getStoreId()) {
        $stores = array(
            (int) $object->getStoreId(),
            Mage_Core_Model_App::ADMIN_STORE_ID,
        );

        $select->join(
            array('cbs' => $this->getTable('cms/block_store')),
            $this->getMainTable().'.block_id = cbs.block_id',
            array('store_id')
        )->where('is_active = ?', 1)
        ->where('cbs.store_id in (?) ', $stores)
        ->order('store_id DESC')
        ->limit(1);
    }

    return $select;
}

Using a collection allows you to bypass this hardcoded load select filtering.

Using a collection to load the CMS block does not trigger all the same observers etc that you would expect, so if you are planning on calling $cmsBlock->save() at the end of your modifications you will have to be sure to re-attach the store information.

$cmsBlock->setData('foo', 'bar');
$existingStoreIds = Mage::getModel('cms/block')->getResource()->lookupStoreIds($cmsBlock->getId());
$cmsBlock->setData('stores', $existingStoreIds);
$cmsBlock->save();

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.