Adding a New Attribute to Product and Serving It to Spartacus
Adding a New Attribute to Product and Serving It to Spartacus
When customizing SAP Commerce Cloud (Hybris), you may need to add new attributes to existing models like ProductModel
and expose them to the Spartacus storefront. This guide will walk you through adding a new attribute to ProductModel
and serving it to Spartacus.
Step 1: Add the New Attribute to ProductModel
Modify the Items.xml
Define the new attribute in the items.xml
file of your extension:
1 | <itemtype code="Product" autocreate="true" generate="true"> |
Run ant clean all
to regenerate the model classes and update the database schema.
Step 2: Add the Attribute to Product Data
Update the ProductData Class
Add the new attribute to ProductData
:
1 | <bean class="de.hybris.platform.commercefacades.product.data.ProductData"> |
Step 3: Create a Populator for the New Attribute
Create a custom populator to populate ProductData
with the value from ProductModel
:
1 | package com.mycompany.core.populators; |
Step 4: Register the Populator in Spring Configuration
Add the populator to the productConverter
in your Spring XML configuration:
1 | <bean id="newAttributePopulator" class="com.mycompany.core.populators.NewAttributePopulator" /> |
Step 5: Extend the Product Web Services
Update the ProductWsDTO
to include the new attribute:
1 | <bean class="de.hybris.platform.commercewebservicescommons.dto.product.ProductWsDTO"> |
Update the FieldSetLevelMapping to map the attribute from ProductData
to ProductWsDTO
:
1 | <bean parent="fieldSetLevelMapping" id="productWsDTOFieldSetLevelMapping"> |
Step 6: Extend the Occ Layer
Update the OCC controller to include the new attribute in the response:
1 |
|
Step 7: Update Spartacus to Consume the New Attribute
Modify the Spartacus product model to include the new attribute:
1 | export interface Product { |
Update the UI components to display the attribute:
1 | <div *ngIf="product?.newAttribute"> |
Testing
- Backend: Ensure the attribute is added to the database and populated correctly.
- API: Verify that the OCC endpoint includes the new attribute.
- Frontend: Test that Spartacus displays the new attribute as expected.
Final Thoughts
By following these steps, you can successfully add new attributes to SAP Commerce and expose them to Spartacus. This approach ensures maintainability and scalability for future customizations.
Happy Coding!