Magento data feed guide

While Magento does provide a way to extract a product data feed from your shopping cart, it is essentially a snapshot of your database at the time you request it - there is no way to retrieve an up-to-date data feed automatically at any given time.

In order to keep your product data feed up-to-date for affiliates, we have created a script that you can use to generate an up-to-date product data feed on Magento. You can then specify that you want your data feed updated on the Commission Factory network automatically every night.

Simply download the attached PHP file, unzip it, and then upload the cf_magento_feed.php file to the Magento root directory on your server.

Once this file has been uploaded, you will be able to access an up-to-date product data feed at any time from a URL (eg. "http://www.yourdomain.com/cf_magento_feed.php"). You can then enter this URL in the Configure Feed page of your dashboard to enable automatic nightly updates.

 Download the Magento Data Feed script (.php file 4KB)

<?php

    header("Content-Disposition: attachment; filename=cf_magento_feed.csv");
    header("Content-Type: text/csv");

    require_once($_SERVER["DOCUMENT_ROOT"] . "/app/Mage.php");

    Mage::app();

    $baseUrl = Mage::app()->getStore(Mage::app()->getStore()->getStoreId())->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    $baseMediaUrl = Mage::getSingleton("catalog/product_media_config")->getBaseMediaUrl();

    $products = Mage::getModel("catalog/product")->getCollection();

    $products->addAttributeToFilter("status", 1);
    $products->addAttributeToFilter("visibility", array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG));
    $products->addAttributeToSelect("sku");

    print("SKU,Name,Category,Description,Url,Image,Price,Brand,Colour,PriceSale\r\n");

    foreach($products->getAllIds() as $productId)
    {
        $product = Mage::getModel("catalog/product")->load($productId);

        $product_data = array();

        $product_data["SKU"] = $product->getSku();
        $product_data["Name"] = $product->getName();
        $product_data["Category"] = "";
        $product_data["Description"] = trim(ereg_replace("^[ \t\r\n]+|[ \t\r\n]+", " ", strip_tags($product->getShortDescription())));
        $product_data["Url"] = $baseUrl . $product->getUrlKey() . Mage::getStoreConfig("catalog/seo/product_url_suffix");
        $product_data["Image"] = $baseMediaUrl . $product->getImage();
        $product_data["Price"] = $product->getFinalPrice();
        $product_data["Brand"] = $product->getAttributeText("manufacturer");
        $product_data["Colour"] = $product->getAttributeText("color");
        $product_data["PriceSale"] = $product->getAttributeText("special_price");

        foreach ($product->getCategoryIds() as $categoryId)
            $product_data["Category"] .= Mage::getModel("catalog/category")->load($categoryId)->getName() . ", ";

        $product_data["Category"] = rtrim($product_data["Category"], ", ");      

        foreach ($product_data as $k => $val)
        {
            $bad = array("\"", "\r\n", "\r");
            $good = array("\"\"", "\n", " ");

            $product_data[$k] = "\"" . str_replace($bad, $good, $val) . "\"";
        }

        $feed_line = implode(",", $product_data) . "\r\n";

        print($feed_line);
    }

?>