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();