When you start to build an e-commerce website with Liferay Commerce, you might be faced with a requirement from a customer to personalize the “Mini Shopping Cart”.
You can use a Widget Display Template to create your own entire custom style and apply it to the “Mini Cart” module. But suppose you want to make just a minor change , because you like the mini-car commerce taglib styles and you don’t want to have to create an entire structure and style with a freemarker.
So... you start to look at how to override some part of the existing cart in Liferay Commerce (of course because we don’t want to reinvent the wheel, we only want to maybe add some small piece, or hide some existing part in this cart). But to your surprise, there is not a “standardized Liferay way” to override it.
It is not a JSP which we are comfortable with and know how to handle. It is a Taglib built with React files…
Don't panic, here I have the step-by-step recipe on how to do it.
First of all…
Go to your Liferay Portal > Control Panel > Sites > click to add a new site and select a SpeedWell Template, to create a new e-commerce Site for our test with the mini cart.
Go to the Catalog page and add some products to the cart. At the top right hand corner you can see the mini cart with the new product added. Here is the piece with which you’ll learn how to customize in this blog post.
So, let's get to know the code a little bit. It’s useful to know and understand the taglibs from Liferay Commerce, here is a link to help you: https://github.com/liferay/liferay-portal/blob/7.4.1-ga2/modules/apps/commerce/commerce-frontend-taglib/src/main/resources/META-INF/resources
|
Good to know: when you see the mini-cart taglib code, you will notice that it is called a react component in your Liferay Module, simply using the <aui:script require="${yourComponen}”>
So, the taglibs we saw were built with React Components, and here we have the Components used: https://github.com/liferay/liferay-portal/tree/7.4.1-ga2/modules/apps/commerce/commerce-frontend-js/src/main/resources/META-INF/resources/components
|
So lets start to create our taglib overriding the mini shopping cart OOTB.
1. I always like to use the blade and starting with a liferay-workspace:
blade init -v portal-7.4-ga2 liferay-workspace-7-4
|
2. For this test create a API module inside your liferay-workspace, with the following command:
|
3. Inside your module, delete the package created by default and create these two java classes for our taglib:
a. com.liferay.commerce.frontend.taglib.custom.internal.servlet.ServletContextUtil
* Inside copy and paste the following code
|
b. com.liferay.commerce.frontend.taglib.custom.CommerceFrontendTaglibCustom
*Inside copy and paste the following code
|
4. Open the bnd.bnd file and add the following configuration
|
|
NOTE: The main target here is to know how to customize a React taglib from commerce OOTB. These two previous steps are only about how to create a simple and common taglib, I don’t want to bore you by going into too much detail.
|
5) Now in “commerce-frontend-taglib-custom/src/main/resources”, eliminate everything inside and create the following structure of directories and files:
└── META-INF
├── liferay-commerce-ui-custom.tld
├── resources
│ ├── init.jsp
│ ├── js
│ │ └── OverriddenCartItem.js
│ └── mini-cart
│ └── page.jsp
└── taglib-mappings.properties
a. In the liferay-commerce-ui-custom.tld add the following code:
|
b. In the init.jsp add the following code:
|


