1
votes

we have a new attribute( is special ??) that needs to be added to product. And only some products have the value of that attribute as true. All others have that value as false.

If we create that attribute at a product level....somebody has to go in and set that value for all the products manually in the backoffice....which is too much work..

Any better ways to handle this ?....can we create a product group for a subset of products and then set that attribute at that group level ? so that maintanence becomes easy...

1

1 Answers

1
votes

There are a few options you can consider: You can declare the attribute on Product item level with a default value of false:

            <attribute qualifier="myAttribute" type="java.lang.Boolean">
                <description>what it dodes</description>
                <persistence type="property" />
                <defaultvalue>Boolean.FALSE</defaultvalue>
            </attribute>

This will guarantee that for each newly created product the default value is false. Note that this will not set the value false to any existing instance. If you add this attribute to an existing system (e.g. as part of a migration) you could e.g. treat a null value as false in your code or you could e.g. write a beanshell script to be executed once as part of the migration that will update all products that have the value still set to null (see http://experts.hybris.com/answers/3446/view.html).

copied/modified (but not tested :) ) from that linked answer on the hybris experts forum:

import de.hybris.platform.jalo.SearchResult; 
import de.hybris.platform.jalo.product.Product; 
import java.util.Collections;
SearchResult products = de.hybris.platform.jalo.flexiblesearch.FlexibleSearch.getInstance().search("select {PK} from {Product} where {myAttribute} IS NULL", Collections.emptyMap(), Customer.class);
int updatedCnt = 0; 
for (Product product : products.getResult()) 
{ 
    try 
    { 
        product.setAttribute("myAttribute", false);
        updatedCnt++;
    } catch (Exception e) 
    { 
        System.out.println("failed to update product " + product.getPK() + " " + e.getMessage()); 
     }
} 
System.out.println("number updated = " + java.lang.Integer.toString(updatedCnt));

Note that you would still need some custom logic to set the value to true for the products you want that attribtue to be true (unless that is only done manually through the backoffice).

Hope this helps, Sebastian