Um einen XML Product Feed in Magento 2 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);
ini_set('display_errors', 1);
use Magento\\Framework\\App\\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\\Framework\\App\\State');
$state->setAreaCode('frontend');
$gtinAttributeName = "gtin";
$brandAttributeName = "manufacturer";
$defaultBrand = "My Brand & Co";
$colorAttributeName = "color";
$sizeAttributeName = "size";
$taxClassIdStandard = 2; // tax class id for standard VAT rate
$taxClassIdReduced = 5; // tax class id for reduced VAT rate
$taxClassIdZero = 0; // tax class id for zero VAT rate
try {
$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);
$storeManager = $objectManager->get('\\Magento\\Store\\Model\\StoreManagerInterface');
$mediaurl= $storeManager->getStore()->getBaseUrl(\\Magento\\Framework\\UrlInterface::URL_TYPE_MEDIA);
$stockRegistry = $objectManager->get('\\Magento\\CatalogInventory\\Model\\StockRegistry');
$productCollectionFactory = $objectManager->get('\\Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->setPageSize(3000);
foreach ($collection as $product) {
if( $product->getTypeId() === 'configurable' ) {
continue;
}
$stockStatus = $stockRegistry->getStockStatusBySku(
$product->getSku(),
$storeManager->getWebsite()->getId()
);
$stockData = $stockStatus->getStockItem();
$item = $doc->createElement("item");
$id = $doc->createElement("g:id");
$id->appendChild(
$doc->createCDATASection($product->getId())
);
$_gtin = $product->getData($gtinAttributeName); 47,3-17 Top
$parentIds = $objectManager->create('\\Magento\\ConfigurableProduct\\Model\\ResourceModel\\Product\\Type\\Configurable')->getParentIdsByChild($product->getId());
if( $parentIds ) {
$parentProduct = $objectManager->get('\\Magento\\Catalog\\Model\\ProductRepository')->getById($parentIds[0]);
if( $parentProduct ) {
$_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($parentProduct->getId())
);
$item->appendChild($itemGroupId);
}
}
$condition = $doc->createElement("g:condition");
$condition->appendChild(
$doc->createTextNode("new")
);
$item->appendChild($condition);
$curstock = $stockStatus->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.
Ab sofort ist der XML Product Feed abrufbar und listet alle Produkte auf.