I am trying to add multiple simple products that belong to a configurable product to the cart at the same time.

This is example code that should add 1 of each simple product (in my case 5 simples) belonging to a configurable product:

    $product = $this->product->load(12345); //get configurable product
    $simple_products = $product->getTypeInstance()->getUsedProducts($product); //get all simple products
    $attributes = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product); //get all possible attributes

    foreach ($simple_products as $simple_product) {
        /* get super attributes for simple product */
        $super_attributes = array();
        foreach ($attributes as $attribute) {
            $text = $simple_product->getAttributeText($attribute['attribute_code']);
            foreach ($attribute['values'] as $value) {
                if ($value['label'] == $text) {
                    $super_attributes[$attribute['attribute_id']] = $value['value_index'];

        $this->cart->addProduct($product, array(
            'qty' => 1,
            'super_attribute' => $super_attributes

What it does instead is adds 5 of the first simple product to the cart. I believe this is because $this->cart->addProduct is changing the value of $product from the configurable product to the simple product so for the next 4 add to carts it is adding the 1st simple product but I can't find any way to stop it doing it or setting product back to the configurable.

| improve this question | | | | |
  • Have you found any solutions for above issue? – Rakesh Jesadiya Apr 12 '16 at 11:53
  • I reported it as an issue on github as the above code should work. I ended up doing it using the API instead (Magento\Quote\Api\CartItemRepositoryInterface) – Alex Apr 12 '16 at 15:37
  • Hello Alex where have you written your code can you tell me. – Sarvesh Tiwari Jun 30 '17 at 6:14

Your Answer

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

Browse other questions tagged or ask your own question.