Expand my Community achievements bar.

SOLVED

How to modify a product property in JSP code?

Avatar

Level 2

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><% } %>
1 Accepted Solution

Avatar

Correct answer by
Level 9

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.

View solution in original post

7 Replies

Avatar

Level 9

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

Avatar

Level 2

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, 

Avatar

Level 10

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

Avatar

Level 2

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.

Avatar

Correct answer by
Level 9

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.

Avatar

Level 10

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