cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Copy attribute content from a Category to all the products in it

datacol
Explorer
0 Likes
893

Hi all,

I'm looking for a way to copy the content of an attribute contained in a Category to an attribute in the products in that category. This operation should happen when an user inserts a value in that field in a Category using the Backoffice. I was looking at the handlers, like "ProductToCategoryDropHandler", but don't know for sure if it is the right choice and I didn't find the handler from Category to product.

Thanks in advance for any suggestion

Accepted Solutions (1)

Accepted Solutions (1)

adambadura
Participant

Hi,

I think that there are three possible options operating in the model layer:

  1. Implement PrepareInterceptor for Category itemtype that will go through all products and set the desired value. Example: de.hybris.platform.outboundsync.config.impl.DefaultChannelConfigurationPrepareInterceptor
    Interceptor documentation: https://help.sap.com/docs/SAP_COMMERCE_CLOUD_PUBLIC_CLOUD/aa417173fe4a4ba5a473c93eb730a417/8bfbf43e8...
    Beware that this may have significant impact on performance in case when you have a lot of products residing in the category - every modification to the Category object would also trigger modification to all Products in this category.
    You could try to mitigate this using asynchronous event raised in the interceptor with additional event listener that actually modifies the products (see this for more info on async events: https://learning.sap.com/learning-journey/expand-upon-technical-essentials-of-sap-commerce-cloud/con...
  2. Listen to AfterSaveEvent on Category item (as described here: https://help.sap.com/docs/SAP_COMMERCE_CLOUD_PUBLIC_CLOUD/aa417173fe4a4ba5a473c93eb730a417/8b51226d8...
    This also may have negative performance impact similar to solution 1.
  3. Implement a dynamic attribute handler on the Product itemtype to dynamically fetch the attribute value from the Category during loading of the Product item. Here you would need to decide which Category to use because one Product may be assigned to multiple Categories.
datacol
Explorer
0 Likes

Hi Adam,

thank you very much for the helpful answer. I really appreciate it. I'm trying the first solution you suggest, but I have a strange behavior.

The copy of the value from the Category to the Product does not always work . I noticed that most of the time the copy of the value does not work is when the Prepare Interceptor of the Product is not called (I checked it with a debugger). But I suppose it should always call when I insert a value in that attribute in the Category since the new value should be also saved in the Product table in the database. Am I wrong?

adambadura
Participant
0 Likes

Hi,

Did you register the new interceptor for Category itemtype?

Here is an example from the Commerce platform for de.hybris.platform.category.interceptors.CategoryPrepareInterceptor:

<bean id="categoryPrepareInterceptor" class="de.hybris.platform.category.interceptors.CategoryPrepareInterceptor">
<property name="categoryService" ref="categoryService" />
</bean>

<bean id="categoryPrepareinterceptorMapping" class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
<property name="interceptor" ref="categoryPrepareInterceptor" />
<property name="typeCode" value="Category" />
</bean>

This example is somewhat similar to what you are trying to achieve. You need to register your interceptor using typeCode=Category to be able to handle Category modifications.

datacol
Explorer
0 Likes

Yes, I did both declarations. In fact, all the changes I do in a category are saved correctly. The issue I am facing is that the associated product/products are not always saved as I am expected. I saw while debugging the code that the prepare interceptor of the product does not always triggered. I am expecting that the CategoryPrepareInterceptor is triggered firstly and then secondly the ProductPrepareInterceptor in order to save the changes I do in the product/products.

adambadura
Participant
0 Likes

Hi,

I'm not sure why this may happen but are you using de.hybris.platform.servicelayer.interceptor.InterceptorContext#registerElementFor to save the modified products in the Category item interceptor? There is an example here: https://help.sap.com/docs/SAP_COMMERCE_CLOUD_PUBLIC_CLOUD/aa417173fe4a4ba5a473c93eb730a417/8bfbf43e8... in section "Example - Creating a RemoveInterceptor that Stores Deleted Users in a Separate Table."

datacol
Explorer

Hi,

sorry for the late reply but I didn't have so much time to test the solution you proposed. In the end, the usage of the context does not work, but I was able to resolve the issue by using modelService.save(...) function of the ModelService class (de.hybris.platform.servicelayer.model.ModelService). Anyway, the interceptor idea is good and works well for my case, so thank you very much for the help you gave me.

Answers (0)