Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Quazz authored Sep 8, 2020
1 parent e261ba9 commit cf87fdc
Showing 1 changed file with 103 additions and 28 deletions.
131 changes: 103 additions & 28 deletions Model/Xmlfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Magefox\GoogleShopping\Model;

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magefox\GoogleShopping\Helper\Data;
use Magefox\GoogleShopping\Helper\Products;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Filesystem\DirectoryList;

class Xmlfeed
{
/**
Expand All @@ -26,14 +32,17 @@ class Xmlfeed
private $_storeManager;

public function __construct(
\Magefox\GoogleShopping\Helper\Data $helper,
\Magefox\GoogleShopping\Helper\Products $productFeedHelper,
\Magento\Store\Model\StoreManagerInterface $storeManager

Data $helper,
Products $productFeedHelper,
StoreManagerInterface $storeManager,
DirectoryList $directoryList,
CollectionFactory $categoryCollection
) {
$this->_helper = $helper;
$this->_productFeedHelper = $productFeedHelper;
$this->_storeManager = $storeManager;
$this->directoryList = $directoryList;
$this->categoryCollection = $categoryCollection;
}

public function getFeed()
Expand All @@ -45,9 +54,20 @@ public function getFeed()
return $xml;
}

public function getXmlHeader()
public function getFeedFile()
{
$pubDir = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::PUB);

$fileName = "googleshopping.xml";
//phpcs:ignore
$fileData = file_get_contents($fileName);

return $fileData;
}

public function getXmlHeader()
{
//phpcs:ignore
header("Content-Type: application/xml; charset=utf-8");

$xml = '<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">';
Expand All @@ -57,7 +77,6 @@ public function getXmlHeader()
$xml .= '<description>'.$this->_helper->getConfig('google_default_description').'</description>';

return $xml;

}

public function getXmlFooter()
Expand All @@ -70,44 +89,72 @@ public function getProductsXml()
$productCollection = $this->_productFeedHelper->getFilteredProducts();
$xml = "";

foreach ($productCollection as $product)
{
if (!empty($product->getData('ean'))) {
$xml .= "<item>".$this->buildProductXml($product)."</item>";
}
foreach ($productCollection as $product) {
if (!empty($product->getData('ean'))) {
$xml .= "<item>".$this->buildProductXml($product)."</item>";
}
}

return $xml;
}

public function buildProductXml($product)
{
$storeId = 21;
$_description = $this->fixDescription($product->getShortDescription());
$xml = $this->createNode("title", $product->getName(), true);
$xml .= $this->createNode("link", $product->getProductUrl());
$xml .= $this->createNode(
"link",
$product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true])
);
$xml .= $this->createNode("description", $_description, true);
//$xml .= $this->createNode("g:product_type", $this->_productFeedHelper->getAttributeSet($product), true);
$xml .= $this->createNode("g:image_link", $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA, true).'catalog/product'.$product->getImage());
$xml .= $this->createNode('g:google_product_category',
$this->_productFeedHelper->getProductValue($product, 'google_product_category'), true);
$xml .= $this->createNode(
"g:image_link",
$this->_storeManager->getStore()->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA,
true
).'catalog/product'.$product->getImage()
);
$xml .= $this->createNode(
'g:google_product_category',
$this->_productFeedHelper->getProductValue($product, 'google_product_category'),
true
);
$xml .= $this->createNode("g:availability", 'in stock');
$xml .= $this->createNode('g:price', number_format($product->getFinalPrice(),2,'.','').' '.$this->_productFeedHelper->getCurrentCurrencySymbol());
if (($product->getSpecialPrice() < $product->getFinalPrice()) && !empty($product->getSpecialPrice()))
$xml .= $this->createNode('g:sale_price', number_format($product->getSpecialPrice(),2,'.','').' '.$this->_productFeedHelper->getCurrentCurrencySymbol());
$xml .= $this->createNode(
'g:price',
number_format(
$product->getFinalPrice(),
2,
'.',
''
).' '.$this->_productFeedHelper->getCurrentCurrencySymbol()
);
if (($product->getSpecialPrice() < $product->getFinalPrice()) && !empty($product->getSpecialPrice())) {
$xml .= $this->createNode(
'g:sale_price',
number_format(
$product->getSpecialPrice(),
2,
'.',
''
).' '.$this->_productFeedHelper->getCurrentCurrencySymbol()
);
}
$_condition = $this->_productFeedHelper->getProductValue($product, 'google_condition');
if (is_array($_condition)) {
$xml .= $this->createNode("g:condition", $_condition[0]);
}
else if ($_condition === "Refurbished") {
} elseif ($_condition === "Refurbished") {
$xml .= $this->createNode("g:condition", "refurbished");
}
else {
$xml .= $this->createNode("g:condition", $this->_helper->getConfig('default_google_condition'));
}
} else {
$xml .= $this->createNode("g:condition", $this->_helper->getConfig('default_google_condition'));
}
$xml .= $this->createNode("g:gtin", $product->getData('ean'));
$xml .= $this->createNode("g:id", $product->getId());
$xml .= $this->createNode("g:brand", $product->getAttributeText('brand'));
$xml .= $this->createNode("g:mpn", $product->getData('mpn'));
$xml .= $this->createNode("g:product_type", $this->getProductCategories($product), true);

return $xml;
}
Expand All @@ -123,16 +170,14 @@ public function fixDescription($data)

public function createNode($nodeName, $value, $cData = false)
{
if (empty($value) || empty ($nodeName))
{
if (empty($value) || empty($nodeName)) {
return false;
}

$cDataStart = "";
$cDataEnd = "";

if ($cData === true)
{
if ($cData === true) {
$cDataStart = "<![CDATA[";
$cDataEnd = "]]>";
}
Expand All @@ -142,4 +187,34 @@ public function createNode($nodeName, $value, $cData = false)
return $node;
}

public function getFilteredCollection($categoryIds)
{
$collection = $this->categoryCollection->create();
$filtered_colection = $collection
->addFieldToSelect('*')
->addFieldToFilter(
'entity_id',
['in' => $categoryIds]
)
->setOrder('level', 'ASC')
->load();
return $filtered_colection;
}

private function getProductCategories($product)
{
$categoryIds = $product->getCategoryIds();
$categoryCollection = $this->getFilteredCollection($categoryIds);
$fullcategory = "";
$i = 0;
foreach ($categoryCollection as $category) {
$i++;
if ($i != $categoryCollection->getSize()) {
$fullcategory .= $category->getData('name') . ' > ';
} else {
$fullcategory .= $category->getData('name');
}
}
return $fullcategory;
}
}

0 comments on commit cf87fdc

Please sign in to comment.