The goal is to add a piece of JS to manipulate a date picker on the checkout page. I use an OSC (OneStepCheckout from mageplaza). So far I've followed this documentation and added this mixin:
var config = {
config: {
mixins: {
'Magento_Checkout/js/action/set-shipping-information': {
'Vendor_Module/js/action/set-shipping-information-mixin': true
},
}
}
}
in this file:
define([
'jquery',
'mage/utils/wrapper',
], function (,ドル wrapper) {
'use strict';
// the html is not there so this is empty
var datepicker = $('#datepicker')
return function (setShippingInformationAction) {
return wrapper.wrap(setShippingInformationAction, function (originalAction) {
// ... some custom code ... //
return originalAction();
});
};
});
When I load the page the date picker var has no HTMLElements, the reason is that the checkout is loading all the checkout components. So how can I tell the mixin to wait until the checkout components are completely done/loaded?
Maybe it shouldn't be a mixin?
2 Answers 2
Try using like below
require([
'jquery',
'Magento_Ui/js/lib/view/utils/dom-observer',
], function (,ドル$do) {
$(document).ready(function(){
$do.get('.class-name', function(elem){
$(elem).addClass('test-class');
//your code
});
});
});
-
This works for me, +1.giangvdm– giangvdm2023年10月13日 11:04:11 +00:00Commented Oct 13, 2023 at 11:04
You can wait for the DOM to be ready with this !domReady requireJs utility.
define([
'jquery',
'mage/utils/wrapper',
'!domReady'
], function (,ドル wrapper) {
'use strict';
var datepicker = $('#datepicker')
return function (setShippingInformationAction) {
return wrapper.wrap(setShippingInformationAction, function (originalAction) {
// ... some custom code ... //
return originalAction();
});
};
});
However due to knockout templates loading asynchronously via knockout this may not work.
I would try initializing datepicker in the wrapper itself. Something like so:
define([
'jquery',
'mage/utils/wrapper',
'!domReady'
], function (,ドル wrapper) {
'use strict';
var datePickerFunc = function() {
var datepicker = $('#datepicker');
...do datepicker things....
};
return function (setShippingInformationAction) {
return wrapper.wrap(setShippingInformationAction, function (originalAction) {
datePickerFunc();
// ... some custom code ... //
return originalAction();
});
};
});
None of this is tested.
-
-
The mixin is working (as far as I can tell). I placed the console logs in both functions but only the
return function (setShippingInformationAction)logs something. But I think that is correct because thesetShippingInformationActionisn't triggered yet.Katch– Katch2018年08月21日 16:37:41 +00:00Commented Aug 21, 2018 at 16:37 -
Maybe this mixin isn't the right place? Maybe it shouldn't be in a action but model or something else perhaps?Katch– Katch2018年08月21日 16:46:02 +00:00Commented Aug 21, 2018 at 16:46
-
This is the full source. What more should I provide?Katch– Katch2018年08月21日 16:57:54 +00:00Commented Aug 21, 2018 at 16:57
-
@Katch Did you find any solution for this ?Juliano Vargas– Juliano Vargas2020年05月26日 10:25:40 +00:00Commented May 26, 2020 at 10:25
Explore related questions
See similar questions with these tags.