7

I inserted a custom column in quote table like this:

if (version_compare($context->getVersion(), '1.0.1', '<')) {
          $setup->getConnection()->addColumn(
              $setup->getTable('quote'),
              'custom_column',
              [
                  'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                  'nullable' => true,
                  'comment' => 'Custom Column',
             ]
          );
        }

The custom column was successfully created, and added to every single row in quote table with null value, but the problem is, i don't know how to update the custom column data in the quote table by using only quote id

2 Answers 2

7

The easiest way to solve this is to fill in this field in the quote model and save it using the Repository:

class QuoteUpdater
{
    /**
     * @var \Magento\Quote\Model\QuoteRepository
     */
    protected $quoteRepository;

    /**
     * @param \Magento\Quote\Model\QuoteRepository $quoteRepository
     */
    public function __construct(
        \Magento\Quote\Model\QuoteRepository $quoteRepository
    ) {
        $this->quoteRepository = $quoteRepository;
    }

    public function updateQuoteData($quoteId, int $customData)
    {
        $quote = $this->quoteRepository->get($quoteId); // Get quote by id
        $quote->setData('custom_column', $customData); // Fill data
        $this->quoteRepository->save($quote); // Save quote
    }
}
3
  • How to get the $customData in this repository if it's a form field in addtocart.phtml?
    – Swetha
    Commented Apr 12, 2018 at 6:13
  • 1
    greate job using service contract!
    – HaFiz Umer
    Commented Aug 31, 2019 at 20:26
  • The \Magento\Quote\Api\CartRepositoryInterface interface should be injected, instead of injecting the \Magento\Quote\Model\QuoteRepository which is not backwards compatible. Commented Jun 16, 2020 at 8:05
2

To set data in quote table, use below code -

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$quoteId = 10;
$customColumnValue = 1; 
$quote = $objectManager->create('Magento\Quote\Model\Quote')->load($quoteId);
$quote->setCustomColumn($customColumnValue);
$quote->save();

Here replace $quoteId variable with your quote ID .

3
  • 1
    You should not use the ObjectManager directly Commented Jun 5, 2017 at 11:11
  • 1
    Yes, this is just for an example. We should Inject Object Manager in Constructor first then can use its object in method. 1. declare object: private $_objectManager; 2. Inject in constructor and initialize : public function __construct( \Magento\Framework\ObjectManagerInterface $objectmanager ) { $this->_objectManager = $objectmanager; }
    – InfoBeans
    Commented Jun 7, 2017 at 11:56
  • Unless its in a plugin..
    – DWils
    Commented Aug 24, 2018 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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