Change requirejs-config.js following way:
Vendor/Module/view/adminhtml/requirejs-config.js
var config = {
"map": {
"*": {
"Magento_ConfigurableProduct/js/variations/steps/summary": "Vendor_Module/js/variations/steps/summary-custom",
}
}
}
Now create summary-custom.js file [Vendor/Module/view/adminhtml/web/js/variations/steps/summary-custom.js]
define([
'uiComponent',
'jquery',
'ko',
'underscore',
'mage/translate',
'Magento_ConfigurableProduct/js/variations/steps/summary'
], function (Component, $, ko, _,$t,summary) {
'use strict';
});
Run following command and clear cache.
php bin/magento setup:static-content:deploy
I'd like to go straight to your questions and then I'll try to make it clear on what you can actually do with the mixins plugin. So, first things first.
Implementation
The main thing here is the ability of any RequireJS plugin to completely take over the loading process of certain files.
This allows to modify the export value of a module before it will be passed as a resolved dependency.
Take a look at this sketchy implementation of what Magento custom mixins plugin is actually is:
// RequireJS config object.
// Like this one: app/code/Magento/Theme/view/base/requirejs-config.js
{
//...
// Every RequireJS plugin is a module and every module can
// have it's configuration.
config: {
sampleMixinPlugin: {
'path/to/the/sampleModule': ['path/to/extension']
}
}
}
define('sampleMixinPlugin', [
'module'
] function (module) {
'use strict';
// Data that was defined in the previous step.
var mixinsMap = module.config();
return {
/**
* This method will be invoked to load a module in case it was requested
* with a 'sampleMixinPlugin!' substring in it's path,
* e.g 'sampleMixinPlugin!path/to/the/module'.
*/
load: function (name, req, onLoad) {
var mixinsForModule = [],
moduleUrl = req.toUrl(name),
toLoad;
// Get a list of mixins that need to be applied to the module.
if (name in mixinsMap) {
mixinsForModule = mixinsMap[name];
}
toLoad = [moduleUrl].concat(mixinsForModule);
// Load the original module along with mixins for it.
req(toLoad, function (moduleExport, ...mixinFunctions) {
// Apply mixins to the original value exported by the sampleModule.
var modifiedExport = mixinFunctions.reduce(function (result, mixinFn) {
return mixinFn(result);
}, moduleExport);
// Tell RequireJS that this is what was actually loaded.
onLoad(modifiedExport);
});
}
}
});
The last and the most challenging part is to dynamically prepend the 'sampleMixinPlugin!' substring to the requested modules. To do this we
intercept define
and require
invocations and modify the list of dependencies before they will be processed by the original RequireJS load method.
It's a little bit tricky and I'd recommend to look at the implementation lib/web/mage/requirejs/mixins.js
if you wanna how it works.
Debugging
I'd recommend this steps:
- Make sure that the configuration for 'mixins!' plugin is actually there.
- Check that the path to a module is being modified. I.e. it turns from
path/to/module
to mixins!path/to/module
.
And the last but not least, requiresjs/mixins.js
has nothing to do with the main.js
or script.js
modules as they can only extend the configuration being passed from the data-mage-init
attribute:
<div data-mage-init='{
"path/to/module": {
"foo": "bar",
"mixins": ["path/to/configuration-modifier"]
}
}'></div>
I mean that the former two files don't mess with the value returned by a module, instead they pre-process configuration of an instance.
Usage Examples
To begin with I'd like to set the record straight that so called "mixins" (you're right about the misnaming) actually allow to modify the exported value of a module in any way you want. I'd say that this is a way more generic mechanism.
Here is a quick sample of adding extra functionality to the function being exported by a module:
// multiply.js
define(function () {
'use strict';
/**
* Multiplies two numeric values.
*/
function multiply(a, b) {
return a * b;
}
return multiply;
});
// extension.js
define(function () {
'use strict';
return function (multiply) {
// Function that allows to multiply an arbitrary number of values.
return function () {
var args = Array.from(arguments);
return args.reduce(function (result, value) {
return multiply(result, value);
}, 1);
};
};
});
// dependant.js
define(['multiply'], function (multiply) {
'use strict';
console.log(multiply(2, 3, 4)); // 24
});
You can implement an actual mixin for any object/function returned by a module and you don't need to depend on the extend
method at all.
Extending a constructor function:
// construnctor.js
define(function () {
'use strict';
function ClassA() {
this.property = 'foo';
}
ClassA.prototype.method = function () {
return this.property + 'bar';
}
return ClassA;
});
// mixin.js
define(function () {
'use strict';
return function (ClassA) {
var originalMethod = ClassA.prototype.method;
ClassA.prototype.method = function () {
return originalMethod.apply(this, arguments) + 'baz';
};
return ClassA;
}
});
I hope that this answers your questions.
Regards.
Best Answer
You may not use js mixin for the particular js file you try to override (i.e /vendor/magento/module-sales/view/adminhtml/web/order/create/scripts.js). Also, your require-config.js declaration is wrong but this not matter since mixin will not work for the js file /vendor/magento/module-sales/view/adminhtml/web/order/create/scripts.js
You may try below steps to override this AdminOrder class of the scripts.js file
I assume you are using a custom Module "Company_MyModule"
Step 1)
Create a xml file sales_order_create_index.xml under /app/code/Company/MyModule/view/adminhtml/layout
File : /app/code/Company/MyModule/view/adminhtml/layout/sales_order_create_index.xml
step 2)
Create a phtml file js.phtml under /app/code/Company/MyModule/view/adminhtml/templates/order/create
File : /app/code/Company/MyModule/view/adminhtml/templates/order/create/js.phtml
step 3)
Create js file scripts.js under /app/code/Company/MyModule/view/adminhtml/web/js/order/create to override AdminOrder class.
File: /app/code/Company/MyModule/view/adminhtml/web/js/order/create/scripts.js
step 4)
Clear your Magento Cache and alson remove adminhtml static files also.