Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace NamespaceVendorCron;
- use DOMDocument;
- class Productexport extends MagentoFrameworkAppActionAction
- {
- public function execute()
- {
- $objectManager = MagentoFrameworkAppObjectManager::getInstance();
- $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
- $date = $objDate->gmtDate();
- $strdate = str_replace(' ', '-', $date);
- $strd = str_replace(':', '-', $strdate);
- $filesystem = $objectManager->get('MagentoFrameworkFilesystem');
- $directoryList = $objectManager->get('MagentoFrameworkAppFilesystemDirectoryList');
- $media = $filesystem->getDirectoryWrite($directoryList::MEDIA)->getAbsolutePath();
- $products = $objectManager->create('MagentoCatalogModelProduct')->getCollection();
- $products->addAttributeToSelect('*')->load();
- $file = $media."product.xml";
- if (file_exists($file)) {
- rename($file, $media.'product-'.$strd.'.xml');
- }
- $doc = new DOMDocument();
- $doc->encoding = 'UTF-8';
- $doc->formatOutput = true;
- $root = $doc->createElement("products");
- $doc->appendChild($root);
- if ($products->getSize() > 0) {
- foreach ($products as $_product) {
- $product = $doc->createElement("product");
- $id = $doc->createElement("id");
- $id->appendChild(
- $doc->createTextNode($_product->getId())
- );
- $product->appendChild($id);
- $name = $doc->createElement("name");
- $name->appendChild(
- $doc->createTextNode(trim($_product->getName()))
- );
- $product->appendChild($name);
- $description = $doc->createElement("description");
- $str = strip_tags($_product->getDescription());
- //preg_replace('/[^a-zA-Z0-9_ %[].()%&-]/s', '', $str);
- $description->appendChild(
- $doc->createTextNode($str)
- );
- $product->appendChild($description);
- $url = $doc->createElement("url");
- $url->appendChild(
- $doc->createTextNode(trim($_product->getProductUrl()))
- );
- $product->appendChild($url);
- $brand = $doc->createElement("brand");
- $brand->appendChild(
- $doc->createTextNode($_product->getBrand())
- );
- $product->appendChild($brand);
- $price = $doc->createElement("price");
- $price->appendChild(
- $doc->createTextNode(trim((int)$_product->getPrice()))
- );
- $product->appendChild($price);
- $original_price = $doc->createElement("original_price");
- $original_price->appendChild(
- $doc->createTextNode(trim((int)$_product->getOriginalPrice()))
- );
- $product->appendChild($original_price);
- $currency = $doc->createElement("currency");
- $currency->appendChild(
- $doc->createTextNode($_product->getCurrency())
- );
- $product->appendChild($currency);
- $category = $doc->createElement("category");
- $category->appendChild(
- $doc->createTextNode($_product->getCategory())
- );
- $product->appendChild($category);
- $root->appendChild($product);
- }
- file_put_contents($file, $doc->saveXML(), FILE_APPEND);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement