0


I have the script below for exporting product xml feed for magento 1.9.2
The problem is that I cannot export the category name for each product

The scipt is:

require 'app/Mage.php';
Mage::app();
$storename = 'default';
$file = "products-top10.xml";
if (file_exists($file)) {
    unlink($file);
}
try {
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->setPageSize(200)
        ->setCurPage(1)
        ->setOrder('id', 'ASC')
        ->addAttributeToFilter('status', array('eq' => '1'));
    $doc = new DOMDocument();
    $doc->encoding = 'utf-8';
    $doc->formatOutput = true;
    $root = $doc->createElement("root");
    $doc->appendChild($root);
    $productsX = $doc->createElement("catalog");
    $root->appendChild($productsX);
     foreach ($products as $_product) {
            $product = $doc->createElement("product");
            $id = $doc->createElement("id");
            $id->appendChild(
                $doc->createTextNode($_product->getId())
            );
            $product->appendChild($id);
            $url = $doc->createElement("url");
            $url->appendChild(
                $doc->createTextNode(trim($_product->getData('url_key')))
            );
            $product->appendChild($imageURL);
            $imageURL = $doc->createElement("image");
            $imageURL->appendChild(
                $doc->createTextNode($_product->getImageURL())
            );
            $product->appendChild($url);
            $urlPath = $doc->createElement("url_path");
            $urlPath->appendChild(
                $doc->createTextNode(trim($_product->getProductUrl()))
            );
            $product->appendChild($urlPath);
            $title = $doc->createElement("title");
            $title->appendChild(
                $doc->createTextNode(trim($_product->getName()))
            );
            $product->appendChild($description);
            $description = $doc->createElement("description");
            $description->appendChild(
                $doc->createTextNode(trim($_product->getDescription()))
            );
            $product->appendChild($category);
            $category = $doc->createElement("category");
            $category->appendChild(
                $doc->createTextNode(trim($_product->getCategoryName()))
            );
            $product->appendChild($title);
            $sku = $doc->createElement("sku");
            $sku->appendChild(
                $doc->createTextNode($_product->getSku())
            );
            $product->appendChild($sku);
            $formatedpriceprice = $doc->createElement("price");
            $formatedpriceprice->appendChild(
                $doc->createTextNode(trim((int)$_product->getFormattedPrice()))
            );
            $product->appendChild($price);
            $formatedprice = $doc->createElement("price_with_vat");
            $formattedPrice = Mage::helper('core')->currency($_product->getPrice(), true, false);
            $formatedprice->appendChild(
                $doc->createTextNode(trim($formattedPrice))
            );
            $product->appendChild($formatedprice);
            $productsX->appendChild($product);
    }
    file_put_contents($file, $doc->saveXML(), FILE_APPEND);
} catch (Exception $e) {
    echo 'Eroror : - ';
    echo $e->getMessage();
}
  • What is the output that you are getting? It is likely that you will have to use $product->getCategoryIds() and then load the category from there. A good place to look might be app/code/core/Mage/Catalog/Model/Product/Api.php – mtr.web Jan 22 at 21:25
  • Hello. I tried it and it returns the same as before: <category/> – Costas Saloubardos Jan 22 at 21:36
  • Someone please? – Costas Saloubardos Jan 24 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.