2

I am trying to get a collection of custom link type called customtype along with related and upsell,In admin while click on a product it will throw an error like

Invalid arguments supplied for foreach() in module-catalog/Model/ProductLink/CollectionProvider.php line-51.

But in the case of related products in my case the below code return an empty array and there is no issue, why this is happening ?

public function getRelatedProducts()
{
    if (!$this->hasRelatedProducts()) {
        $products = [];
        $collection = $this->getRelatedProductCollection();
        foreach ($collection as $product) {
            $products[] = $product;
        }
        $this->setRelatedProducts($products);
    }
    return $this->getData('related_products');
}

Related product collection

/**
 * Retrieve collection related product
 *
 * @return \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection
 */
public function getRelatedProductCollection()
{
    $collection = $this->getLinkInstance()->useRelatedLinks()->getProductCollection()->setIsStrongMode();
    $collection->setProduct($this);
    return $collection;
}

My custom code is below,

 public function getCustomtypeProducts()
{
    if (!$this->hasCustomtypeProducts()) {
        $products = [];
        $collection = $this->getCustomtypeProductCollection();
        foreach ($collection as $product) {
            $products[] = $product;
        }
        $this->setCustomtypeProducts($products);
    }
    return $this->getData('customtype_products');
}

Collection provider

public function getCollection(\Magento\Catalog\Model\Product $product, $type)
{
    if (!isset($this->providers[$type])) {
        throw new NoSuchEntityException(__('Collection provider is not registered'));
    }

    $products = $this->providers[$type]->getLinkedProducts($product);
    $converter = $this->converterPool->getConverter($type);
    $output = [];
    foreach ($products as $item) {
        $output[$item->getId()] = $converter->convert($item);
    }
    return $output;
}

di.xml

 <type name="Magento\Catalog\Model\Product\LinkTypeProvider">
    <arguments>
        <argument name="linkTypes" xsi:type="array">
            <item name="alsobought" xsi:type="const">vendor\ProductLink\Model\Catalog\Product\Link::LINK_TYPE_ALSOBOUGHT</item>
            <item name="companion" xsi:type="const">vendor\ProductLink\Model\Catalog\Product\Link::LINK_TYPE_COMPANION</item>
        </argument>
    </arguments>
</type>
<type name="Magento\Catalog\Model\ProductLink\CollectionProvider">
    <arguments>
        <argument name="providers" xsi:type="array">
            <item name="alsobought" xsi:type="object">vendor\ProductLink\Model\Product\Link\CollectionProvider\Alsobought</item>
            <item name="companion" xsi:type="object">vendor\ProductLink\Model\Product\Link\CollectionProvider\Companionstyles</item>
        </argument>
    </arguments>
</type> 
2
  • Show full php code, also show getRelatedProductCollection() function Commented Nov 16, 2016 at 5:07
  • Have you solve this error ? Commented Feb 7, 2019 at 7:33

1 Answer 1

1

You can try with below code,

public function getRelatedProducts()
{
    if (!$this->hasRelatedProducts()) {
        $products = [];
        $collection = $this->getRelatedProductCollection(); 

        if($collection->count()) {
            foreach ($collection as $product) {
                $products[] = $product;
            }
        }

        $this->setRelatedProducts($products);
    }
    return $this->getData('related_products');
}
3
  • Still getting same error, do I need to add any dependencies more in di.xml, just look, I edited the code. Commented Nov 16, 2016 at 5:52
  • Have you remove var content? Commented Nov 16, 2016 at 5:54
  • yes, removed var and cache also cleared. Commented Nov 16, 2016 at 5:59

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.