Question

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"?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top