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.
2 Answers 2
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.
-
can you please check this question magento.stackexchange.com/questions/117369/…Kingshuk Deb– Kingshuk Deb2016年05月28日 06:20:30 +00:00Commented May 28, 2016 at 6:20
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
-
1Thank 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– dsplynm2013年12月14日 12:55:55 +00:00Commented Dec 14, 2013 at 12:55
-
still confused with your question. you already have widget and just you have to display using .phtml files?Keyul Shah– Keyul Shah2013年12月14日 12:59:59 +00:00Commented 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.Rob– Rob2015年12月10日 21:52:06 +00:00Commented Dec 10, 2015 at 21:52