0

For a configurable product I want to get all related products of the linked simple products. How is it possible to merge multiple collections?

At the moment I overwrite the related products on each iteration of the foreach loop.

$product = $this->_coreRegistry->registry('product');
/* @var $product \Magento\Catalog\Model\Product */

if ($product->getTypeId() === 'configurable') {
    $simpleProducts = $product->getTypeInstance()->getUsedProducts($product);
}

$relatedProductCollection = false;
/* @var $simpleProduct \Magento\Catalog\Model\Product */
foreach ($simpleProducts as $simpleProduct) {
    /** @var $relatedProductCollection \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection */
    $relatedProductCollection = $simpleProduct->getRelatedProductCollection()->addAttributeToSelect(
        'required_options'
    )->setPositionOrder()->addStoreFilter();
}
2
  • You already getting collection's or product id's and you just want to merge ? I am correct Commented Mar 22, 2018 at 14:35
  • Yes, that's right. Commented Mar 22, 2018 at 14:39

1 Answer 1

1

I am assuming that you know how to get id as you said , here is logic :

1- Initialize an array for ex: $productids[] = $product->getId().

2- Store all product id into single array (you can also use array merge if you have id in different array's).

3- Then array_unique($productid);

create model class for product collection for example: app/code/Vendor/ModuleName/Model/Product.php

public function __construct(
        \Magento\Catalog\Model\Product $product
    )
{
    $this->product = $product;
}

public function getCollection($productIds)  //array of product id's
{
    $collection = $this->product->create()->getCollection()
      ->addFieldToFilter('entity_id', array('in' => $productIds))
    ->addAttributeToSelect('*');
    return $collection;
}
1
  • @tho Did this answer help you if not ? then kindly post your solution for this I am pretty convinced that there is plenty of ways to perform this operation. So if you have any share your idea . Commented Mar 23, 2018 at 9:09

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.