7

Following this post answer I have used mixins of shipping.js file to change the result of validateShippingInformation method. My requirers-config.js file is:

var config = {
 config: {
 mixins: {
 'Magento_Checkout/js/view/shipping': {
 'Vendor_checkoutShipping/js/shipping-method-mixin': true
 }
 }
 }};

and shipping-method-mixin.js file:

define([
'jquery',
'mage/utils/wrapper',], function (,ドル wrapper) {
'use strict';
return function (shipping) {
 var newMethod = wrapper.wrap(shipping.validateShippingInformation, function (originalFunction) {
 var result = originalFunction();
 return after(result);
 });
 console.log(newMethod);
 shipping.validateShippingInformation = newMethod; 
 return shipping;
};});

Problem I have: The file is read by browser when page is loaded (identified by breakpoints) and right then executed. But the method in original file that i tray to extend by using after method is executed by button click in front-end (why is the file not running after the original method is executed). I do not need the script I added to run when the page is loading(reloaded) and I do not want to override the original javaScript file.

I need to change validateShippingInformation value before the method setShippingInformation if sentence is being executed. The if sentence in shipping.js file is:

if (this.validateShippingInformation()) {.(I need to allow or disallow this code to run based on my additional logic. How can i do it?).}
asked Feb 7, 2017 at 14:01

2 Answers 2

8
+50

If you look at target file Magento/Checkout/view/frontend/web/js/view/shipping.js,you'll see that this is requirejs module which returns UiComponent (child of Magento_Ui/js/form/form).

In your example you have been used mage/utils/wrapper but this util applicable only for functions (like Magento/Checkout/view/frontend/web/js/action/create-billing-address.js).

For extending UiComponent you need to use base method extend:

var config = {
 config: {
 mixins: {
 'Magento_Checkout/js/view/shipping': {
 'Vendor_checkoutShipping/js/shipping-method-mixin': true
 }
 }
 }};

shipping-method-mixin.js

define([], function () {
 'use strict';
 return function (Component) {
 return Component.extend({
 validateShippingInformation: function () {
 var result = this._super();
 result = this._modifyResult(result);
 return result;
 }
 });
 }
});
answered Feb 9, 2017 at 21:41
2
  • Your answer was very helpful, but I thing that the answer would be more helpful to others and more complete if you replace this row return alert('validate') && this._super(); with a way to change the original method (after). I have had doubts to accept your answer because @David Verholen showed in his answer showed how can I do that Commented Feb 13, 2017 at 10:59
  • sure, i did it! Commented Feb 13, 2017 at 11:05
5

I'm not 100% sure, but I think mixins on ui components do not work as you are expecting it, since ui components return an ui component object which work slightly differtent, like further described here.

what you can do, is overriding the ui component (by creating the file in your theme like: app/design/Vendor/theme/Magento_Checkout/web/js/view/shipping.js) in your theme and extend it from the original ui component:

/*global define*/
define(['Magento_Checkout/js/view/shipping'], function (Component) {
 'use strict';
 return Component.extend({
 validateShippingInformation: function () {
 var originalResult = this._super();
 var newResult = this.doYourStuff(originalResult);
 return newResult;
 }
 });
 }
);

while you can of course add any object you need to the define function array

Siarhey Uchukhlebau
16.2k11 gold badges57 silver badges89 bronze badges
answered Feb 9, 2017 at 20:45
2
  • nope, mixins are applied for any requirejs modules, which return any value (component, function, object, text). Commented Feb 9, 2017 at 21:28
  • I agree while basically you are doing exactly the same using another overriding mechanism Commented Feb 9, 2017 at 21:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.