0

I want to add a simple product to a grouped product. I tried a lot of different approaches, including direct database modifications, all without success. I'm guessing it should be something like this.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($groupedID); // Load Configurable Product
    $associatedProductIds = array($simpleID); //Product Ids Of Associated Products
    $product->setAssociatedProductIds($associatedProductIds);// Setting Associated Products
    $product->save();

the above gives no error. but also does not create a link.

0

I haven’t found a permanent solution for this problem yet, but for now I have used a direct SQL connection as described here: https://magento.stackexchange.com/a/105148/15548

$q = $this->_objectManager->get('\Magento\Framework\App\ResourceConnection');
$q->getConnection('core_write')->query("insert into catalog_product_link (linked_product_id, product_id,link_type_id) values (".$linked_product_id.",".$product_id.",".$link_type_id.")");

Where $link_type_id is 3 as this is the ID for grouped products.

Not the best way but for now It works. I'll update this when I find a better way.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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