Um einen XML Product Feed in Magento 1 zu erstellen, kann die im folgenden beschriebene Vorgehensweise genutzt werden. Alternativ können hierfür auch spezielle (jedoch i.d.R. kostenpflichtige) Plugins zum Einsatz kommen.
Kurzbeschreibung des Vorgehens:
Das Folgende PHP-Script erstellt den notwendigen Produkt-Export als XML-Feed. Dabei bedient sich das Script der bestehenden Magento-Schnittstelle und beinhaltet keine sicherheitsrelevanten Informationen (Passwörter o.ä.).
Der Dateiname kann beliebig gewählt werden, bspw. “scalerion-feed.php”.
<?php
error_reporting(E_ALL | E_STRICT);
require 'app/Mage.php';
ini_set('display_errors', 1);
Mage::app();
$storeId = 1; // the store id with products for the export
$imageStoreId = 0; // the store id containing the product images
$gtinAttributeName = "gtin"; // attribute used for GTINs
$brandAttributeName = "manufacturer"; // attribute used for brand names
$colorAttributeName = "color"; // attribute used for colors
$sizeAttributeName = "size"; // attribute used for sizes
$taxClassIdStandard = 2; // tax class id for standard VAT rate
$taxClassIdReduced = 5; // tax class id for reduced VAT rate
$taxClassIdZero = 6; // tax class id for zero VAT rate
$itemGroupArray;
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load($storeId));
try {
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->setOrder('id', 'ASC');
$doc = new DOMDocument();
$doc->encoding = 'utf-8';
$doc->formatOutput = true;
$rss = $doc->createElement("rss");
$rss->setAttributeNS('<http://www.w3.org/2000/xmlns/>' ,'xmlns:g', '<http://base.google.com/ns/1.0>');
$doc->appendChild($rss);
$channel = $doc->createElement("channel");
$rss->appendChild($channel);
foreach ($products as $product) {
if($product->isConfigurable()) {
continue; // no export of parent products
}
$item = $doc->createElement("item");
$id = $doc->createElement("g:id");
$id->appendChild(
$doc->createCDATASection($product->getId())
);
$item->appendChild($id);
$_gtin = $product->getData($gtinAttributeName);
if( !$_gtin ) {
continue;
}
$gtin = $doc->createElement("g:gtin");
$gtin->appendChild(
$doc->createCDATASection($_gtin)
);
$item->appendChild($gtin);
$_brand = $product->getResource()->getAttribute($brandAttributeName)->getFrontend()->getValue($product);
if( !$_brand ) {
continue;
}
$brand = $doc->createElement("g:brand");
$brand->appendChild(
$doc->createCDATASection($_brand)
);
$item->appendChild($brand);
$title = $doc->createElement("g:title");
$title->appendChild(
$doc->createCDATASection($product->getName())
);
$item->appendChild($title);
$description = $doc->createElement("g:description");
$description->appendChild(
$doc->createCDATASection($product->getDescription())
);
$item->appendChild($description);
$temp_product = Mage::getModel('catalog/product')->setStoreId($imageStoreId)->load($product->getId());
if( $temp_product ) {
$images = $temp_product->getMediaGalleryImages();
if( $images ) {
$i = 0;
foreach($images as $image) {
if( $i == 0 ) {
$image_link = $doc->createElement("g:image_link");
$image_link->appendChild(
$doc->createCDATASection($image['url'])
);
$item->appendChild($image_link);
$i++;
} else {
$additional_image_link = $doc->createElement("g:additional_image_link");
$additional_image_link->appendChild(
$doc->createCDATASection($image['url'])
);
$item->appendChild($additional_image_link);
}
}
if( $i == 0 ) {
$image_link = $doc->createElement("g:image_link");
$image_link->appendChild(
$doc->createCDATASection(Mage::helper('catalog/image')->init($product, 'image')->__toString())
);
$item->appendChild($image_link);
}
}
}
$availability = $doc->createElement("g:availability");
$isInStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getIsInStock();
$availability->appendChild(
$doc->createTextNode($product->getStatus() === "1" && $isInStock ? "in_stock" : "out_of_stock")
);
$item->appendChild($availability);
$price = $doc->createElement("g:price");
$price->appendChild(
$doc->createTextNode(trim((float)$product->getPrice())." EUR")
);
$item->appendChild($price);
if( $product->getSpecialPrice() > 0 ) {
$sale_price = $doc->createElement("g:sale_price");
$sale_price->appendChild(
$doc->createTextNode(trim((float)$product->getSpecialPrice())." EUR")
);
$item->appendChild($sale_price);
}
$_taxClassId = $product->getData("tax_class_id");
$tax = $doc->createElement("g:tax");
if( $_taxClassId == $taxClassIdStandard ) {
$tax->appendChild(
$doc->createTextNode("DE::19")
);
} else if( $_taxClassId == $taxClassIdReduced ) {
$tax->appendChild(
$doc->createTextNode("DE::7")
);
} else if( $_taxClassId == $taxClassIdZero ) {
$tax->appendChild(
$doc->createTextNode("DE::0")
);
}
$item->appendChild($tax);
$mpn = $doc->createElement("g:mpn");
$mpn->appendChild(
$doc->createCDATASection($product->getSku())
);
$item->appendChild($mpn);
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
if( $parentIds ) {
$parent = Mage::getModel('catalog/product')->load($parentIds[0]);
if( $parent ) {
$_color = $product->getResource()->getAttribute($colorAttributeName);
if( $_color ) {
$_colorValue = $_color->getFrontend()->getValue($product);
$color = $doc->createElement("g:color");
$color->appendChild(
$doc->createCDATASection($_colorValue)
);
$item->appendChild($color);
}
$_size = $product->getResource()->getAttribute($sizeAttributeName);
if( $_size ) {
$_sizeValue = $_size->getFrontend()->getValue($product);
$size = $doc->createElement("g:size");
$size->appendChild(
$doc->createCDATASection($_sizeValue)
);
$item->appendChild($size);
}
$itemGroupId = $doc->createElement("g:item_group_id");
$itemGroupId->appendChild(
$doc->createCDATASection($parent->getId())
);
$item->appendChild($itemGroupId);
}
}
$adult = $doc->createElement("g:adult");
$adult->appendChild(
$doc->createTextNode("no")
);
$item->appendChild($adult);
$condition = $doc->createElement("g:condition");
$condition->appendChild(
$doc->createTextNode("new")
);
$item->appendChild($condition);
$curstock = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
$stock = $doc->createElement("g:stock");
if( $curstock ) {
$stock->appendChild(
$doc->createTextNode($curstock)
);
} else {
$stock->appendChild(
$doc->createTextNode(0)
);
}
$item->appendChild($stock);
$channel->appendChild($item);
}
print($doc->saveXML());
} catch (Exception $e) {
echo 'Error: - ';
echo $e->getMessage();
}
?>
Hinweise zum obigen Code:
Die erstellte Datei muss nun im Root-Verzeichnis der Magento-Installation auf dem Server abgelegt werden. Die Datei muss durch den Server lesbar sein, bitte prüfen Sie diesbez. die Zugriffsrechte der Datei.