Override the Minicart

So far I have altered some of the behavior of the Minicart using mixins. This usually means adding more properties and behavior to an already present functionality. With mixins, the properties and behavior of two different objects are merged and you end up enhancing your object in a way that is not possible via classical inheritance principles.

This given, deciding how many items can be shown in a given moment is not mixing up properties, since, well, the maxItemsToDisplay has always been there, I just updated its value. This is practically overriding and I’ve used mixins for it.

In this article here we will be overriding the minicart component. Lets get started.

I assume you are the 0.2.0 version of the program. If you have loaded perhaps the home page of your application (or any other page except the checkout) and have added one break-point at this line

https://gist.github.com/marjan7790/7693e5aed1d9d053d6d46bbbe25a84a1

you will see the data behind the component may look like

myobj-after-super-mixins

In the case of the screenshot, the component is: “Magento_Checkout/js/view/minicart”.

So, lets do a couple of file system changes. First, change view/frontend/requirejs-config.js content to

https://gist.github.com/marjan7790/e1eddbae663c6bad7ec726136fc4f4da

OK, so, this code practically says we do not need requirejs-config.js at all!

But, I thought you might want to keep the old code commented out just as a reminder to the mixins syntax, and if you would keep the file, well, it needs to have var config in it. An empty one, but, has to have it.

If you do not want to keep the old mixins config, simply remove the file.

Next, edit the minicart.js to look like

https://gist.github.com/marjan7790/2d6dd7bd0bfc37b2412e847766cf2754

