I need to generate a custom XML schema in magento 1.8.0 with some info of my configurable products. In these info, I need to fetch some attributes (size, color) from the simple products for each configurable. This is a relevant piece of my code so far, which generates my XML fine, except the fact I can't find a way to fetch the attributes 'color' and 'shirt_size' from the simple products.
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status', 1);
$products->addAttributeToFilter('visibility', 4);
$products->addAttributeToSelect('*');
$xml = new SimpleXMLElement('<xml/>');
foreach ($products as $product_all) {
$sku = $product_all->getSku();
$price = $product_all->getPrice();
$prod = $xml->addChild('product');
$prod->addChild('product_id', "$sku");
$prod->addChild('price', "$price");
$prod->addChild('color', "");
$prod->addChild('shirt_size', "");
}
print($xml->asXML());
Thanks!
You could try something like this, which loads the child products then gets the attribute values of their configured options:
foreach ($products as $product_all) {
...
$prod = $xml->addChild('product');
...
/* @var Mage_Catalog_Model_Product $product_all */
if ($product_all->isConfigurable()) {
$options = $product_all->getOptionList();
/* @var Mage_Catalog_Model_Product_Type_Configurable $type */
$type = $product_all->getTypeInstance();
$children = $type->getUsedProducts(null, $product_all);
/* @var Mage_Catalog_Model_Product $child */
foreach ($children as $child) {
foreach ($options as $option) {
$prod->addChild($option->code, $child->getAttributeText($option->code));
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.