1

We have complex products (of type "simple product") with associations between them. Most of our products therefore have accessories associated, which are displayed on the product detail page.

However, our accessories still are valid, viewable products on their own, meaning that one can find them using the search function. It would be beneficial for customers to see the products that the product they are currently viewing is associated from.

An example:

The products "A", "B" and "C" have the product "Z" as a product association. "Z" is displayed on each of their product detail pages.

So the question is:

Is there any way of finding a simple product to simple product association from "Z" to "A", "B" and "C", without loading all products and checking $product->getAssociatedProducts() manually for the ID of "Z"?

1

Lets assume product Z has ID 12 this should work to get an array of parent product IDs:

$productId = 12;
$parentIds = Mage::getModel('catalog/product_type_configurable')
    ->getParentIdsByChild($productId);

var_dump($parentIds);

More to read here: Programatically get parent ids of products of array


Edit:

For simple product relationships you can try this:

$productId = ...;
$type = Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED;
$related = Mage::getModel('catalog/product_link')->getCollection()
    ->addFieldToFilter('linked_product_id', $productId)
    ->addFieldToFilter('link_type_id', $type);

var_dump($related->getColumnValues('product_id'));

Other available types are LINK_TYPE_GROUPED, LINK_TYPE_UPSELL and LINK_TYPE_CROSSSELL.

  • Our products aren't configurable, just normal simple products. It seems that this method only works with configurable ones. I'll add this information to my post. – 0xCAFEBABE Aug 3 '17 at 13:13
  • @0xCAFEBABE updated answer. – sv3n Aug 3 '17 at 13:34
  • Tested it, it works well and is fast. Thank you. – 0xCAFEBABE Aug 4 '17 at 9:27

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.