update layout programmatically in magento event observer

后端 未结 5 982
陌清茗
陌清茗 2020-12-23 23:43

I am trying to change the template(view.phtml) of a block (product.info) for product detail page, to do this, I am observing an event (controller_action_l

5条回答
  •  清歌不尽
    2020-12-24 00:24

    I was going to comment on the fantastic answer by JBreton, but my particular use case which brought me to this thread is slightly different. (Also I'm an SO lurker and do not have adequate reputation to comment yet.)

    The accepted answer and other suggestions for modifying the layout in PHP code did not work for me, even after trying to observe various events, so I figured I'd post a steal/support/example answer on JBreton's side. My use case was to REMOVE blocks (core and custom module blocks) from the checkout_cart_index layout programmatically based on certain conditions. The method of using a custom layout handle works for ADDING blocks as well since it simply "activates" a new handle that Magento will process from a standard layout XML file in a theme.

    JBreton's method is the BEST from all of the ones that I tried. It makes more sense in the respect of current and future needs. Especially in the case where designers and template builders are not the same people who should be nosing around in the PHP code. Template people know XML and should be well familiar with Magento's layout XML system anyways. So using a custom handle to modify layouts on specific programmatic conditions is the superior method than adding XML through a string in PHP.

    AGAIN ... this is not a solution I conjured on my own ... I stole this from JBreton's answer above and am supplying example code which my doppelganger could use in their situation as an additional starting point. Note that not all of my module code is included here (notably the app/modules XML file, model classes, etc).

    My module's config file:

    app/code/local/Blahblah/GroupCode/etc/config.xml

    
      ... other config XML too ...
    
      
        
            
                
                    
                        singleton
                        Blahblah_Groupcode_Model_Ghost
                        checkout_cart_prepare
                    
                
            
        
      
    
    

    The observer's method in the class:

    app/code/local/Blahblah/GroupCode/Model/Observer.php

    _ghost_checkoutPermitted();
    
            if(!$checkoutPermitted)
            {
                // add a custom handle used in our layout update xml file
                Mage::app()->getLayout()->getUpdate()->addHandle($fullActionName . '_disable_checkout');
            }
    
            return $this;
        }
    

    The layout update inclusion in the theme file:

    app/design/PACKAGE/THEME/etc/theme.xml

    
    
        ...
    
        
            
                
                
                    blahblah--checkout_cart_index.xml
                
    
                ... other update references too ...
            
        
    
    

    The layout update definition file:

    app/design/PACKAGE/THEME/layout/blahblah--checkout_cart_index.xml

    
        
            
                
                
                
            
        
    
        ... other layout updates too ...
    
    

    (Yes, there is other code in my module which watches the checkout process events to ensure that someone doesn't sneak in with a manual URL path. And other checks are in place to truly "disable" the checkout. I'm just showing my example of how to programmatically modify a layout through an observer.)

提交回复
热议问题