Question

I have been looking into programmatically attaching children to their parent (grouped) products, but with the default quantity set as well. There seems to be little to no information on how to do more than attach a single product to a single parent, no quantity information involved.

Code I have that can attach products, albeit with a default quantity of 0:

$linkApi = new Mage_Catalog_Model_Product_Link_Api();
$linkApi->assign('grouped',$parent_id,$child_id);

There is an option $data variable, but I have yet to be able to format an array that would properly be accepted, even after using $linkApi->items('grouped',$id); as a baseline.

Another option is forgoing that part of the API and using the product model, but again not much luck in the way of documentation. Closest I have is this fragment I found, but it does not seem to work.

$grouped = Mage::getModel('catalog/product')->load($parent);
$relationData[$grouped->getId()] = array(
    'qty' => $qty,
    'position' => 0,
    'ids'      => $child,
);
$grouped->setGroupedLinkData($relationData);
try{ $grouped->save(); } catch() //Snipped for brevity

It saves without error, but no associated products appear. If anyone could assist, I would be most grateful.

Summarized:

  • Need to attach/group children products
  • Need to attach with a default quantity on a per-child basis
Was it helpful?

Solution

D'oh, I could have sworn I attempted this. But it seems I either did not, or I mistyped last time.

The solution to this problem is indeed the $data variable in the API call.

Solution:

$linkApi->assign('grouped', $id, $id_to_attach, array('qty' => $qty, 'position' => 0));
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top