0

I have a static block widget that is set to display only on specific products. I'm looking to programmatically add products to this list, but can't seem to find where the adminhtml module does this.

The name of the field in the fieldset is

widget_instance[0][all_products][entities]

but a search for this in the source code turns up nothing, so I think it is generated dynamically.

Can anyone point me in the right direction?

1 Answer 1

0

Here is a fully tested answer. The product ids are stored in the widget_instance_page table in the entities field. I am not a huge advocate of querying the db directly so I came up with this method using Magento's native code derived from the Mage_Widget_Model_Widget_Instance::_beforeSave($object) method.

<?php

$widgetInstanceId = 3; //change to the ID of your own widget
$productIdsToAdd  = array(237, 238, 239); //hypothetical product IDs to add

/** @var Mage_Widget_Model_Widget_Instance $model */
$model = Mage::getModel('widget/widget_instance')->load($widgetInstanceId);
if(is_array($groups = $model->getPageGroups())){
    foreach($groups as &$group){
        $entities = explode(',',$group['entities']);
        $entities = array_unique(array_merge($entities, $productIdsToAdd));
        $group['entities'] = implode(',',$entities);
        $group['block'] = $group['block_reference'];
        $group['template'] = $group['page_template'];
        $group['for'] = $group['page_for'];
        $group[$group['page_group']] = $group;
    }
}
$model->setData('page_groups',$groups);
$model->save();
1
  • @sadq3377 if my answer helped you out, please remember to mark it as accepted. Thank you! Commented Aug 22, 2016 at 18:37

Your Answer

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

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