Dependecy error Magento 2 ('sectionLoadUrl' error)
You should use define in your jquery.cartAjax.js file instead of require, also pass $ as an argument so you can use it in the ajax call, require is specifically designed for phtml files and define for javascript files.
define([
'jquery',
'Magento_Checkout/js/action/get-totals'
], function ($, getTotalsAction) {
$(document).ready(function(){
$(".cart-container .form-cart .action.update").remove();
// On change handler
jQuery(document).on('change', 'input[name$="[qty]"]', function(){
console.log("quantity changed");
var form = jQuery(jQuery(this).closest('form'));
$.ajax({
url: form.attr('action'),
data: form.serialize(),
showLoader: true,
context: this.changedInput,
success: function (res) {
var parsedResponse = $.parseHTML(res);
var result = $(parsedResponse).find("#form-validate");
$("#form-validate").replaceWith(result);
var deferred = $.Deferred();
getTotalsAction([], deferred);
console.log('Ajax succes function completed');
},
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
console.log(err.Message);
}
});
}
);
});
});
hope it helpes
Update:
i think there's also somehting going wrong with the call to the file
it should look somehting like this:
<script>require(['ajaxQty']);</script>
where ajaxQty is the name declared in the require-config.js
Okay, I fixed it eventually, with help and thinking of Pascal Wientjes
So, when discussing about what the issue could be with said person, we came to the conclusion that customer-data.js
was loaded after cartAjaxQty.js
.
Even though I included the Magento_Customer
module in a sequence-tag within the module.xml
, still customer-data.js
still got loaded AFTER my script. I posted a question about this:
<sequence> not working?
Where other people reacted, and so Magento loads customer-data.js
via it's layout. So I had to figure out another way. Pascal told maybe you could use a $(document).ready
, so I tried but no luck. I thought.
This eventually fixed it: $(window).on("load", function ()
My script.phtml
eventually looks like this!
<script>
require ([
'jquery'
],
function ($) {
$(window).on("load", function () {
require([
'Vendor_AjaxCart/js/cartAjaxQty'
]);
});
});
And now all loads fine. It's not the fix I had hoped for, a more cleaner approach would've been my preference but it fixes my error.