Saving a Quote in Magento 2.1 Since Save() is Deprecated

magento-2.1modelordersquote

I have created a new attribute for quotes and orders in Magento 2.1.5.

I have an observer that sets the value for this attribute based on a session variable (indentOrder).

The code I have is working:

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Logger\Monolog;
use Magento\Quote\Model\Quote;
use Wildcard\IndentOrders\Model\IndentOrders;

class QuoteSaveObserver implements ObserverInterface
{
    protected $indentOrders;

    protected $logger;

    public function __construct(IndentOrders $indentOrders, Monolog $logger) {
        $this->indentOrders = $indentOrders;
        $this->logger = $logger;
    }

    public function execute(Observer $observer)
    {
        // Check if is indent order or not
        if ($this->indentOrders->isIndent()) {
            $indentOrder = 1;
        } else {
            $indentOrder = 0;
        }

        /** @var Quote $quote */
        $quote = $observer->getQuote();

        $quote->setIndentOrder($indentOrder);
        $quote->save();
    }
}

I am concerned with the deprecation of the Magento\Quote\Model\Quote::save() method for future updates.

I have read Here that I should be using Module Service Contract. I read the link on that answer but it didn't make any sense as to how I can use this for my scenario.

Looking into the Magento_Sales code I can see that there is a model called QuoteRepository that contains a save method but this takes a \Magento\Quote\Api\Data\CartInterface as a parameter:

public function save(\Magento\Quote\Api\Data\CartInterface $quote)
{
    if ($quote->getId()) {
        $currentQuote = $this->get($quote->getId(), [$quote->getStoreId()]);

        foreach ($currentQuote->getData() as $key => $value) {
            if (!$quote->hasData($key)) {
                $quote->setData($key, $value);
            }
        }
    }

    $this->getSaveHandler()->save($quote);
    unset($this->quotesById[$quote->getId()]);
    unset($this->quotesByCustomerId[$quote->getCustomerId()]);
}

Can someone let me know if i am on the right track for this and help me with how to use the above method? With the same strength, can someone please let me know if I am way off track and help to put me on the correct one?

Best Answer

Class \Magento\Quote\Model\Quote implements \Magento\Quote\Api\Data\CartInterface so you can safely pass it to the repository object.

Related Topic