Skip to content

How to handle extension attributes? #4302

@marvincaspar

Description

@marvincaspar

I want to extend the payment object with 2 new attributes.

etc/extension_attributes.xml

    <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\OrderPaymentInterface">
            <attribute code="payment_token" type="string"/>
            <attribute code="transaction_token" type="string"/>
        </extension_attributes>
    </config>

I created an update script to add both new attributes to the sales_order_payment table. If I now set the extension attributes, they are not stored in the database.

    $orderPayment = $order->getPayment();

    // Set payment/transaction token to extension attributes
    $paymentExtensionAttributes = $orderPayment->getExtensionAttributes();
    if (is_null($paymentExtensionAttributes)) {
        $paymentExtensionAttributes = $this->extensionAttributesFactory->create('Magento\Sales\Model\Order\Payment');
    }

    $paymentExtensionAttributes->setPaymentToken("some-payment-token");
    $paymentExtensionAttributes->setTransactionToken("some-transaction-token");
    $orderPayment->setExtensionAttributes($paymentExtensionAttributes);
    $order->setPayment($orderPayment);
    $order->save();

I would expect that both attributes are automatically stored in the database table.

For testing I set the fields in the database and run $orderPayment->getExtensionAttributes() and got null as response. I thought that they are automatocally loaded.

Is my understanding from the extension attributes wrong? If yes, how I have to handle extension attributes?

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions