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.

asked Dec 14, 2013 at 11:52

2 Answers 2

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.

answered Dec 14, 2013 at 13:06
1
1
answered Dec 14, 2013 at 12:22
3
  • 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. Commented Dec 14, 2013 at 12:55
  • still confused with your question. you already have widget and just you have to display using .phtml files? Commented Dec 14, 2013 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. Commented Dec 10, 2015 at 21:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.