2

Can I create a widget instance and assign it to a specific page to appear through install script? I'd like to have some functionality similar to sample contents, but with widgets. To be more specific, my question is aimed at creating instances of widgets and placing them on certain pages programmatically through install script.

11

Its not pretty, but of course you can create a widget instance programmatically, for example in a setup script:

$widgetParameters = array(
    'param1' => 'This is some value from the widget form,
    'param2' => 'Some/other/value',
    'param2_2' => 'Rly?',
    'template' => 'this/is/the/template.phtml'
);

$instance = Mage::getModel('widget/widget_instance')->setData(array(
    'type' => 'your_module/your_widget',
    'package_theme' => 'default/theme', // has to match the concrete theme containing the template
    'title' => 'This is the Widget title',
    'store_ids' => '0', // or comma separated list of ids
    'widget_parameters' => serialize($widgetParameters),
    'page_groups' => array(array(
        'page_group' => 'all_pages',
        'all_pages' => array(
            'page_id' => null,
            'group' => 'all_pages',
            'layout_handle' => 'default',
            'block' => 'left',
            'for' => 'all',
            'template' => $widgetParameters['template],
        )
    ))
))->save();

This example sets the widget instance to display on every page in the left column.
If you want to specify different pages or target blocks, you need to update the values accordingly. I suggest looking what is set in Mage_Widget_Adminhtml_Widget_InstanceController::saveAction() and adjust the code above accordingly.

|improve this answer|||||
1

You can create custom module rather then script.you can refer this Magento official link to Creating a Magento Widget

http://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-a-magento-widget-part-1

http://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-a-magento-widget-part-2

|improve this answer|||||
  • 1
    Thank you, but I am comfortable with creating widget types. My question is aimed at creating instances of widgets and placing them on certain pages programmatically through install script. – dsplynm Dec 14 '13 at 12:55
  • still confused with your question. you already have widget and just you have to display using .phtml files? – Keyul Shah Dec 14 '13 at 12:59
  • @KeyulShah. I know this is an old thread, but for example. You have a development server with dozens of blocks. On go live to a new server, you don't want to have to remember to create all of them by hand in the new Database, just a way to have them already be there. – Rob Dec 10 '15 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.