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).

First Step: Create a New Hybris Extension (Steps 1-9)

1. Go to <HYBRIS_BIN_DIR>/platform using your command prompt, and run the setantenv.bat command (Windows machines), or . ./setantenv.sh (Linux or MAC OS X machines)

setantenvbat

2. Run ant extgen command.

3. Type yempty then press Enter

4. Next name your extension, for example, type hybrisarchitect then press Enter. Note: Feel free to name your extension to your specific needs.

5. Type com.hybrisarchitect for the package name then press Enter. Note: You may enter a different package name for your specific needs.

6. Confirm the extension has been properly created. You should see the following BUILD SUCCESSFUL in your command prompt.

7. In your code editor, open the localextensions.xml from the <HYBRIS_CONFIG_DIR>, and add the following line before the ending </extensions> tag.

<extension name="hybrisarchitect"/>

8. Go to <HYBRIS_BIN_DIR>custom\hybrisarchitect\resources and open the hybrisarchitect-items.xml file in your code editor, and add the following XML to hybrisarchitect-items.xml file

<itemtype code="AbstractOrder" generate="false" autocreate="false">
      <attributes>
                        <attribute qualifier="ipAddress" type="java.lang.String">
                              <modifiers optional="false" />
                              <persistence type="property" />
                        </attribute>
   </attributes>
</itemtype>

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

11. Open your Web browser, and type https://localhost:9002/ then log into HAC by using admin credentials

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

 

Marc Raygoza

Marc is the Founder of HybrisArchitect.com.
He enjoys helping others learn more about SAP Commerce Cloud (Hybris). Marc is a SAP Commerce Certified Professional and has held the role of SAP Commerce Cloud Architect at Deloitte, PwC, Brillio (a Bain Company), and Nasty Gal. 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: mraygoza@hybrisarchitect.com

You may also like...

Popular Posts