Programmatically remove block from layout

杀马特。学长 韩版系。学妹 提交于 2019-12-01 08:56:20

问题


I want to remove the product_options_wrapper block from the product view page according to the logedin user via frontend router controller.

I know that I can programmatically append a new block but I didn't find a remove function. :-(

Tried sth. like that

$this->getLayout()->unsetBlock('product_options_wrapper');

$this->getLayout()->getBlock('product.info')->remove('product_options_wrapper');

But nothing works.


回答1:


The OP code should work, if it used the correct block name, which is product.info.options.wrapper, as opposed to the block alias.

$this->loadLayout();
//e.g. 
if (Mage::getSingleton('customer/session')->getCustomerGroupId() == [id]){
     $this->getLayout()->unsetBlock('product.info.options.wrapper');
}
$this->renderLayout();



回答2:


Inorder to remove a block using its parent block use the code below

$this->getLayout()->getBlock('product.info')->unsetChild('product_options_wrapper');



回答3:


This should work:

    $blockName = 'left'; // Add yours
    $update = Mage::app()->getLayout()->getUpdate();
    $removeInstruction = "<remove name=\"$blockName\"/>";
    $update->addUpdate($removeInstruction);

Why? Have a look in the file Mage_Core_Model_Layout in the method generateXml() the XML is parsed and where a remove is set for a block, the attribute ignore is added to the block. In the method generateBlocks() all the blocks which have that attribute are not added.



来源:https://stackoverflow.com/questions/14275333/programmatically-remove-block-from-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!