Question

I need to retrieve related products from a particular product with id.

I tried using the getRelatedProductCollection method unsuccessfully, not sure if this method is.

I need to get these related products to apply to other products programmatically. Anyone help me please?

try {

    $configProductIds = [1663]; //configurable product ids to update images

    foreach ($configProductIds as $product_id) {

        $configProduct = Mage::getModel('catalog/product')->load($product_id);

        $relatedProducts = $configProduct->getRelatedProductCollection()
            ->addAttributeToFilter('entity_id', array('in' => $product_id));

        echo "<pre>";
        print_r($relatedProducts->getData('product_id'));exit;
Was it helpful?

Solution

You can get the related products of a product by using the below code in your script.

try {

    $configProductIds = [1663];

    foreach ($configProductIds as $product_id) 
    {

        $configProduct = Mage::getModel('catalog/product')->load($product_id);

        // Get all related product ids of $product_id.
        $allRelatedProductIds = $configProduct->getRelatedProductIds();

        foreach ($allRelatedProductIds as $id) {
            $relatedProduct = Mage::getModel('catalog/product')->load($id);

            // get Product's name
            echo $relatedProduct->getName();

            // get product's short description
            echo $relatedProduct->getShortDescription()
        }
    }
} catch (Exception $e) {
    return $e;
}

Hope it helps!!!

OTHER TIPS

try {

    $configProductIds = [1663]; //configurable product ids to update images

    foreach ($configProductIds as $product_id) {

        $configProduct = Mage::getModel('catalog/product')->load($product_id);

        $relatedProducts = $configProduct->getRelatedProductCollection();

             // No need for this filter as from particular product object you will get related collection for this $product_id product.
            //->addAttributeToFilter('entity_id', array('in' => $product_id));



        echo "<pre>";
        print_r($relatedProducts->getData());exit;

Let me know if you still have any issue in it

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