1

I have a Magento CE-1.9.2.4 installation and I am trying to create an xml feed with configurable products.

My products (clothes) are configured based on Color attribute. Each configurable product has Associated Products based on Size.

I am experiencing three issues with:

  • <additionalimage>
  • <category>
  • <size>

Here is the code I am using:

<?php
ini_set('display_errors', 1);
set_time_limit(0);

define('FEED_LOCATION','export/xml-feed.xml');
define('LIVE_EL_URL','https://www.my-domain.com/');
define('DEV_EL_URL','http://dev.my-domain.com/');

require_once 'app/Mage.php';

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::app('default');

try{
    $handle = fopen(FEED_LOCATION, 'w');
    $heading = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
    $heading .= '<mywebstore>'."\r\n";
    $heading .= "\t"."<created_at>".date('Y-m-d G:i')."</created_at>"."\r\n";
    $heading .= "\t".'<products>'."\r\n";
    $feed_line = $heading;
    fwrite($handle, $feed_line);

    //GET THE PRODUCTS
    $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToFilter('status', 1); //enabled
    $products->addAttributeToFilter('visibility', 4); //catalog, search
    $products->addAttributeToSelect('*');
    $prodIds = $products->getAllIds();
    $product = Mage::getModel('catalog/product');
    foreach($prodIds as $productId) {
        $product->load($productId);
        $product_data = array();
        $product_data['start']  = "\t\t".'<product>'."\r\n";
        $product_data['id']     = "\t".'<id>'.$product->getSku().'</id>'."\r\n"; //id
        $product_data['name']   = "\t".'<name><![CDATA['.$product->getName().' ('.$product->getResource()->getAttribute('color')->getFrontend()->getValue($product).')]]></name>'."\r\n"; //name
        $product_data['link']   = "\t".'<link><![CDATA['.DEV_EL_URL.$product->getUrlPath().']]></link>'."\r\n"; //url
        $product_data['image']  = "\t".'<image><![CDATA['.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage().']]></image>'."\r\n"; //image

        foreach ($product->getMediaGalleryImages() as $image) {
            $product_data['additionalimage']    .= "\t".'<additionalimage><![CDATA['.$image->getUrl().']]></additionalimage>'."\r\n"; //additional images
        }

        foreach($product->getCategoryIds() as $_categoryId){
            $category = Mage::getModel('catalog/category')->load($_categoryId);
            $product_data['category'].=$category->getName().' > ';
        }
        $product_data['category'] = "\t".'<category><![CDATA['.rtrim($product_data['category'],'> ').']]></category>'."\r\n"; //category

        $normal_price = number_format($product->getPrice(), 2, '.', ''); //price
        $special_price = number_format($product->getSpecialPrice(), 2, '.', ''); //special price
        $price = "";
        $todayDate = date('m/d/y');
        $special_to_date = $product->getSpecialToDate();
        if($special_price != "0.00"){
            if($todayDate < $special_to_date){
                $price = $normal_price;
            } else {
                $price = $special_price;
            }
        } else {
            $price = $normal_price;
        }
        $product_data['price'] = "\t".'<price_with_vat>'.$price.'</price_with_vat>'."\r\n";

        $product_data['manufacturer'] = "\t".'<manufacturer><![CDATA['.$product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($product).']]></manufacturer>'."\r\n"; //manufacturer
        $product_data['description'] = "\t".'<description><![CDATA['.$product->getShortDescription().']]></description>'."\r\n"; //description

        $product_data['instock'] = "\t".'<instock>Y</instock>'."\r\n"; // Y: InStock, N: NOT InStock
        $product_data['availability'] = "\t".'<availability>In Stock</availability>'."\r\n";
        $product_data['shipping'] = "\t".'<shipping>0</shipping>'."\r\n"; // 0: Free Shipping

        $sizes = "";
        $attrs = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
        foreach($attrs as $attr) {
            if(0 == strcmp("size", $attr['attribute_code'])) {
                $options = $attr['values'];
                foreach($options as $option) {
                    $sizes .= $option['store_label'].', ';
                }
            }
        }
        $product_data['size'] = "\t".'<size>'.$sizes.'</size>'."\r\n";

        $product_data['color'] = "\t".'<color>'.$product->getResource()->getAttribute('color')->getFrontend()->getValue($product).'</color>'."\r\n";

        $product_data['end'] = '</product>';

        foreach($product_data as $k=>$val){
            $product_data[$k] = $val;
        }

        $feed_line = implode("\t\t", $product_data)."\r\n";
        fwrite($handle, $feed_line);
        fflush($handle);
    }

    $footer = "\t".'</products>'."\r\n";
    $footer .= '</mywebstore>';
    $feed_line = $footer;
    fwrite($handle, $feed_line);
    fclose($handle);
} catch(Exception $e) {
    die($e->getMessage());
}
?> 

Here is the xml feed produced (just a few products):

