ProductAttributeSet set = new ProductAttributeSet();
set.setId(9);
set.setName("Vestuario");
Product product = new Product();
product.setAttributeSet(set);
product.setSku(MagjaStringUtils.randomString(3, 10).toUpperCase());
product.setName(MagjaStringUtils.randomString(3, 5) + " Configurable Prod");
product.setShortDescription("Short description");
product.setDescription("Some description for that configurable product");
product.setPrice(new Double(222.23));
product.setCost(new Double(111.22));
product.setEnabled(true);
product.setWeight(new Double(0.100));
Integer[] websites = { 1 };
product.setWebsites(websites);
// set the product type as a configurable
product.setType(ProductTypeEnum.CONFIGURABLE.getProductType());
/*
* creates the Configurable Attributes Data, that are the attributes wich
* are configurable for this product
*/
product.setConfigurableAttributesData(new LinkedList<ConfigurableAttributeData>());
// the attribute SIZE
ConfigurableAttributeData cnfgAttributeSize = new ConfigurableAttributeData();
cnfgAttributeSize.setAttributeId(126);
cnfgAttributeSize.setAttributeCode("size");
// the attribute COLOR
ConfigurableAttributeData cnfgAttributeColor = new ConfigurableAttributeData();
cnfgAttributeColor.setAttributeId(83);
cnfgAttributeColor.setAttributeCode("color");
// add the conf attr data to the product
product.getConfigurableAttributesData().add(cnfgAttributeSize);
product.getConfigurableAttributesData().add(cnfgAttributeColor);
// reset the configurable product data from product
product.setConfigurableSubProducts(new LinkedList<ConfigurableProductData>());
/*
* creates the variations of the attributes, for each configurable attribute
* it can add a new product for this variation, remember you have to
* creates the Configurable Attributes Data first (above code)
*/
ConfigurableProductData prdData1 = new ConfigurableProductData();
ConfigurableData confgDataSizeOne = new ConfigurableData();
confgDataSizeOne.setAttributeId(126);
confgDataSizeOne.setLabel("P");
confgDataSizeOne.setValueIndex(6);
ConfigurableData confgDataColorOne = new ConfigurableData();
confgDataColorOne.setAttributeId(83);
confgDataColorOne.setLabel("blue");
confgDataColorOne.setValueIndex(15);
prdData1.getData().add(confgDataSizeOne);
prdData1.getData().add(confgDataColorOne);
try {
prdData1.configurateProduct(product, new Double(10), new Double(100));
} catch (ConfigurableDataException e) {
e.printStackTrace();
fail(e.getMessage());
}
ConfigurableProductData prdData2 = new ConfigurableProductData();
ConfigurableData confgDataSizeTwo = new ConfigurableData();
confgDataSizeTwo.setAttributeId(126);
confgDataSizeTwo.setLabel("M");
confgDataSizeTwo.setValueIndex(7);
ConfigurableData confgDataColorTwo = new ConfigurableData();
confgDataColorTwo.setAttributeId(83);
confgDataColorTwo.setLabel("red");
confgDataColorTwo.setValueIndex(14);
prdData2.getData().add(confgDataSizeTwo);
prdData2.getData().add(confgDataColorTwo);
try {
prdData2.configurateProduct(product, new Double(15), new Double(130));
} catch (ConfigurableDataException e) {
e.printStackTrace();
fail(e.getMessage());
}
// don't forget to add the ConfigurableProductData as a subproduct to the main product
product.getConfigurableSubProducts().add(prdData1);
product.getConfigurableSubProducts().add(prdData2);
// finally, save the product
service.save(product);
assertTrue(product.getId() != null);
productId = product.getId();
// way to get the subproducts from a super products:
// product.get("subproduct_ids");
}