How do you add a new attribute to an existing item type in SAP Hybris Commerce?

With SAP Hybris Commerce, it’s very simple to add a new attribute to an existing item type. If you are new to Hybris Commerce, you might be asking yourself (rightfully so), what does this actually mean? I will give you an example, in Hybris OOTB the Order (item type) does not contain an IP Address (attribute). From an e-commerce perspective, it’s prudent to save the IP Address used by the customer as he or she places the order. In this tutorial, we will solve this problem by creating a new attribute ( the IP Address) for an existing item type (the Order).

When done, your changes should resemble the following, please note the <itemtype> XML node element should be placed in the ending </itemtypes> tag:

The above-mentioned itemtype XML properly customizes Hybris core by adding the ipAddress attribute to the AbstractOrder itemtype, which permits the Order entity to inherent this attribute as well. In other words, Hybris will automatically generate the getter and setters (modifiers) for the ipAddress attribute, as well as, create a new database column called P_IPADDRESS for the ORDER table. No code changes are required to implement CRUD (Create-Read-Update-Delete) for the IP Address attribute!

9. Go back to the command prompt, and type ant clean all

The ant clean all script will take several minutes to complete, which will generate the ipAddress modifiers for the OrderModel.java and GeneratedHybrisarchitectManager.java files.

If interested, you may verify the modifiers have been generated by opening the <HYBRIS_BIN_DIR>\platform\bootstrap\gensrc\de\hybris\platform\core\model\order\OrderModel.java

10. Run hybrisserver.bat from the command prompt (Windows machines). Run ./hybrisserver.sh on Linux or MAC OS X machines.

After several minutes, the Hybris server will be up and running.
Next, let’s login into HAC and verify the P_ADDRESS column has been added to the Order Entity

12. Once you have logged in successfully, go to Console->FlexibleSearch. When the FlexibleSearch form is displayed type the following in the FlexibleSearch query console:

select {ipAddress} from {Order}

Your FlexibleSearch form should resemble the following:

13. Press the Execute button.

14. Click on the Search result tab to verify the P_IPADDRESS has been created.

15. This tutorial is complete. You now know how to add a new attribute to an existing item type in SAP Hybris Commerce

Special Offer: A step-by-step video tutorial is available for the "How do you add a new attribute to an existing item type in SAP Hybris Commerce?" tutorial. To subscribe, click on the link below.Click here to register

About the author

Marc is the Founder of HybrisArchitect.com.
He enjoys helping others learn more about SAP Hybris Commerce.
Marc has held the role of Hybris Architect at Exemplis and Nasty Gal. He is a long-time Java/Spring developer. Marc holds an M.S. Software Engineering from Carnegie Mellon University and a B.S. in Accountancy from California State University, Fresno. He can be reached at: info@hybrisarchitect.com