
Never miss Magento tips, tricks, tutorials, and news.
Magento 2 is the leading E-commerce platform that provides improved scalability and flexibility for an online store.
An example of Magento 2’s flexibility is that store owners can customize and overwrite the core features of its framework according to the business requirements.
Thank you for subscribing.
Thank You.
Feel free to share the solution with Magento Community via social media.
If you have any doubts regarding this post, don’t be confused; just mention them in the Comments section below.
Method to Use Payment Extension Attributes in Magento 2
- Create registration.php file at appcodeVendorModule
use MagentoFrameworkComponentComponentRegistrar;ComponentRegistrar::register(ComponentRegistrar::MODULE, ‘Vendor_Module’, __DIR__); - Create module.xml file at appcodeVendorModuleetc
<?xml version=“1.0”?><config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”xsi:noNamespaceSchemaLocation=“urn:magento:framework:Module/etc/module.xsd”><module name=“Vendor_Module” setup_version=“1.0.0”/> - Create an extension_attributes.xml file at appcodeVendorModuleetc
<?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=“MagentoQuoteApiDataPaymentInterface”><attribute code=“custom” type=“string”/></extension_attributes> - Create a requirejs-config.js file at appcodeVendorModuleviewfrontend
var config = {config: {mixins: {‘Magento_Checkout/js/action/place-order’: {‘Vendor_Module/js/order/place-order-mixin’: true},‘Magento_Checkout/js/action/set-payment-information’: {‘Vendor_Module/js/order/set-payment-information-mixin’: true}}}; - Create place-order-mixin.js file at appcodeVendorModuleviewfrontendwebjsorder
‘jquery’,‘mage/utils/wrapper’,‘Vendor_Module/js/order/custom-assigner’], function ($, wrapper, ordercommentAssigner) {‘use strict’;return function (placeOrderAction) {return wrapper.wrap(placeOrderAction, function (originalAction, paymentData, messageContainer) {ordercommentAssigner(paymentData);return originalAction(paymentData, messageContainer);});}; - Create custom-assigner.js at appcodeVendorModuleviewfrontendjsorder
‘jquery’], function ($) {‘use strict’;return function (paymentData) {if (paymentData[‘extension_attributes’] === undefined) {paymentData[‘extension_attributes’] = {};}paymentData[‘extension_attributes’][‘custom’] = jQuery(‘[name=”ordercomment[custom]”]’).val();}; - Create set-payment-information-mixin.js at appcodeVendorModuleviewfrontendjsorder
‘jquery’,‘mage/utils/wrapper’,‘Vendor_Module/js/order/custom-assigner’], function ($, wrapper, ordercommentAssigner) {‘use strict’;return function (placeOrderAction) {return wrapper.wrap(placeOrderAction, function (originalAction, messageContainer, paymentData) {ordercommentAssigner(paymentData);return originalAction(messageContainer, paymentData);});}; - Create di.xml at appcodeVendorModuleetc
<?xml version=“1.0”?><config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”xsi:noNamespaceSchemaLocation=“urn:magento:framework:ObjectManager/etc/config.xsd”><type name=“MagentoCheckoutModelPaymentInformationManagement”><plugin name=“set_payment_data_before_save”type=“VendorModulePluginModelSavePaymentPlugin” sortOrder=“10”/></type><type name=“MagentoCheckoutModelGuestPaymentInformationManagement”><plugin name=“guest_set_payment_data_before_save”type=“VendorModulePluginModelGuestSavePaymentPlugin” sortOrder=“10”/></type> - Create SavePaymentPlugin.php at VendorModulePluginModel
namespace VendorModulePluginModel;use MagentoCheckoutModelPaymentInformationManagement;use MagentoQuoteApiDataAddressInterface;use MagentoQuoteApiDataPaymentInterface;class SavePaymentPluginprotected $quoteRepository;public function beforeSavePaymentInformationAndPlaceOrder(PaymentInformationManagement $subject,$cartId, PaymentInterface $paymentMethod,AddressInterface $billingAddress = null)$orderCustom = $paymentMethod->getExtensionAttributes();$objectManager = MagentoFrameworkAppObjectManager::getInstance();$quoteRepository = $objectManager->create(‘MagentoQuoteApiCartRepositoryInterface’);$quote = $quoteRepository->getActive($cartId);$custom = $orderComment->getCustom();$quote->setCustom(custom);$quote->save(); - Create GuestSavePaymentPlugin.php at VendorModulePluginModel
namespace VendorModulePluginModel;use MagentoCheckoutModelPaymentInformationManagement;use MagentoQuoteApiDataAddressInterface;use MagentoQuoteApiDataPaymentInterface;class GuestSavePaymentPluginprotected $quoteRepository;public function beforeSavePaymentInformationAndPlaceOrder(PaymentInformationManagement $subject,$cartId, $email, PaymentInterface $paymentMethod,AddressInterface $billingAddress = null)$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, ‘masked_id’);$quoteId = $quoteIdMask->getQuoteId();$orderCustom = $paymentMethod->getExtensionAttributes();$objectManager = MagentoFrameworkAppObjectManager::getInstance();$quoteRepository = $objectManager->create(‘MagentoQuoteApiCartRepositoryInterface’);$quote = $quoteRepository->get($quoteId);$custom = $orderComment->getCustom();$quote->setCustom(custom);$quote->save();
For example, the customers enter custom messages or comments while placing the order, and to save these comments, you need to pass it with the payment details using knockout JS.
The below solution is the method to use payment extension attributes in Magento 2 that allows saving the custom field values in the database.
The below method shows the same:
Something went wrong.
Magento 2 extension attributes are primarily containers used for adding an additional piece of information to our entities that allow adding additional custom data into an entity such as Product, Customer, Order, etc.
I would be glad to help.
Get Weekly Updates
One such requirement is to add custom fields into our entities. To serve this purpose, Magento 2 offers a flexible and declarative way to extend functionality that is called extension attributes.
Done!
Now you can easily save your additional field’s data after placing an order.
/**/
/*.mfp-bg{background:#000000d4}
/*]]>*/
/*.mfp-bg.mfp-ready{opacity:.6}
/*]]>*/