1

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?

2

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

  • Just to make sure I have this correct, I just use \Magento\Quote\Model\QuoteRepository::save($quote) Despite $quote being of type \Magento\Quote\Model\Quote? – Wildcard27 Apr 29 '17 at 1:01
  • Yes, when you look into that class the definition is class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\CartInterface so it is compatible with save method in the repository class. – Zefiryn Apr 29 '17 at 10:19
  • Not sure if I am allowed to ask you to, but are you able to let me know what might be going on here magento.stackexchange.com/questions/173368/… – Wildcard27 May 8 '17 at 3:48
1

If the service contract does not exist, use resource model to save the object.

\Magento\Quote\Model\ResourceModel\Quote::save(\Magento\Framework\Model\AbstractModel $object)
  • Cool. I assume I have to use \Magento\Quote\Model\ResourceModel\Quote::load($quote->getId()), make changes and then save()? – Wildcard27 Apr 28 '17 at 5:46
  • Well, I do not think that is a good idea. You are inside an observer, and there might be unsaved changes in the quote object. If you load it and save it again it will reset to the original state with your change. – B G Kavinga Apr 28 '17 at 5:51

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.