cancel
Showing results for 
Search instead for 
Did you mean: 

Magento2: add custom quote extension attribute

Magento2: add custom quote extension attribute

Hi,

 

I am trying to add an extension attribute to a quote through a custom REST API, and I would like to retrieve this info later.

But when I try to get the value, is always null.

The code will explain better than me.

 

etc/extension_attributes.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\CartInterface">
    <attribute code="order_platform" type="string"/>
</extension_attributes>
</config>

 

I have checked in /var/generation that CartExtensionInterface exists and has the getOrderPlatform() and setOrderPlatform() methods.

 

Now, in a custom REST API I am trying to set this value:

      $quote = $this->quoteRepository->getActive($cartId);
      // [..] Quote validation tests here, it' fine
      $extensions = $quote->getExtensionAttributes();
      $extensions->setOrderPlatform("test");
      $quote->setExtensionAttributes($extensions);
      $this->quoteRepository->save($quote);

No errors given.

After this, I try to get back this value in another REST API call (but I will need to take the value even later in a plugin): 

      $quote = $this->quoteRepository->getActive($cartId);
      $extensions = $quote->getExtensionAttributes();
      if ($extensions->getOrderPlatform() != null) {
               // Sadly I never enter here!
      }

 No errors here, but the value I am looking for is always NULL.

 

Any help please?

 

Thanks a lot,

  Lorenzo

1 REPLY 1

Re: Magento2: add custom quote extension attribute

Hi, I think that in order to achieve that you need to place a plugin where you would pick up the data and save it somewhere (if it's another call since REST is supposed to be stateless) and another one for retrieving it on load.

 

If you want an example in the core you can check how stock_item is saved on product.