1 Comment

create a block from cron – layout seem different

Magento 2 docker install: What is my admin user and password.
Retrieve custom variable in custom module

I need to create a block from a cronjob that render products grid (yes I know it sound weird), and get the output html. To do so I created a custom block with template and tried to use it like this (reminder: from a cronjob):

$block = Mage::app()->getLayout()->createBlock('my_special_block')
                        ->setTemplate('my_special_template.phtml');
Mage::log($block->toHtml());

This returned the html of a product grids with products in it, but they looked nothing like the actual site products. So I tried using this code in a controller, something like this:

$block = $this->getLayout()->createBlock('my_special_block')
                            ->setTemplate('my_special_template.phtml');
echo $block->toHtml();

And indeed the html I get when going to the controller url is totally different than the one I got from the cron.

My guess is that when I get the layout in the cron, Magento don’t load all the layout updates or something like that. but I couldn’t find info in the docs of how to do it. Or maybe its a different problem?

Any solution would be appreciated, I just need to get my hands on the correct html from inside the cron.

EDIT:
Tried adding these lines before creating the block in the cronjob, but the html still looks different. :/

    Mage::app()->setCurrentStore(0);
    $layout = Mage::app()->getLayout();
    $layout->getUpdate()->addHandle('default')->load();

Thanks!

Related Posts

  • swatches on custom page (1)
    I'm trying to load a product view into another page, here is how I'm trying to achieve <?php Mage::register('product',$product);?> <?php $customview = […]
  • Magento2 Add Login/Logout Button homepage (1)
    I have a home landing page that has a Login/Logout button. It's currently static html, but I'm trying to convert it so that the button actually functions same as default login/logout button. I tried creating a custom template landing-content.phtml and updating my Layout Update XML […]
  • Do we need to use loadlayout() in an action that we are calling through Ajax? (2)
    In indexAction of my custom module's controller, I have called loadlayout() and renderlayout(). I am now calling an ajaxAction through ajax from a template file. Do I need to use loadlayout() again? Should it be loadlayout(false)? Edit: I intend to display a list of products […]
  • Block not showing after layout update (1)
    Considering this layout update XML: <?xml version="1.0"?> <layout version="0.0.1"> <checkout_cart_index> <reference name="methods"> <block type="mhquote/request_link" name="mhquote.request.link" as="mhquoteRequestLink" before="-" […]
  • Block not showing after layout update (1)
    Considering this layout update XML: <?xml version="1.0"?> <layout version="0.0.1"> <checkout_cart_index> <reference name="methods"> <block type="mhquote/request_link" name="mhquote.request.link" as="mhquoteRequestLink" before="-" […]
  • Programatically create phtml files (1)
    I'm writing a magento extension and I need to have a phtml template file that is fetched from an external source. It can change remotely. Currently I'm using http requests to get the file, and then php file access (file_put_contents()) to write it. However this method sound kinda […]
  • M2: Is there a faster way to Update Frontend Themes? (1)
    When updating frontend components in Magento 2, meaning Templates as phtml, html templates for Knockout, CSS files, Less Files, Fonts, Images etc. I can only get this to work by Flushing the /var and /pub/static folders running php bin/magento setup:static-content:deploy for […]
  • M2: Is there a faster way to Update Frontend Themes? (1)
    When updating frontend components in Magento 2, meaning Templates as phtml, html templates for Knockout, CSS files, Less Files, Fonts, Images etc. I can only get this to work by Flushing the /var and /pub/static folders running php bin/magento setup:static-content:deploy for […]
  • Magento custom admin module – use template instead of grid? (1)
    I need to create a custom admin module and I want to use a template file instead of using a grid. Can someone give me an example of how I would do this. Not sure if it is possible to do this in the controller or if I still need a block and then reference the template there?
  • setColumnCount won't display wrong count of products. (0)
    I have created a new template for my magento backend, now i want to set its layout grid to display 2 products per row, however when i do <action method="setColumnCount"><count>3</count></action> in my catalog.xml it shows 3 but when i give it <action […]
Magento 2 docker install: What is my admin user and password.
Retrieve custom variable in custom module

1 Comment on this article

  • User November 12, 2016

    Magento don’t load all the layout updates or something like that.

    Of course not, because why should Magento load frontend layout updates in the cron? But that’s not your problem, because you don’t use any of the layout updates (i.e. the XML that you would load with loadLayout() in the controller), but create a standalone block.

    Setting a store is not the worst idea, but do not use the admin store (id 0):

    Mage::app()->setCurrentStore(0);
    

    But instead the default frontend store (or another specific one, depends what you need):

    Mage::app()->setCurrentStore(1);
    

    Additionally, you might need to set the frontend area:

    Mage::getDesign()->setArea('frontend');
    

    Alternative

    One possible problem with this approach is that in the same process, other cronjobs will be executed and setting the store might have side effects on them.

    There is another option, which is also used to render emails sent from cron or admin: Store Emulation

    Start Emulation:

    $storeId = 1;
    
    $initialEnvironmentInfo = Mage::getSingleton('core/app_emulation')->startEnvironmentEmulation($storeId);
    Mage::getDesign()->setStore($storeId);
    Mage::getDesign()->setTheme(Mage::getStoreConfig('design/theme/default', $storeId));
    

    Stop Emulation:

    Mage::getSingleton('core/app_emulation')->stopEnvironmentEmulation($initialEnvironmentInfo);
    

    Reply

Add a comment