In the define, a dependency is added to the original minicart and that compoenent is now internally here referred to as Minicart. We extend it and return it with return Minicart.extend({.

The rest of the code, you have noticed, especially the initialize() function, is exactly the same!

We can’t stop here. This component won’t be loaded. There is an .xml file that dictates the js loading. In our case the original file is

https://gist.github.com/marjan7790/e10b6aef3f6674555da55c0abd839abe

so, we need to override it. Create default.xml file inside view/frontend/layout in your module with the following content

https://gist.github.com/marjan7790/ee7ee850e1642d4bb96f8d6017b026a0

Find that line where the compoent is specified and edit Vendor_Yourmodule accordingly to your module.

So, re-deploy static content, clear magento cache and browser cache and you are done!

Set a brake point just like earlier and notice that the minicart component name is not the default one!

As one extra thing, we can deploy our own .html template file. First create file content.html inside view/frontend/web/template/ folder of your extension with this content

https://gist.github.com/marjan7790/f9360e831f7af1f36d3f34dad4411c46

This is basically the original file, but I have added one single line

https://gist.github.com/marjan7790/4b45b98905b1229997b4286b7ff24196

Since new .html file is used, make sure that the default.xml specifies your file instead of the default one. Do this over here

https://gist.github.com/marjan7790/fe5f415ef37f85911346859f0d0e7075

Once you are done, a mere magento cache flush should sufice it, and you should see the Maximum items to display 1 message at the top of the minicart container.

You can always check the code from the 0.3.0 tag for comparison.


Notes and Conclusion

Note:

<span text=”some-js-variable-here” /> that is added to the content.html is NOT available by default Knockout.

Something like

<span data-bind=”text: maxItemsToDisplay”>

is probably the normal Knockout syntax.

Magento has simply taken Knockout a step further adding one more layer of abstraction here. By not clearly listing all of these specifics, Magento does make these things one level harder for the community.

Anyway, we have now the mini-cart overridden.

As a summary, to achieve this
– we don’t need to create requirejs-config.js at all to override a js component
– we do need to override a native layout .xml file
– we could specify our own .html template

Addressing finally the elephant in the room: initialize() is completely the same both in using the mixins and the override approach. Well, we are anyway returning some-object.extend(), and it is the same object, we merely use different references for it. And in both cases our code executes before the original initialize() does. So, naturally, the code stays the same.

This last one can be very instructive: if the code from version 0.3.0 is claimed to be a better solution to the requirements, it is only because it satisfied and acknowledges the well established classical idea/concept of overriding. It does not work better, and you’ve already tackled a default.xml file which you did not had to do it 0.2.0.

Thus, almost always ask yourself: is an established concept always worth applying?

I’d hate to leave you with such philosophical questions, so, lets continue and extend this series and take a look at the question: where the data is really coming from.

 

 

Pass our own server data to the Minicart

From the previous post how server side data is being passed to the Minicart, you remember that Magento uses window.checkout object to configure the original minicart.js component. And I had an idea to modify the window.checkout object inside minicart.phtml, which seems rather straightforward and nice.

While this is all practical, lets try to figure out a way to pass data to our component in a way that the value is actually a property of the component.

If you are not the type that likes too much experiments, I promise, this is that type of article that has high chances of giving a headache, so, now is the moment to go back to your favorite series :). For the rest of you, lets start playing around with Magento by opening

https://gist.github.com/marjan7790/d6331447c83d420014d22c4a05a30ca2

and take a look at this code more carefully

https://gist.github.com/marjan7790/36fe1702a71ab2bc710eeea5c57ac983

In essence, this says:

Pass a JSON object to Magento_Ui/js/core/app

Practically, Magento_Ui/js/core/app is alias to app.js, which is found in vendor/magento/module-ui/view/base/web/js/core/app.js, and serves to create the UI components instances according to the configuration of the JSON using uiLayout.
(original text from: https://devdocs.magento.com/guides/v2.2/ui_comp_guide/concepts/ui_comp_config_flow_concept.html)

Next, lets see where this $block->getJsLayout() code comes from.
Well, at the top of the minicart.phtml, we can see

https://gist.github.com/marjan7790/8986394570f50f45ed01c1965535ffc0

but in the mentioned class there is no getJsLayout() method. Tracking through inheritance, gives us this chain

https://gist.github.com/marjan7790/05091cc53bd9d7621fd4615f8ad5c587

and inside the AbstractBlock we can see

https://gist.github.com/marjan7790/c568048be3528a3f3968b1f8593f543e

If we were to setup a break point over here, we will see that $this->jsLayout is a mere PHP array that will be turned into json object, the content of which looks like

p2

OK, since app.js serves to create instances according to configuration, and there is “config” key in this array, lets try to add data to the config.

Go ahead and create etc/di.xml with the following content

https://gist.github.com/marjan7790/69107f04517ec70acee169db6a5a1f30

and inside the Block/Cart folder, create the class Sidebar.js with

https://gist.github.com/marjan7790/3c096f592258023039d36165f8139772

If effect, I’ve set new key named maxItems with the config sub-array of the minicart_content component.

You probably need to re-compile so the server end actually considers using your block instead of the original, and you might need to re deploy the static content to get the .js changes.

Eventually, you will re-run the page and right click on the browser and “View Page Source”.  Search within the source for

[data-block='minicart']

and you will see what gets send to Magento_Ui/js/core/app.

I’ve copied the JSON that is passed and send it to https://jsoneditoronline.org/, so I have a nice tree like representation which helps visualize things. Thus, the JSON object looks like

p3

Great, the new key is there. But, how do we use it?

Replacing in minicart.js,
maxItemsToDisplay: 3

with
maxItemsToDisplay: origMiniCart.maxItems

does not work.

Well, if you make the code to be like

https://gist.github.com/marjan7790/65f89a8153dc04fdcc58c8e1a51d3550

and set a break point at var tmp = origMiniCart; you will see that that tmp is actually uiClass constructor, which in essence means we still do not actually have our object. Remember, from previous tutorial, the original initialize() has not executed yet.

If we don’t have an object, then, lets just create one! Update the code to be like

https://gist.github.com/marjan7790/6899d704c38328dee78a80cdfda94b17

Now, this works! But … why exactly?

It is important to know that you could send anything to your component in the config key and surely it will be available as property to your object. For example, lets say you have something like this in your .phtml

https://gist.github.com/marjan7790/acb101eb0e892e0f24df638dc653dbd9

With this code

  • You have declared that your component is named mycomponentname
  • have told that the actual js file is Vendor_YourModule/js/mycomponent
  • have told what the .html template is Vendor_YourModule/mycomponenttemplate
  • set a component title
  • added a new config property named valueToPass

If you have your component instantiated at some point you should be able to call
    mycomponentname.valueToPass
and this will return 55.

But, the origMiniCart is still merely a constructor function, so, this is exactly what we remedy against.

Lets dig in the original minicart.js initialize() function. It surely does first its own specific things, but eventually returns

https://gist.github.com/marjan7790/9874b3376536db681de15f53a33b41ad

Well, lets track this and figure who is the parent of this component?

It actually says return Component.extend({ …,

and if we scroll up in the “define” section of this RequireJs module, we will see that Component is alias for uiComponent and actually refers to Magento_Ui/js/lib/core/collection.

If you wonder where this alias comes from, open

https://gist.github.com/marjan7790/1acc044a4743851fa34f8be279a4b058

But anyways, opening Magento_Ui/js/lib/core/collection we see returns Element.extend({ and here Element is actually alias to uiElement, which (again from requirejs-config.js) is alias to Magento_Ui/js/lib/core/element/element.

Element is where for the first time we will see initialize() method that actually returns return this;, and not return this._super(); as wee saw in minicart.js.

So, uiElement is as far as the hierarchy goes. Therefore, if we typed in our initialize() something like

https://gist.github.com/marjan7790/7693e5aed1d9d053d6d46bbbe25a84a1

we will eventually force uiElement to return an actual object.

After that, its easier. We simple refer to the new property maxItems and assign it to the actual property that the original script uses, which is maxItemsToDisplay.

Finally, we return the parent object.

Check the 0.2.0 version of the module.


Conclusion

This article showed how to send new properties to a given component by passing customized json to app.js that added to the config array. Please keep in mind, that with this example, the new maxItems property is set to the original minicart component first. And this is all done in place after server side PHP has executed.

However, on the client end, at the moment of applying the mixins, I have instantiated an actual original minicart object, read from it the new property and updated an existing one. Not the most natural thing to do, but after this I hope you get a sense of what could potentially work as a way to pass server side data.

In the next article, we will continue to do an actual override of the component.

Thank you for reading.

How Magento passes server data to minicart.js

If you have ever build some admin grids that would list some entity data, you may have come across Data Sources. Magento recognizes the need to have a separate “channel” to be able to pass any server data into the java script objects and uses this Data Sources heavily in the admin section.

But, at the end of the day, its java script and if you want to make a variable available, you could always add it to the window object and all other js objects will have access to it.
Not ideal, but practical for sure!

In fact this is how its done in the original code:

https://gist.github.com/marjan7790/fba5c72f16a118cc66f60d7cc1fd67f2

Where is the checkout object set? Lets try common sense again! 🙂

It should be part of the checkout module, and it would be probably in a .pthml file. So, ask your favorite IDE to search in the vendor/magento/module-checkout folder for phrases like “window.checkout =”. Restrict this to a .pthml files only for faster processing.

Your IDE should find a match in

https://gist.github.com/marjan7790/d6331447c83d420014d22c4a05a30ca2

The code looks like

https://gist.github.com/marjan7790/b6c7b2fcb8bfadfd3121ac77a6899acf

Yes, the $block has a method that creates a checkout object, which among other properties, has the maxItemsToDisplay set as well.

By the way, if you were to debug, eventually you will figure the maxItemsToDisplay gets to be populated from the configuration value from

Configuration -> Sales -> Checkout (Common sense, remember?) -> Shopping cart Sidebar -> Maximum number of items to display

But regardless of that, you should by now have an idea how to edit this if needed. There can be many ways like:

  • override the block by adding new method like getMySerializedConfig() and call that method in an overridden minicart.phtml file
  • override the minicart.phtml by adding a new like

https://gist.github.com/marjan7790/004041751ed52cd46cf449899d1b31f5

There are various possibilities, it really depends on the needs, but, eventually, we will need to focus on passing values directly to the <script type=”text/x-magento-init”>, or in particular, extending this code:

https://gist.github.com/marjan7790/eadbdbe6a8e23dd7b95eea7de1fa80ff

The next article tries to pass our own server data to the Minicart.

Applying mixins to the minicart.js

Keeping in mind what has been done before, lets now do it right.

You will notice that editing the number of items to display is within this section of code

        return Component.extend({});

In fact, how things are set, maxItemsToDisplay is a property of this component.

On a contrary, this code

https://gist.github.com/marjan7790/9b8096668cb4a31e9a47ef1b5e733159

is not part of the Component object, but is well within the main anonymous function. This may not seem like an important thing, but actually, the way it is set leads to one important consequence:

This code is plain old jquery and not being part of the component means, that it sits there, loaded in the browser and listens for any direct clicks/taps.

This in particular means, it both gets executed as much as times there is a click/tap on the proper HTML element and has NO correlation/dependency on any other java script object that is already loaded. It just does a simple work on its own.

To confirm this, place a break-point in the original minicart.js and see how the execution is paused in the debugger every time you click to open the Mini cart.

Clear any earlier breakpoints and now place a break-point on this line

var self = this,

which is right within

https://gist.github.com/marjan7790/5cd955f5c20c8c51844c4d9f048195b9

and you will see that no matter what you do or click, the initialize function gets executed once and on page load.

Now keep this in mind, while we try to change the behavior of the Mini Cart component by applying mixins to it. (I’m assuming you already have created and enabled a module of yours, which is out of scope of this article.)

We will go through these two steps:

First, in you extension’s folder view/frontend add file requirejs-config.js with this content

https://gist.github.com/marjan7790/941eb1805171159d700fa2583358dcf0

And second, create the file minicart.js under the view/frontend/web/js folder of your extension, with this content

https://gist.github.com/marjan7790/49bbca41222c12f5f69d9e10169bb9ea

Re-deploy static content if you must, clear cache and voila, you are done!

What happened?

In requirejs-config.js, as the file name itself suggests, we have added configuration to requirejs, telling it to do something like:

RequireJs, there is a module under this path ‘Magento_Checkout/js/view/minicart’.
Would you please, find my new file under this path YourVendor_YourModule/js/minicart and apply mixins to it?

Mixins? Suffice it to say, there are two JS objects and you want to merge their properties and functions. Well, the idea behind is multiple inheritance and JS certainly makes this possible.
At the end of the operation, when mixins are applied, new object emerges that has the combined properties of both of the objects. This is not something that you can achieve with plain old Object Oriented Programming.
I have said this in a oversimplified manner, and some of you may want to continue elsewhere to gather more details about how mixins work.

OK, that given, our code, that it is supposed to be merged with the original minicart.js is

https://gist.github.com/marjan7790/69ca73c54b8754f9e2c36c00defec7e4

See, Magento has implemented RequireJs in a way that you are kindly getting a reference to the target object. I have named that object origMiniCart.

We might want to thank Underscore.js for the possibility to call origMiniCart.extend({}), but it is clear that with this we can properly override the component properties in a good way.

I have set maxItemsToDisplay: 3 and returned the extended component.


The code so far was solving requirement B: display only 3 items.

You will notice that the code that addresses requirement A  has nothing to with the component and it is mere jquery

https://gist.github.com/marjan7790/7dd23978ef049b1f91459d5add8cf786

Please notice that

https://gist.github.com/marjan7790/3f5b8a16fc17aea51b4b25267b1003b3

does NOT contain the initSidebar() call. The call to initSidebar() stays as is in the original minicart.js

Now, lets debug:

add a break-point at localBody.css(‘overflow’,’hidden’); in your minicart.js and one at initSidebar() in the original minicart.js

Click to show the Mini Cart.

You will notice that first the debugger stops in the overridden file , executing to hide the overflow, and only then moves to execute the initSidebar() in the original.

Now, lets clear any breakpoints for clarity and add new ones.
One at return origMiniCart.extend({ in your minicart.js and one at var self = this, within the initialize() method in the original minicart.js

When you reload the page, you will notice the same order of execution: first your code is executed, and then the execution moves to the original initialize().

For those that you have come this far, one last info.

Open file

https://gist.github.com/marjan7790/572c2cebff58aeda7caace08a90fb4c5

Locate this code

https://gist.github.com/marjan7790/88828f3ed71c55a0b2c01935974c579f

The comment above the function is sufficient enough to figure that the value to be modified, i.e. the original component is referred to as the “target”. After the modification the “target” will be returned.

Feel free to check the git repository for tag 0.1.0: https://github.com/marjan7790/minicart

Thank you for coming this far.

In the next article we will talk about parameters passing. Since this maxItemsToDisplay: 3 is a bit … hardcoded, no? 🙂

Locating and Editing minicart.js

How do you locate the component? There are many ways, but lets try to use both common sense and common tools.

In Google Chrome, just like mentioned earlier, in any Magento page (except the checkout), right click in your browser and then click “Inspect”. Then move to the “Sources” tab.
For those of you who prefer Firefox, right click in your browser and then click “Inspect Element”. Then move to the “Debugger” tab.

Now, in the left sidebar we see all available sources, and we will continue to focus on the pub/static folder.
Continue to open this path: /pub/static/frontend/[vendor]/[theme]/[locale].
This usually resolves to: /pub/static/frontend/magento/luma/en_US. (And yes, /pub may not be there is your web server is pointing to the /pub folder directly.)

But, moving back to the important issue. What’s next?
Well, common sense says that if the cart itself is part of the Magento Checkout module, perhaps the Mini Cart is too.

So, we continue to try and open folder Magento_Checkout/js and then we see it is very easy to spot /view/minicart.js. This is our component!

So far, we were looking at the deployed file in the /static folder, but now it is not hard to locate the actual source file:

https://gist.github.com/marjan7790/6999f2ad14af620b49143a47d5d0e73c
Now, lets solve the problem in a bad way.

Find this code

https://gist.github.com/marjan7790/9b8096668cb4a31e9a47ef1b5e733159

and change it to

https://gist.github.com/marjan7790/f6d49ea064af25765ac1b8e5763eac0a

Tip: if you’ve edited the file in the /vendor folder, you might need to re-deploy the static content. This given, changing the file directly in /static folder, may be more practical for trial and error approach like this.

Eventually, reload the page and click on the mini cart icon. It works! The window scroll on the right of the window is gone.

However, if we now close the mini cart, the scroll is still gone.

So, add this code

https://gist.github.com/marjan7790/2b51050947e15f1b5cebeece92454903

below the code that you edited earlier (but above return Component.extend({)

There, done.

Now, move a bit down and spot this line

https://gist.github.com/marjan7790/7aed17b35d43850040e748f2eeb9e931

change it to

https://gist.github.com/marjan7790/1cb374a40be8bd05e1384f4ea7764694

Re-deploy and clean cache if you must. But there, you are done! Take a brake now 🙂

Next, we will apply mixins to the original minicart component.