4

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;
4

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!!!

0
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

  • Thanks for your help. This code is listing all ecommerce products and I need it to list only products related to a specific id product. – Marcos Vinicius Sep 23 at 22:28

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.