Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some issues #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions Controller/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,55 @@

namespace Magefox\GoogleShopping\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
use Magefox\GoogleShopping\Helper\Data;
use Magefox\GoogleShopping\Model\Xmlfeed;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\ForwardFactory;

class Index extends Action
{
/**
* XmlFeed Model
*
* @var \Magefox\GoogleShopping\Model\Xmlfeed
* @var Xmlfeed
*/
protected $xmlFeed;

/**
* General Helper
*
* @var \Magefox\GoogleShopping\Helper\Data
* @var Data
*/
private $helper;

/**
* Result Forward Factory
*
* @var \Magefox\GoogleShopping\Helper\Data
* @var Data
*/
private $resultForward;

/**
* Index constructor.
* @param Context $context
* @param Xmlfeed $xmlFeed
* @param Data $helper
* @param ForwardFactory $resultForwardFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magefox\GoogleShopping\Model\Xmlfeed $xmlFeed,
\Magefox\GoogleShopping\Helper\Data $helper,
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
) {
Context $context,
Xmlfeed $xmlFeed,
Data $helper,
ForwardFactory $resultForwardFactory
)
{
$this->xmlFeed = $xmlFeed;
$this->helper = $helper;
$this->resultForwardFactory = $resultForwardFactory;
parent::__construct($context);
}

public function execute()
{
$resultForward = $this->resultForwardFactory->create();
Expand All @@ -47,4 +61,5 @@ public function execute()
$resultForward->forward('noroute');
}
}
}

}
101 changes: 74 additions & 27 deletions Model/Xmlfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,54 @@

namespace Magefox\GoogleShopping\Model;

use Magefox\GoogleShopping\Helper\Data;
use Magefox\GoogleShopping\Helper\Products;
use Magento\Store\Model\StoreManagerInterface;

class Xmlfeed
{
/**
* General Helper
*
* @var \Magefox\GoogleShopping\Helper\Data
* @var Data
*/
private $_helper;

/**
* Product Helper
*
* @var \Magefox\GoogleShopping\Helper\Products
* @var Products
*/
private $_productFeedHelper;

/**
* Store Manager
*
* @var \Magefox\GoogleShopping\Helper\Products
* @var Products
*/
private $_storeManager;

/**
* Xmlfeed constructor.
* @param Data $helper
* @param Products $productFeedHelper
* @param StoreManagerInterface $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

) {
)
{
$this->_helper = $helper;
$this->_productFeedHelper = $productFeedHelper;
$this->_storeManager = $storeManager;
}

/**
* @return string
*/
public function getFeed()
{
$xml = $this->getXmlHeader();
Expand All @@ -45,66 +59,95 @@ public function getFeed()
return $xml;
}

/**
* @return string
*/
public function getXmlHeader()
{

header("Content-Type: application/xml; charset=utf-8");

$xml = '<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">';
$xml = '<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">';
$xml .= '<channel>';
$xml .= '<title>'.$this->_helper->getConfig('google_default_title').'</title>';
$xml .= '<link>'.$this->_helper->getConfig('google_default_url').'</link>';
$xml .= '<description>'.$this->_helper->getConfig('google_default_description').'</description>';
$xml .= '<title>' . $this->_helper->getConfig('google_default_title') . '</title>';
$xml .= '<link>' . $this->_helper->getConfig('google_default_url') . '</link>';
$xml .= '<description>' . $this->_helper->getConfig('google_default_description') . '</description>';

return $xml;

}

/**
* @return string
*/
public function getXmlFooter()
{
return '</channel></rss>';
return '</channel></rss>';
}

/**
* @return string
*/
public function getProductsXml()
{
$productCollection = $this->_productFeedHelper->getFilteredProducts();
$xml = "";

foreach ($productCollection as $product)
{
$xml .= "<item>".$this->buildProductXml($product)."</item>";
foreach ($productCollection as $product) {
$xml .= "<item>" . $this->buildProductXml($product) . "</item>";
}

return $xml;
}

/**
* @param $product
* @return bool|string
*/
public function buildProductXml($product)
{
$_description = $this->fixDescription($product->getDescription());
$xml = $this->createNode("title", $product->getName(), true);
$xml .= $this->createNode("link", $product->getProductUrl());
$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: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());
$xml .= $this->createNode('g:price', number_format($product->getFinalPrice(), 2, '.', '') . ' ' . $this->_productFeedHelper->getCurrentCurrencySymbol());
if ($product->getSpecialPrice() != $product->getFinalPrice())
$xml .= $this->createNode('g:sale_price', number_format($product->getSpecialPrice(),2,'.','').' '.$this->_productFeedHelper->getCurrentCurrencySymbol());
$_condition = $product->getAttributeText('condition');
$xml .= $this->createNode('g:sale_price', number_format($product->getSpecialPrice(), 2, '.', '') . ' ' . $this->_productFeedHelper->getCurrentCurrencySymbol());
$_condition = $this->getAttributeText($product, 'condition');
if (is_array($_condition))
$xml .= $this->createNode("g:condition", $_condition[0]);
else
$xml .= $this->createNode("g:condition", $_condition);
$xml .= $this->createNode("g:gtin", $product->getAttributeText('gr_ean'));
$xml .= $this->createNode("g:gtin", $this->getAttributeText($product, 'gr_ean'));
$xml .= $this->createNode("g:id", $product->getId());
$xml .= $this->createNode("g:brand", $product->getAttributeText('manufacturer'));
$xml .= $this->createNode("g:brand", $this->getAttributeText($product, 'manufacturer'));
$xml .= $this->createNode("g:mpn", $product->getSku());

return $xml;
}

/**
* @param $product
* @param $attributeCode
* @return bool
*/
protected function getAttributeText($product, $attributeCode)
{
if ($attribute = $product->getResource()->getAttribute($attributeCode)) {
return $attribute->getSource()->getOptionText($product->getData($attributeCode));
} else {
return false;
}
}

/**
* @param $data
* @return string
*/
public function fixDescription($data)
{
$description = $data;
Expand All @@ -114,23 +157,27 @@ public function fixDescription($data)
return $description;
}

/**
* @param $nodeName
* @param $value
* @param bool $cData
* @return bool|string
*/
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 = "]]>";
}

$node = "<".$nodeName.">".$cDataStart.$value.$cDataEnd."</".$nodeName.">";
$node = "<" . $nodeName . ">" . $cDataStart . $value . $cDataEnd . "</" . $nodeName . ">";

return $node;
}
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.