3

I am adding one custom field to payment and i want that field value in sales_place_order_before event

I am adding that custom field like this

checkout_index_index.xml

<?xml version="1.0"?>

 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="checkout.root">
        <arguments>
            <argument name="jsLayout" xsi:type="array">
                <item name="components" xsi:type="array">
                    <item name="checkout" xsi:type="array">
                        <item name="children" xsi:type="array">
                            <item name="steps" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="billing-step" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <item name="payment" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="payments-list" xsi:type="array">
                                                        <item name="children" xsi:type="array">
                                                            <item name="order-comment" xsi:type="array">
                                                                <item name="component" xsi:type="string">Namespace_Modulename/js/view/comment</item>
                                                                <item name="displayArea" xsi:type="string">before-place-order</item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
</body>
</page>

comment.js

define(
[
    'ko',
    'jquery',
    'uiComponent'
],
function (ko, $, Component) {
    'use strict';
    return Component.extend({
        defaults: {
            template: 'Namespace_Modulename/checkout/comment'
        }
    });
}
);

comment.html

<form data-role="order-comments" method="post">
  <label><strong>Order Comment</strong></label>
   <textarea class="input-text" name="order_comment" type="text"></textarea>
 </form>

place-order.js

define([
'jquery',
'mage/utils/wrapper'
], function ($, wrapper) {
'use strict';

return function (placeOrderAction) {
    /** Override default place order action and add agreement_ids to request */
    return wrapper.wrap(placeOrderAction, function(originalAction, paymentData, redirectOnSuccess, messageContainer) {

        var commentForm = $('.payment-method._active form[data-role=order-comments]'),
            commentData = commentForm.serializeArray();

        paymentData.extension_attributes = {order_comment: commentData};
        return originalAction(paymentData, redirectOnSuccess, messageContainer);
    });
};
});

requirejs-config.js

var config = {
config: {
    mixins: {
        'Magento_Checkout/js/action/place-order': {
            'Namespace_Modulename/js/model/place-order': true
        }
    }
}
};

extension_attribute.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\Quote\Api\Data\PaymentInterface">
    <!-- This is needed to provide type hint to serializer -->
    <attribute code="order_comment" type="string[]" />
</extension_attributes>
</config>

In observer file

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getEvent()->getOrder();
    $data = $order->getPayment()->getExtenstionAttributes()->getOrderComment();
    print_r($data);exit;
}

But Using this code its not giving any type of extenstion attribute how can i get this?

3

In order to load and save extension attributes, you should add a plugin on the Repository Service Interface, on the methods load/save. It should retrieve the value of extension attributes during save operation and make sure they are saved in database. On load, they should load data for extension attributes and populate the fields of extension attributes object. When the data from the Form is mapped to the Data Object, make sure that extension attributes are populated before persistence operation is called

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.