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

  • 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(); – gajjala sandeep May 23 '17 at 12:34
  • if you include the details in question instead of comment , there will be chances of getting solution..... – user52996 May 30 '17 at 9:15
  • Hi vickey, I added the solution which I got.. – gajjala sandeep Jun 2 '17 at 12:44
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(); 

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.