2

I want to add parent product to cart using child product id, it's configurable options in. Do like this

Get child id & using child id we can get parent product id.

Load the Configurable Attributes names of a product with it's(parent id). Then use In

$childid='15';$rep= $obj->create('Magento\Catalog\Model\ProductRepository'); $childObj=$rep->getById($childid); $aray[$att['attribute_id']]=$childObj->getData($attr->getDat‌​a('attribute_code'))‌​; now we have only child product configurableoptions $params2=array();$params2['selected_configurable_option'] = $childid;$params2['super_attribute'] = $aray;$params2['qty'] = 1; $this->cart->addProduct($parentid,$params2); $this->cart->save();

Note : Added the content from the comments to clear information

3
  • do like this*Get child id & using child id we can get parent product id.*Load the Configurable Attributes names of a product with it's(parent id).*then use In $childid='15';$rep= $obj->create('Magento\Catalog\Model\ProductRepository'); $childObj=$rep->getById($childid); $aray[$att['attribute_id']]=$childObj->getData($attr->getData('attribute_code')); now we have only child product configurableoptions $params2=array();$params2['selected_configurable_option'] = $childid;$params2['super_attribute'] = $aray;$params2['qty'] = 1; $this->cart->addProduct($parentid,$params2); $this->cart->save(); Commented May 23, 2017 at 12:34
  • if you include the details in question instead of comment , there will be chances of getting solution..... Commented May 30, 2017 at 9:15
  • Hi vickey, I added the solution which I got.. Commented Jun 2, 2017 at 12:44

1 Answer 1

2

I got solution from below code and it's worked for me..

 $childid='15'; $obj= \Magento\Framework\App\ObjectManager::getInstance(); $rep= $obj->create('Magento\Catalog\Model\ProductRepository'); $childObj=$rep->getById($childid); $aray[$att['attribute_id']]=$childObj->getData($attr->getDat‌​a('attribute_code'))‌​; //now we have only child product configurableoptions $params2=array(); $params2['selected_configurable_option']=$childid; $params2['super_attribute'] = $aray; $params2['qty'] = 1; $this->cart->addProduct($parentid,$params2); $this->cart->save(); 
1
  • its not worked, please check the solution again Commented Jan 27, 2020 at 11:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.