<?xml version="1.0" encoding="UTF-8"?>
<mywebstore>
    <created_at>2016-08-26 8:40</created_at>
    <products>
        <product>
            <id>3042738-Coral</id>
            <name><![CDATA[Διπλό ζέρσεϋ μίντι φόρεμα (Coral)]]></name>
            <link><![CDATA[http://dev.my-domain.com/3042738-coral-diplo-zersey-midi-forema.html]]></link>
            <image><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-03.jpg]]></image>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-03.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-02.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-04.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-01.jpg]]></additionalimage>
            <category><![CDATA[Κατηγορίες > Γυναίκα > MY FIRM > Φορέματα > Φορέματα]]></category>
            <price_with_vat>53.00</price_with_vat>
            <manufacturer><![CDATA[MY FIRM]]></manufacturer>
            <description><![CDATA[Διπλό μίντι φόρεμα με μπουφάν λάστιχο στην μέση]]></description>
            <instock>Y</instock>
            <availability>In Stock</availability>
            <shipping>0</shipping>
            <size>XS, S, M, </size>
            <color>Coral</color>
        </product>
        <product>
            <id>3042738-Violet</id>
            <name><![CDATA[Διπλό ζέρσεϋ μίντι φόρεμα (Violet)]]></name>
            <link><![CDATA[http://dev.my-domain.com/3042738-violet-diplo-zersey-midi-forema.html]]></link>
            <image><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-violet-diplo-zersey-midi-forema-01.jpg]]></image>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-03.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-02.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-04.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-01.jpg]]></additionalimage>
            <category><![CDATA[Κατηγορίες > Γυναίκα > MY FIRM > Φορέματα > Φορέματα]]></category>
            <price_with_vat>71.00</price_with_vat>
            <manufacturer><![CDATA[MY FIRM]]></manufacturer>
            <description><![CDATA[Διπλό μίντι φόρεμα με μπουφάν λάστιχο στην μέση]]></description>
            <instock>Y</instock>
            <availability>In Stock</availability>
            <shipping>0</shipping>
            <size>XS, S, M, </size>
            <color>Violet</color>
        </product>
        <product>
            <id>3055011-Coral</id>
            <name><![CDATA[Ανάλαφρο μίντι φόρεμα με λεπτές τιράντες (Coral)]]></name>
            <link><![CDATA[http://dev.my-domain.com/3055011-coral-analafro-midi-forema-leptes-tirantes.html]]></link>
            <image><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3055011-coral-analafro-midi-forema-leptes-tirantes-01.jpg]]></image>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-03.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-02.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-04.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-01.jpg]]></additionalimage>
            <category><![CDATA[Κατηγορίες > Γυναίκα > MY FIRM > Φορέματα > Φορέματα]]></category>
            <price_with_vat>97.00</price_with_vat>
            <manufacturer><![CDATA[MY FIRM]]></manufacturer>
            <description><![CDATA[Αέρινο, δροσερό μίντι φόρεμα από διπλή γάζα. All day stylish look και για τις διακοπές και για την πόλη.]]></description>
            <instock>Y</instock>
            <availability>In Stock</availability>
            <shipping>0</shipping>
            <size>XS, S, M, </size>
            <color>Coral</color>
        </product>
        <product>
            <id>3055011-Fuchsia</id>
            <name><![CDATA[Ανάλαφρο μίντι φόρεμα με λεπτές τιράντες (Fuchsia)]]></name>
            <link><![CDATA[http://dev.my-domain.com/3055011-fuchsia-analafro-midi-forema-leptes-tirantes.html]]></link>
            <image><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3055011-fuchsia-analafro-midi-forema-leptes-tirantes-03.jpg]]></image>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-03.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-02.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-04.jpg]]></additionalimage>
            <additionalimage><![CDATA[http://dev.my-domain.com/media/catalog/product/3/0/3042738-coral-diplo-zersey-midi-forema-01.jpg]]></additionalimage>
            <category><![CDATA[Κατηγορίες > Γυναίκα > MY FIRM > Φορέματα > Φορέματα]]></category>
            <price_with_vat>97.00</price_with_vat>
            <manufacturer><![CDATA[MY FIRM]]></manufacturer>
            <description><![CDATA[Αέρινο, δροσερό μίντι φόρεμα από διπλή γάζα. All day stylish look και για τις διακοπές και για την πόλη.]]></description>
            <instock>Y</instock>
            <availability>In Stock</availability>
            <shipping>0</shipping>
            <size>XS, S, M, </size>
            <color>Fuchsia</color>
        </product>
    </products>
</mywebstore>

As you can see, after the first product, <additionalimage>, <category>,<size> are the same (they shouldn't)! It is repeating the first product's values?!

I think it something so obvious that I can not see it :(
Please advise.

2 Answers 2

1

In this code You have to check the product type before getting the additional image, category and size. and need to add the condition there like if product type is not configurable then what you are doing is perfect.

But in case of product type is configurable then you have to load all simple product of that product and then get all the category and its image for each product as you are doing.

This will resolve your issue and let me know if not.

1
  • Could you please explain in code what you mean?
    – kanenas
    Commented Aug 26, 2016 at 10:04
1

I am answering my own question. Here is the code I used to create the xml feed:

ini_set('display_errors', 1);
set_time_limit(0);

define('FEED_LOCATION','feeds/xml-feed.xml');
define('LIVE_EL_URL','https://www.my-domain.com/');
define('DEV_EL_URL','http://dev.my-domain.com/');

require_once 'app/Mage.php';

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::app('default');

try{
    $handle = fopen(FEED_LOCATION, 'w');
    $heading = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
    $heading .= '<mywebstore>'."\r\n";
    $heading .= "\t"."<created_at>".date('Y-m-d G:i')."</created_at>"."\r\n";
    $heading .= "\t".'<products>'."\r\n";
    $feed_line = $heading;
    fwrite($handle, $feed_line);

    //GET THE PRODUCTS
    $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToFilter('status', 1); //Enabled
    $products->addAttributeToFilter('visibility', 4); //Catalog, Search
    $products->addAttributeToSelect('*');
    $prodIds = $products->getAllIds();
    $product = Mage::getModel('catalog/product');
    foreach($prodIds as $productId) {
        $product->load($productId);

        $productType = $product->getTypeID();
        if($productType == 'configurable') {
            $productgetId = Mage::getModel('catalog/product')->load($product->getId());

            $product_data = array();
            $product_data['start']  = "\t\t".'<product>'."\r\n";
            $product_data['id']     = "\t".'<id>'.$product->getSku().'</id>'."\r\n"; //ID
            $product_data['name']   = "\t".'<name><![CDATA['.$product->getName().' ('.$product->getResource()->getAttribute('color')->getFrontend()->getValue($product).')]]></name>'."\r\n"; //Name
            $product_data['link']   = "\t".'<link><![CDATA['.LIVE_EL_URL.$product->getUrlPath().']]></link>'."\r\n"; //URL
            $product_data['image']  = "\t".'<image><![CDATA['.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage().']]></image>'."\r\n"; //Image

            foreach($productgetId->getMediaGalleryImages() as $image) {
                $product_data['additionalimage'] .= "\t".'<additionalimage><![CDATA['.$image->getUrl().']]></additionalimage>'."\r\n"; //Additional images
            }

            $categoriesList = $productgetId->getCategoryIds();
            $categoryId = end($categoriesList);
            $category = Mage::getModel('catalog/category')->load($categoryId);
            $product_data['category'] = "\t".'<category><![CDATA['.$category->getName().']]></category>'."\r\n"; //Category

            $normal_price = number_format($product->getPrice(), 2, '.', ''); //Price
            $special_price = number_format($product->getSpecialPrice(), 2, '.', ''); //Special Price
            $price = "";
            $todayDate = strtotime(date('Y-m-d H:i:s'));
            $special_to_date = strtotime($product->getSpecialToDate());
            if($special_price !== "0.00"){
                if($special_to_date) {
                    if($todayDate - $special_to_date > 0){
                        $price = $normal_price;
                    } else {
                        $price = $special_price;
                    }
                } else {
                    $price = $special_price;
                }
            } else {
                $price = $normal_price;
            }
            $product_data['price'] = "\t".'<price_with_vat>'.$price.'</price_with_vat>'."\r\n";

            $product_data['manufacturer'] = "\t".'<manufacturer><![CDATA['.$product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($product).']]></manufacturer>'."\r\n"; // Manufacturer
            $product_data['description'] = "\t".'<description><![CDATA['.$product->getShortDescription().']]></description>'."\r\n"; // Description

            $product_data['instock'] = "\t".'<instock>Y</instock>'."\r\n"; // Y: InStock, N: NOT InStock
            $product_data['availability'] = "\t".'<availability>Available</availability>'."\r\n";
            $product_data['shipping'] = "\t".'<shipping>0</shipping>'."\r\n"; // 0: Free Shipping

            $sizes = "";
            foreach($productgetId->getTypeInstance(true)->getUsedProducts(null, $productgetId) as $simple) {
                $associatedStock = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
                if($associatedStock !== 0) {
                    // There MUST be at least one Associated Product with size!
                    $sizes .= $simple->getResource()->getAttribute('size')->getFrontend()->getValue($simple).', ';
                }
            }
            $product_data['size'] = "\t".'<size>'.rtrim($sizes,', ').'</size>'."\r\n";

            $product_data['color'] = "\t".'<color>'.$product->getResource()->getAttribute('color')->getFrontend()->getValue($product).'</color>'."\r\n";

            $product_data['end'] = '</product>';

            foreach($product_data as $k=>$val){
                $product_data[$k] = $val;
            }

            $feed_line = implode("\t\t", $product_data)."\r\n";
            fwrite($handle, $feed_line);
            fflush($handle);
        }
    }

    $footer = "\t".'</products>'."\r\n";
    $footer .= '</mywebstore>';
    $feed_line = $footer;
    fwrite($handle, $feed_line);
    fclose($handle);
} catch(Exception $e) {
    die($e->getMessage());
}

Remember to comment the first line when in production.
I know the code looks like spaghetti and needs to be refactored.
Any suggestions would be more than welcomed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.