Magento - 以编程方式保存静态块内容

时间:2012-06-14 09:42:11

标签: php magento static-block

我有一个静态块,我想通过脚本更新,通过cron运行。

我已经找到了如何以编程方式创建或检索块,而不是如何编辑现有块。

这适用于检索块:

// Retrieve the layout object
$layout = Mage::getSingleton('core/layout');

// Generate a CMS block object
$block = $layout->createBlock('cms/block');

// Set the block ID of the static block
$block->setBlockId('my_block_id');

// Write the static block content to screen
echo $block->toHtml();

我想我在这里缺少一些简单的东西,但是在这个块上执行setContent()然后save()只会导致“Invalid method Mage_Cms_Block_Block :: save”

3 个答案:

答案 0 :(得分:7)

按块ID:

Mage::getModel('cms/block')->load($id)
  ->setData('content', 'Example content')
  ->save();

按标识符:

Mage::getModel('cms/block')
  ->getCollection()
  ->addFieldToFilter('identifier', 'my_block_id')
  ->load()
  ->setData('content', 'Example content')
  ->save();

答案 1 :(得分:6)

$identifier = 'footer_links';
Mage::getModel('cms/block')
    ->load($identifier, 'identifier')
    ->setData('content', 'Your new block content')
    ->save()
;

或者如果您知道块ID:

$id = 1;
Mage::getModel('cms/block')
    ->load($id)
    ->setData('content', 'Your new block content')
    ->save()
;

答案 2 :(得分:0)

使用magento脚本更新并添加静态块

function createBlock($blockData) {

$block = Mage::getModel('cms/block')->load($blockData['identifier']);
$block->setTitle($blockData['title']);
$block->setIdentifier($blockData['identifier']);
$block->setStores(array($blockData['storeId']));
$block->setIsActive($blockData['active']);
$block->setContent($blockData['content']);
$block->save();

}

请参阅我的博客,了解分步说明

http://www.pearlbells.co.uk/how-to-create-and-update-the-static-blocks-using-magento-script/