Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. namespace NamespaceVendorCron;
  2. use DOMDocument;
  3. class Productexport extends MagentoFrameworkAppActionAction
  4. {
  5. public function execute()
  6. {
  7. $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  8. $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
  9. $date = $objDate->gmtDate();
  10. $strdate = str_replace(' ', '-', $date);
  11. $strd = str_replace(':', '-', $strdate);
  12. $filesystem = $objectManager->get('MagentoFrameworkFilesystem');
  13. $directoryList = $objectManager->get('MagentoFrameworkAppFilesystemDirectoryList');
  14. $media = $filesystem->getDirectoryWrite($directoryList::MEDIA)->getAbsolutePath();
  15. $products = $objectManager->create('MagentoCatalogModelProduct')->getCollection();
  16. $products->addAttributeToSelect('*')->load();
  17. $file = $media."product.xml";
  18. if (file_exists($file)) {
  19. rename($file, $media.'product-'.$strd.'.xml');
  20. }
  21. $doc = new DOMDocument();
  22. $doc->encoding = 'UTF-8';
  23. $doc->formatOutput = true;
  24. $root = $doc->createElement("products");
  25. $doc->appendChild($root);
  26. if ($products->getSize() > 0) {
  27. foreach ($products as $_product) {
  28. $product = $doc->createElement("product");
  29.  
  30. $id = $doc->createElement("id");
  31. $id->appendChild(
  32. $doc->createTextNode($_product->getId())
  33. );
  34. $product->appendChild($id);
  35.  
  36. $name = $doc->createElement("name");
  37. $name->appendChild(
  38. $doc->createTextNode(trim($_product->getName()))
  39. );
  40. $product->appendChild($name);
  41.  
  42. $description = $doc->createElement("description");
  43. $str = strip_tags($_product->getDescription());
  44. //preg_replace('/[^a-zA-Z0-9_ %[].()%&-]/s', '', $str);
  45. $description->appendChild(
  46. $doc->createTextNode($str)
  47. );
  48. $product->appendChild($description);
  49.  
  50. $url = $doc->createElement("url");
  51. $url->appendChild(
  52. $doc->createTextNode(trim($_product->getProductUrl()))
  53. );
  54. $product->appendChild($url);
  55.  
  56. $brand = $doc->createElement("brand");
  57. $brand->appendChild(
  58. $doc->createTextNode($_product->getBrand())
  59. );
  60. $product->appendChild($brand);
  61. $price = $doc->createElement("price");
  62. $price->appendChild(
  63. $doc->createTextNode(trim((int)$_product->getPrice()))
  64. );
  65. $product->appendChild($price);
  66.  
  67. $original_price = $doc->createElement("original_price");
  68. $original_price->appendChild(
  69. $doc->createTextNode(trim((int)$_product->getOriginalPrice()))
  70. );
  71. $product->appendChild($original_price);
  72.  
  73. $currency = $doc->createElement("currency");
  74. $currency->appendChild(
  75. $doc->createTextNode($_product->getCurrency())
  76. );
  77. $product->appendChild($currency);
  78.  
  79. $category = $doc->createElement("category");
  80. $category->appendChild(
  81. $doc->createTextNode($_product->getCategory())
  82. );
  83. $product->appendChild($category);
  84. $root->appendChild($product);
  85. }
  86. file_put_contents($file, $doc->saveXML(), FILE_APPEND);
  87. }
  88. }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement