cancel
Showing results for 
Search instead for 
Did you mean: 

Add simple to grouped Error: Call to undefined method Magento\Catalog\Api\Data\ProductLinkExtension

Highlighted

Add simple to grouped Error: Call to undefined method Magento\Catalog\Api\Data\ProductLinkExtension

Hi I'm trying to add simple products on Grouped product, but I can't.

I see the following code but not works for me on magento 2.1.8 https://meetanshi.com/blog/programmatically-create-grouped-product-in-magento-2/

 

My code 

//recuperem a quin grouped correspon l'article actual, que sabem, cat?
        $grp = $this->_productCollectionFactory->create()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id', array('eq' => 'grouped'));
        $compsaonlineDate = date('d-m-Y', strtotime('-1 day', strtotime($this->_curdate)));
        /** @var \Magento\Catalog\Api\Data\ProductInterface $grouped*/
        foreach ($grp as $grouped) {
            $groupedModel = $this->productModel->load($grouped->getId());
            $sku = $groupedModel->getSku();
            try{
                $sql = "Select * from imp.ec_v_categorias3_a where primary_id like '".$sku."'";//and xfecha >= '$compsaonlineDate'
                $stmt = sqlsrv_prepare($this->_conn, $sql);
                if(!$stmt){
                    $intent++;
                    $this->_tryConnection(array($this,'setRefsToGrouped'),$intent);
                }
                $result = sqlsrv_execute($stmt);
                if(!$result){
                    $intent++;
                    $this->_tryConnection(array($this,'setRefsToGrouped'),$intent);
                }
                $position = 0;
                //$ids = array();
                $associated = $groupedModel->getProductLinks();
                while($reference = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){
                    $position++;
                    try {
                        //You need to load each product to get what you need in order to build $productLink
                        $linkedProduct = $this->productRepository->get($reference['xarticulo_id']);
                    } catch (\Magento\Framework\Exception\NoSuchEntityException $e){
                        $linkedProduct = false;
                    }
                    if($linkedProduct){
                        $stock = $linkedProduct->getData('quantity_and_stock_status');
                        /** @var Magento\Catalog\Model\ProductLink $productLink */
                        $productLink = $this->_productLinkInterface->setSku($sku)//sku of product group
                            ->setLinkType('associated')
                            ->setLinkedProductSku($linkedProduct->getSku())
                            ->setLinkedProductType($linkedProduct->getTypeId())
                            ->setPosition($position)
                            ->getExtensionAttributes()
                            ->setQty($stock['qty']);
                        //$ids[] = $linkedProduct->getId();
                        $associated[] = $productLink;
                        $this->_logger->info("setRefsToGrouped grouped:".$sku.' ref:'.$reference['xreferencia']);
                    }
                }
                if(count($associated)){
                    $groupedModel->setProductLinks($associated);
                    //$grouped->setAssociatedProductIds($ids);
                    $this->productRepository->save($groupedModel);
                }
            }catch (CouldNotSaveException $e) {
                $this->_logger->error("setRefsToGrouped ".$e->getMessage());
            } catch (InputException $e) {
                $this->_logger->error("setRefsToGrouped ".$e->getMessage());
            } catch (StateException $e) {
                $this->_logger->error("setRefsToGrouped ".$e->getMessage());
            } catch (NoSuchEntityException $e) {
                $this->_logger->error("setRefsToGrouped ".$e->getMessage());
            }catch (Exception $e){
                $this->_logger->error("setRefsToGrouped ".$e->getMessage());
            }
        }

When I execute the 

$this->productRepository->save($groupedModel);

 get the following exception: 

PHP Fatal error: Uncaught Error: Call to undefined method Magento\Catalog\Api\Data\ProductLinkExtension::getSku() in /var/www/embalajeonline.com/web/publico/vendor/magento/framework/Reflection/DataObjectProcessor.php:86
Stack trace:
#0 /var/www/embalajeonline.com/web/publico/vendor/magento/framework/Reflection/DataObjectProcessor.php(113): Magento\Framework\Reflection\DataObjectProcessor->buildOutputDataArray(Object(Magento\Catalog\Api\Data\ProductLinkExtension), '\\Magento\\Catalo...')
#1 /var/www/embalajeonline.com/web/publico/vendor/magento/framework/Api/ExtensibleDataObjectConverter.php(46): Magento\Framework\Reflection\DataObjectProcessor->buildOutputDataArray(Object(Magento\Catalog\Model\Product\Interceptor), 'Magento\\Catalog...')
#2 /var/www/embalajeonline.com/web/publico/vendor/magento/module-catalog/Model/ProductRepository.php(521): Magento\Framework\Api\ExtensibleDataObjectConverter->toNestedArray(Object(Magento\Catalog\Model\Product\Interceptor), Array, 'Magento\\Catalog...')

 

Can you help me? How can I add associated simple products to grouped product?