3

In a list page of Magento 2, I want to check that @ every product "Is it a child of any grouped product or not?" if yes then I want to change their URL link when I click on add-to-cart it should be redirected to its parent grouped product detail page.

Thank you.

3 Answers 3

2

Product relations in Magento is stored in catalog_product_relation table.

So you can check if the particular simple product has any data present in the table to check if it has any parent product.

SELECT * FROM catalog_product_relation WHERE child_id = <product_id>

If it has any parent product you can get the Product ID of parent to check the parent product is grouped and you can get the respective product page URL.

1

This is going to be very inefficient on none cache page or first load.

What you could do is create a product attribute (group_product_url) to store this URL and set it to include in the flat table.

Then create a cron module that re-indexes your DB and updates the attribute if the product is part of a group product.

In your cron module

foreach($collection() as $product){
    $parentIds = $product->getTypeInstance()->getParentIdsByChild($product->getId());
    //what product to choose if belong to multiple group products
    if($parentIds){
       //load parent to get url
       // $parent = ...
       //save url to product
       //$product->setData('group_product_url', $parent->getUrl())->save()
    }

}

Then on your product listing page

if($product->getData('group_product_url')){
    //redirect to group product page
}
else{
    //regular action
}

Magento\GroupedProduct\Model\Product\Type\Grouped

/**
* Retrieve Required children ids
* Return grouped array, ex array(
*   group => array(ids)
* )
*
* @param int $parentId
* @param bool $required
* @return array
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getChildrenIds($parentId, $required = true)
{
   return $this->productLinks->getChildrenIds(
       $parentId,
       \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED
   );
}

/**
* Retrieve parent ids array by requested child
*
* @param int|array $childId
* @return array
*/
public function getParentIdsByChild($childId)
{
   return $this->productLinks->getParentIdsByChild(
       $childId,
       \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED
   );
}

See https://www.mageplaza.com/get-parent-product-bundle-grouped-products-in-magento-2.html

1

Via object manager

 $parentId = 1234;
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $childIds = $objectManager->create('Magento\GroupedProduct\Model\Product\Type\Grouped')->getParentIdsByChild($parentId);
1
  • But this requires you to know the parent id??
    – Black
    Jan 21, 2021 at 16:52

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.