How to modify a product property in JSP code? | Community
Skip to main content
Alberto_León
New Participant
March 8, 2016
Solved

How to modify a product property in JSP code?

  • March 8, 2016
  • 7 replies
  • 2285 views

Hi all,

 

I'm trying to obtain a property of a product, and it is done with the following lines of code:

 "String combo = product.getProperty("componentBox", String.class);

            out.println(combo);"

but, now I want do a setProperty of this product to modify the componentBox field, but the API product haven't this function and my questions is... How could I modify a property of a product in CRXDE with code in .jsp?

Is it possible to do this?

Could you give me any example?

 

Thanks a lot for your time.

 

Best regards.

<%@page session="false"%> <% %><%@ include file="/libs/foundation/global.jsp" %><% %><%@ page contentType="text/html; charset=utf-8" import=" com.adobe.cq.commerce.api.CommerceSession, com.adobe.cq.commerce.common.CommerceHelper, com.adobe.cq.commerce.api.Product, info.geometrixx.commons.util.GeoHelper, com.adobe.cq.commerce.api.CommerceService, java.util.Locale"%><% final Locale pageLocale = currentPage.getLanguage(false); final String language = pageLocale.getLanguage(); CommerceService commerceService = resource.adaptTo(CommerceService.class); CommerceSession session = commerceService.login(slingRequest, slingResponse); final Page listItem = (Page)request.getAttribute("listitem"); final Product product = CommerceHelper.findCurrentProduct(listItem); if (product != null) { final String pagePath = listItem.getPath(); final String title = product.getTitle(language); final String price = session.getProductPrice(product); String combo = product.getProperty("componentBox", String.class); out.println(combo); %><li> <a href="<%= xssAPI.getValidHref(pagePath) %>.html" title="<%= xssAPI.encodeForHTMLAttr(title) %>" onclick="CQ_Analytics.record({event: 'listItemClicked', values: { listItemPath: '<%= xssAPI.encodeForJSString(pagePath) %>' }, collect: false, options: { obj: this }})"> <img src="<%= xssAPI.getValidHref(product.getThumbnailUrl(160)) %>" width="160" height="120" alt="<%= xssAPI.encodeForHTMLAttr(title) %>"/> <h4><%= xssAPI.encodeForHTML(title) %></h4> <% if (GeoHelper.notEmpty(price)) { %> <p><%= xssAPI.encodeForHTML(price) %></p><% } %> </a> </li><% } %>
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Jitendra_S_Toma

Following things you could do:

  • Session jcrSession = resourceResolver.adoptTo(Session.class)
  • Get the product path from Product interface. String etcPath = product.getPath()
  • Resource resource = resourceResolver.getResource(etcPath)
  • if (resource != null ) Node node = resource.adoptTo(Node.class)
  • node.setProperty(......).
  • session.save();

Hope this helps.

7 replies

smacdonald2008
New Participant
March 8, 2016

OK  - in AEM everyone is a node. Resources such as products are nodes. So using the JCR API - you can use NODE API methods to perform CRUD (create, replace, update, and delete) operations on Nodes. See this community article - it will help you get up and running on JCR API: 

http://scottsdigitalcommunity.blogspot.ca/2012/03/programmatically-accessing-day-cq.html

Jitendra_S_Toma
Jitendra_S_TomaAccepted solution
New Participant
March 8, 2016

Following things you could do:

  • Session jcrSession = resourceResolver.adoptTo(Session.class)
  • Get the product path from Product interface. String etcPath = product.getPath()
  • Resource resource = resourceResolver.getResource(etcPath)
  • if (resource != null ) Node node = resource.adoptTo(Node.class)
  • node.setProperty(......).
  • session.save();

Hope this helps.

rubenm81941414
New Participant
March 8, 2016

Hi,

We are beginners in the development of AEM. We have not tried to use the JCR API  to modify the product node. Could you tell us the step to follow?

Thanks you.

smacdonald2008
New Participant
March 8, 2016

A Product is a JCR node. Have you tried using JCR API to set a prop on a product node? 

Alberto_León
New Participant
March 8, 2016

Jitendra S.Tomar wrote...

Well, 

The Product is not a simple POJO class which can provide setter & getter. This Commerce Product provides a way to read your product information.And, for that, It depends on product commerce factory & Commerce session. 

To answer your question whether you can set or not. The answer is NO.

Ideally, you should not be modified any product information from your components & pages. What if product information is coming from the third party?. So, if you want to update some property of the product, do it offline or some other way.

---Jitendra

 

Thanks Jitendra.

You confirm my toughts. I have another question related to this post. There is another way to modify the property value of a product, through a node o the product, or something similar?

I found this link http://www.day.com/maven/jsr170/javadocs/jcr-1.0/javax/jcr/Node.html. This interface can implement setProperty() but I don't know how to implement in my .jsp code. Could you give me some steps to do that?

Thanks in advice, 

smacdonald2008
New Participant
March 8, 2016
Jitendra_S_Toma
New Participant
March 8, 2016

Well, 

The Product is not a simple POJO class which can provide setter & getter. This Commerce Product provides a way to read your product information.And, for that, It depends on product commerce factory & Commerce session. 

To answer your question whether you can set or not. The answer is NO.

Ideally, you should not be modified any product information from your components & pages. What if product information is coming from the third party?. So, if you want to update some property of the product, do it offline or some other way.

---Jitendra