KonaKart Community Forum

Installation / Configuration => Programming of KonaKart => Topic started by: chloeloves on February 24, 2010, 06:26:59 pm

Title: Product Quantity (aggregated)
Post by: chloeloves on February 24, 2010, 06:26:59 pm
What is the most efficient way to retrieve a products aggregated quantity?

In other words, if a product has multiple options, is there an out of the box way to retrieve the sum of the product option quantities.

Right now I am performing somewhat of a hack to get this.


public static int getAggreatedStockLevelsForProduct(KKEngIf engine, ProductIf product) throws KKException {
if(product == null) return 0;

int productQuantity = 0;
OptionIf[] opts = product.getOpts();

if(opts!= null && opts.length > 0){
for(int i=0; i<opts.length; i++){
        OptionIf option = opts[i];
        StringBuffer encodedString = new StringBuffer();
            encodedString.append(product.getId());
           
            encodedString.append("{").append(option.getId()).append("}").append(option.getValueId());
            ProductQuantityIf quantity  = engine.getProductQuantity(encodedString.toString());
            productQuantity += quantity.getQuantity();
        }
}else {
productQuantity = product.getQuantity();
}
        return productQuantity;
}




I would like to know the quantiy available for a given product on the category page, so I am afraid that if a category has a large amount of products, the above method call for each product would slow things down.

Thanks in advance.

Roy
Title: Re: Product Quantity (aggregated)
Post by: trevor on February 27, 2010, 08:17:28 pm
What you could do is to create a custom API call . Within this API call you could implement a single query on the products_quantity table that sums the quantities for all records where products_id == x .