2

I'm trying to add a configurable product (and the chosen options) to the quote.

In Magento 1 you could use this:

$storeId = Mage::app()->getStore()->getStoreId(); // get current store id
$quote = Mage::getModel('sales/quote')->setStoreId($storeId)->load(4); //4 is your quote id
$params = array(
        'product' => 1,
        'super_attribute' => array(
            132 => 3,
            133 => 8
        ),
        'qty' => 1,
    );
$product = Mage::getModel('catalog/product')->load(1); //your config product is
$quote->addProduct($product, new Varien_Object($params))->save();

I haven't been able to find any help for Magento 2 though.

Anyone have a clue for issue?

*EDIT - This is the code I have tried based on Rakesh's answer below

<?php


namespace Sulman\AddConfigurableToQuote\Controller\Add;
use Magento\Catalog\Model\Product;

set_time_limit(0);

class Index extends \Magento\Framework\App\Action\Action
{
    protected $productFactory;
    protected $quote;
    protected $session;
    protected $product;
    protected $formKey;
    protected $cart;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Quote\Model\QuoteFactory $quote,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Framework\Data\Form\FormKey $formKey,
        \Magento\Checkout\Model\Session $session,
        \Magento\Catalog\Model\Product $product,
        \Magento\Checkout\Model\Cart $cart,
        array $data = []
    ) {
        $this->productFactory = $productFactory;
        $this->quote = $quote;
        $this->session = $session;
        $this->formKey = $formKey;
        $this->cart = $cart;
        $this->product = $product;
        parent::__construct($context,$data);
    }

    public function execute()
    {
        $product = $this->productFactory->create()->load(3); /* config product id */

        /* 136 is finish id and 137 is length attribute id */
        $requestInfo = new \Magento\Framework\DataObject(
            ['qty' => 1,'super_attribute' => array(136 => 4, 137 => 6)]
        );

        /** @var $quote \Magento\Quote\Model\Quote $quote */
        $quote = $this->session->getQuote();
        $quote->setStoreId(1);
        $quote->addProduct($product, $requestInfo);
        $quote->save();
    }
}
3
  • 4 and 6 is valid attribute option id? Oct 5, 2017 at 11:15
  • Yes that's correct.
    – sulman
    Oct 5, 2017 at 11:18
  • replace $quote = $this->session->getQuote(); with $quote = $this->quote->create(); Oct 5, 2017 at 11:59

2 Answers 2

0

This example is tested on Magento 2 with configurable options.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product');
$product->load(67);

/* 142 is size id and 93 is color attribute id */
/* 168 is S size and 49 is Black color id */
$requestInfo = new \Magento\Framework\DataObject(
    ['qty' => 1,'super_attribute' => array(142 => 168,93 => 49)]
);

/** @var $quote \Magento\Quote\Model\Quote $quote */
$quote = $objectManager->create('Magento\Quote\Model\Quote');
$quote->setStoreId(1);
$quote->addProduct($product, $requestInfo);
$quote->save();

Dont use objectmanager in production site use always prefer way like below using consturct() function,

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Quote\Model\QuoteFactory $quote,
    \Magento\Catalog\Model\ProductFactory $product,
    array $data = []
) {
    $this->product = $product
    $this->quote = $quote
    parent::__construct($context,$data);
}

public function createConfigQuote(){
    $product = $this->productFactory->create()->load(67); /* config product id */


    /* 142 is size id and 93 is color attribute id */
    /* 168 is S size and 49 is Black color id */
    $requestInfo = new \Magento\Framework\DataObject(
        ['qty' => 1,'super_attribute' => array(142 => 168,93 => 49)]
    );

    /** @var $quote \Magento\Quote\Model\Quote $quote */
    $quote =  $this->quote->create('Magento\Quote\Model\Quote');
    $quote->setStoreId(1);
    $quote->addProduct($product, $requestInfo);
    $quote->save(); 
}
3
  • Thanks. Doesn't seem to be working for me though. Magento version 2.1.7
    – sulman
    Oct 4, 2017 at 15:49
  • Can you please share your code which you have tried? Oct 5, 2017 at 5:57
  • Thanks Rakesh - I have added my code to the main question. Can you see where I have gone wrong?
    – sulman
    Oct 5, 2017 at 11:03
4

Try using this piece of code. your request info should have 'selected_configurable_option'(i.e simple product id). The code will be

    /* 136 is finish id and 137 is length attribute id */
    $requestInfo = new \Magento\Framework\DataObject(
        ['qty' => 1,'super_attribute' => array(136 => 4, 137 => 6),'product' => $item->getProduct()->getId(),'selected_configurable_option' => $simpleProductId]);

    /** @var $quote \Magento\Quote\Model\Quote $quote */
    $quote = $this->session->getQuote();
    $quote->setStoreId(1);
    $quote->addProduct($product, $requestInfo);
    $quote->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.