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

jsi*_*281 2 php static-block magento

我有一个静态块,我想通过脚本更新,通过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();
Run Code Online (Sandbox Code Playgroud)

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

小智 7

按块ID:

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

按标识符:

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


Dre*_*ter 6

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

或者如果您知道块ID:

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