on ‎2023 Sep 13 10:06 AM
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
Request clarification before answering.
Hi,
I think that there are three possible options operating in the model layer:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
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.
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.
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."
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.
| User | Count |
|---|---|
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.