5

I tried to update a widget instance as follows (where /PATH/TO and WIDGET_INSTANCE_ID should be replaced by the actual values:

require_once '/PATH/TO/app/Mage.php';
Mage::app();
$widgetInstance = Mage::getModel('widget/widget_instance');
$widgetInstance->load(WIDGET_INSTANCE_ID);
$widgetInstance->setData('title', 'foo');
$widgetInstance->save();

After executing this code, all layout updates of the widget have been removed.

What could be the cause of this?

5

The cause of this issue is that the format of the page_groups field needs a different format for saving than is used available after loading.

The format needed for saving can be generated from the format after loading by placing the following code somewhere between the load and save calls:

if ($widgetInstance->getData('page_groups')) {
    $formattedPageGroups = [];
    foreach ($widgetInstance->getData('page_groups') as $pageGroup) {
        $pageGroupName = $pageGroup['page_group'];
        $formattedPageGroups[] = [
            'page_group' => $pageGroupName,
            $pageGroupName => [
                'page_id' => $pageGroup['page_id'],
                'layout_handle' => $pageGroup['layout_handle'],
                'for' => $pageGroup['page_for'],
                'block' => $pageGroup['block_reference'],
                'entities' => $pageGroup['entities'],
                'template' => $pageGroup['page_template']
            ]
        ];
    }
    $widgetInstance->setData('page_groups', $formattedPageGroups);
}

Note that this is in essence the same issue as described in the post on copying a widget instance.

| improve this answer | |
1

I think the name of the property is 'page_template' and not 'template' at line:

'template' => $pageGroup['template']

should be

'template' => $pageGroup['page_template']

| improve this answer | |
  • You're right, this was an error in the sample code. I have now changed this in my answer. Thanks for reporting this. – Aad Mathijssen Jun 5 '19 at 11:54

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.