How to Use Javascript Mixins in Magento 2
Sometimes you want to extend some function or want to modify some data without overwriting javascript. In this article, I am going to explain to you how to use js mixins in Magento 2. Before we go with technical let me explain to you what is mixins and then we'll see how to use mixins in Magento 2.
What is Mixins?
According to adobe commerce guide Mixin is a class whose methods are added to, or mixed in, with another class.
A base class includes the methods from a mixin instead of inheriting from it. This allows you to add to or augment the behavior of the base class by adding different mixins to it.
How to use Javascript mixins in Magento 2?
To use mixins in Magento 2 please follow the below steps.
Step 1: Create requirejs-config.js at app/code/Vendor/CustomModule/view/frontend and paste the below code. You need to change code as per your requirement.
saveCopyzoom_out_mapvar config = { config: { mixins: { 'Magento_Checkout/js/action/set-shipping-information': { 'Vendor_CustomModule/js/action/set-shipping-information-mixin': true } } } };
In above code I've extended Magento_Checkout/js/action/set-shipping-information js file with my Vendor_CustomModule/js/action/set-shipping-information-mixin js. You'll extend any other js files according to your requirement.
Step 2: Create set-shipping-information-mixin.js file at app/code/Vendor/CustomModule/view/frontend/web/js/action to extend original function.
Here I just try to append some data to shipping address just for an example. You can write here your code according to your requirement.
saveCopyzoom_out_mapdefine([
'jquery',
'mage/utils/wrapper',
'Magento_Checkout/js/model/quote'
], function ($, wrapper, quote) {
'use strict';
return function (setShippingInformationAction) {
return wrapper.wrap(setShippingInformationAction, function (originalAction) {
var shippingAddress = quote.shippingAddress();
if (shippingAddress['customAttributes'] === undefined) {
shippingAddress['customAttributes']= {}
}
shippingAddress['customAttributes']['custom_data'] = "Custom Value";
//console.log(quote.shippingAddress());
return originalAction();
});
};
});
Now, open Command line in folder root of magento and run the below commands.
saveCopyzoom_out_mapphp bin/magento s:up php bin/magento s:s:d -f php bin/magento c:c
If you check console log quote.shippingAddress() in js then you will see custom_data is appened under the customAttributes with Custom Value.
That’s it,
We hope this Magento post helped you to know How to Use Javascript Mixins in Magento 2. Bookmark it for your future reference. Do comment below if you have any other questions or doubts.
P.S. Do share this post with your team.
AI-Powered Recommended Articles
How to Reload Total Using JS on Cart / Checkout Page in Magento 2
Learn how to dynamically reload cart totals and summary on the Magento 2 checkout page using JavaScript. Step-by-step guide to updating totals programmatically for seamless user experience.
How to Add Custom Validation Rule in Magento 2
Learn how to add custom validation rules in Magento 2 forms for data validation.
How to Create Custom Shipping Method in Magento 2
Learn how to create and integrate a custom shipping method in Magento 2 for customized shipping options.
Easiest Way to Override Component and Files in Magento 2 PWA Studio
Learn the easiest method to override components and files in Magento 2 PWA Studio for customized functionality.
How To Add Custom Field At Checkout Address Form – Magento 2
Learn how to add custom fields to the checkout address form in Magento 2 for enhanced customer data collection.
How to Add/Remove CSS and JS file in Magento 2
Learn how to add or remove CSS and JavaScript files in Magento 2 to manage your theme and layout.