1

I am trying to get the related products from a specific product using the function getRelatedProducts.

$relatedProductsArray  = $productModel->getRelatedProducts();

if($relatedProductsArray && count($relatedProductsArray) > 0){
   foreach($relatedProductsArray as $_relatedProduct){
      ...
   }
}

It works when the related products are assigned directly to the product in the admin product details. However, when the related products are created from Related Products Rules, the function returns to me an empty array.

What is the right way to get the related products assigned to a product from a rule?

1 Answer 1

0

The catalog target rules operate totally independently from the related product links as you discovered. They are based on an indexing-based system so they don’t update in real-time.

The related products produced by a catalog target rule get added to a product’s detail page with the \Magento\TargetRule\Block\Catalog\Product\ProductList\Related block which retrieves the related products and displays them in Magento_Catalog::product/list/items.phtml by calling the getAllItems() method on the block.

So your option is to move that block instance in your layout to the place you want to render it, or add an instance of that block to your layout somewhere by referencing how it is done in Magento_TargetRule/view/frontend/layout/catalog_product_view.xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.