0

I tried getting the checkout fields attributes which are included with the Swissup Firecheckout ... the developer said its possible with $order->getExtensionAttributes();.

So I created some throw away code to test, like this:

$orderId = 12345;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);
var_dump($order->getExtensionAttributes()); // does NOT work
var_dump($order->getIncrementId()); // does work

Nothing is dumped except for the increment id of the order. I am sure the order has some checkout fields set ...

Am I missing something or whats wrong here?

Thanks!

1
  • first check var_dump($order->getData()); , you will get more data in array Commented Jan 30 at 11:51

1 Answer 1

0

This is the correct method to fetch the extension attributes from the order.

If you are unable to retrieve the extension attributes, it could be due to other reasons. It's possible that event/plugin/classes related to the extension attributes are not working properly.

I have followed the steps/code below and it appears to be functioning properly without any problems. I am able to obtain the correct extension attribute values with your code.

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\Sales\Api\Data\OrderInterface">
        <attribute code="additional_charges1" type="decimal" />
        <attribute code="additional_charges2" type="decimal"/>
    </extension_attributes>
</config> 

events.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_load_after">
        <observer name="sales_order_load_attribute" instance="[Namespace]\[ModuleName]\Observer\OrderLoadAfter"/>
    </event>
</config>

sales_order_load_after Observer:

<?php

namespace [Namespace]\[ModuleName]\Observer;

use Magento\Framework\Event\ObserverInterface;

class OrderLoadAfter implements ObserverInterface
{
    protected $_orderExtension;

    public function __construct(\Magento\Sales\Api\Data\OrderExtension $orderExtension)
    {
        $this->_orderExtension = $orderExtension;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getOrder();

        $extensionAttributes = $order->getExtensionAttributes();
        if ($extensionAttributes === null) {
            $extensionAttributes = $this->_orderExtension;
        }
        $additional_charges1 = 52;
        $additional_charges2 = 51;
        $extensionAttributes->setData('additional_charges1', $additional_charges1);
        $extensionAttributes->setData('additional_charges2', $additional_charges2);
        $order->setExtensionAttributes($extensionAttributes);
    }
}

Got below response after using your code:

object(Magento\Sales\Api\Data\OrderExtension)[1242]
  protected '_data' => 
    array (size=6)
      'additional_charges1' => int 52
      'additional_charges2' => int 51
      'applied_taxes' => 
        array (size=0)
          empty
      'item_applied_taxes' => 
        array (size=0)
          empty
      'shipping_assignments' => 
        array (size=1)
          0 => 
            object(Magento\Sales\Model\Order\ShippingAssignment)[1244]
              ...
      'payment_additional_info' => 
        array (size=1)
          0 => 
            object(Magento\Payment\Model\PaymentAdditionalInfo)[1263]
2
  • Thanks for you time! Is there not another way to get the values? It doesnt work in my case ... Commented Jan 30 at 8:23
  • Are you not able to get the value if we pass the field name under the getData() like this $order->getData('fieldname'); Commented Jan 30 at 8:50

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.