Dynamic Attribute in SAP Commerce

Dynamic attributes allow you to add custom attributes to your model without modifying the model class itself. They enable you to create custom logic for these attributes, which is not present in the standard model class. Dynamic attributes serve as transient data, meaning they are not stored in the database.

When we define an attribute for an item type, it comes with a tag called “persistence type”.
If the persistence type is set to “property”
A corresponding column is created in the database.
Values assigned to this attribute will be stored in the database.
This type of attribute is called a “persistence attribute” because its values persist in the database.

If the persistence type is set to “dynamic”
No column is created in the database.
Values assigned to this attribute will not be stored in the database.
This type of attribute is called a “non-persistent” or “dynamic” attribute because its values are not persisted in the database.

For every dynamic attribute we define, we need to specify the attribute handler. If we don’t specify it, a Bean Id will be generated automatically. In such cases, we must use the generated Bean Id when defining the Spring bean in XML.

The attribute handler is implemented using Spring. Therefore, we need to specify the Spring bean Id for the attribute handler. After that, we define a class for that Spring bean Id which provides the custom logic for the dynamic attribute. This class contains the logic for how the dynamic attribute behaves and is responsible for handling its behavior.

Example:

Step-1:-

Define a new attributes in existing item type.
D:\Hybris2205SPA\hybris\bin\custom\clavrit\clavritcore\resources\clavritcore-items.xml

	

Step-2:-

Create DynamicAttributeHandler class for handeling the newly created custom dynamic attribute.
D:\Hybris2205SPA\hybris\bin\custom\clavrit\clavritcore\src\com\clavrit\core\dynamic\DayBeforExpiryHandler.java

package com.clavrit.core.dynamic;

import de.hybris.platform.servicelayer.model.attribute.DynamicAttributeHandler;
import de.hybris.platform.voucher.model.VoucherModel;

import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;

public class DayBeforExpiryHandler implements DynamicAttributeHandler<string, vouchermodel=""> {
    final public static String EXPIRY_MSG_DATE_NOT_AVAIALBLE = "Expiry data is not Available";
    final public static String VOUCHER_EXPIRED = "Voucher Expired";
    final public static String MONTHS = "Months :";
    final public static String DAYS = "Days :";
    final public static String DAY = "Day :";
    final public static String COMMA = ", ";


    @Override
    public String get(VoucherModel voucherModel) {
        Date eDate = voucherModel.getEndtDate();
        if (eDate == null) {
          return EXPIRY_MSG_DATE_NOT_AVAIALBLE;
        }
        LocalDate expiryDate = eDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate currentDate = LocalDate.now();
        return currentDate.isBefore(expiryDate) ? getMessage(expiryDate,currentDate) : VOUCHER_EXPIRED ;
    }

    @Override
    public void set(VoucherModel model, String s) {

    }

    private String getMessage( LocalDate expiryDate , LocalDate currentDate){

        Period period = Period.between(currentDate, expiryDate);
        int daysLeft = period.getDays();
        int monthsLeft = period.getMonths();
        if(monthsLeft!=0){
            return MONTHS + monthsLeft + COMMA + DAYS + daysLeft;
        }
        return  daysLeft==1 ? DAY + daysLeft :DAYS + daysLeft;
    }
}

</string,>

Step-3:-

Define bean inside clavritcore-spring.xml.
D:\Hybris2205SPA\hybris\bin\custom\clavrit\clavritcore\resources\clavritcore-spring.xml

    

Note:

Bean id should be same as attributeHandler mentioned in items.xml.

Step-4:-

Perform build and then update the system using HAC Or CMD

Step-5:-

Now we can check inside backoffice as I have shown below
Now set endDate in my case its is 03 April and currentDate is 02 April, we can see bellow

backoffice1
We can see 1 day left to expire
If it’s been more than a day, then it will show as one day.

backoffice2
If the expiry date exceeds 30 days, it will be changed to a month.

backoffice3
The code displays an expired message when it passes the set expiration date.

backoffice4

backoffice5

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top