Skip to content

Commit

Permalink
Fixed 500 Controller error. Fixed rendering page error by replacing g…
Browse files Browse the repository at this point in the history
…etAttributeText function in Xmlfeed.php.

This closes #2 and closes #6
  • Loading branch information
adampmoss authored and br33f committed Apr 17, 2020
2 parents 419aca8 + 8ac6caf commit 4714ef9
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 47 deletions.
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');
}
}
}

}
36 changes: 32 additions & 4 deletions Helper/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,47 @@ class Products extends \Magento\Framework\App\Helper\AbstractHelper
*/
protected $_helper;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
public $_storeManager;

/**
* @var \Magento\Catalog\Model\Product\Attribute\Source\Status
*/
public $_productStatus;

/**
* @var \Magento\Catalog\Model\Product\Visibility
*/
public $_productVisibility;

public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Eav\Model\AttributeSetRepository $attributeSetRepo,
\Magefox\GoogleShopping\Helper\Data $helper
\Magefox\GoogleShopping\Helper\Data $helper,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
\Magento\Catalog\Model\Product\Visibility $productVisibility
)
{
$this->_productCollectionFactory = $productCollectionFactory;
$this->_attributeSetRepo = $attributeSetRepo;
$this->_helper = $helper;
$this->_storeManager = $storeManager;
$this->_productStatus = $productStatus;
$this->_productVisibility = $productVisibility;
parent::__construct($context);
}

public function getFilteredProducts()
{
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->setPageSize(200);
// $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
$collection->addAttributeToFilter('status', ['in' => $this->_productStatus->getVisibleStatusIds()]);
$collection->setVisibility($this->_productVisibility->getVisibleInSiteIds());

return $collection;
}

Expand Down Expand Up @@ -66,4 +89,9 @@ public function getProductValue($product, $attributeCode)

return false;
}
}

public function getCurrentCurrencySymbol()
{
return $this->_storeManager->getStore()->getCurrentCurrencyCode();
}
}
128 changes: 96 additions & 32 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,77 +59,127 @@ 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>Store Name</title>';
$xml .= '<link>Base URL</link>';
$xml .= '<description>Store Name</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", $product->getDescription(), true);
$xml .= $this->createNode("g:product_type", $this->_productFeedHelper->getAttributeSet($product));
$xml .= $this->createNode("g:image_link", $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA, true).'catalog/product'.$product->getImage());
$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 .= "<g:availability>".$product->getId()."</g:availability>";
$xml .= "<g:condition>".$product->getId()."</g:condition>";
$xml .= "<g:id>".$product->getSku()."</g:id>";
$xml .= "<g:brand>".$product->getId()."</g:brand>";
$xml .= "<g:mpn>".$product->getId()."</g:mpn>";
$xml .= "<g:tax></g:tax>";*/

$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())
$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", $this->getAttributeText($product, 'gr_ean'));
$xml .= $this->createNode("g:id", $product->getId());
$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;
$encode = mb_detect_encoding($data);
$description = mb_convert_encoding($description, 'UTF-8', $encode);

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;
}

}
}
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Google Shooping Feed for Magento 2

__Installation__

1. Create the directory (if it does not exist) that will hold the module contents
`$ mkdir -p Magefox/GoogleShopping` on app/code directory

2. Upload files to Magefox/GoogleShopping files

3. Execute Magento setup upgrade

$ bin/magento setup:upgrade

4. Clean cache and generated code

$ bin/magento cache:clean


Login to Magento Admin and fill out info on Store > Configuration > Magento Fox > Google Shooping

To access the feed : www.website.com/magefoxgoogleshopping/

Loading

0 comments on commit 4714ef9

Please sign in to comment.