diff --git a/Controller/Index/Index.php b/Controller/Index/Index.php index f1ba0d9..e44d441 100644 --- a/Controller/Index/Index.php +++ b/Controller/Index/Index.php @@ -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(); @@ -47,4 +61,5 @@ public function execute() $resultForward->forward('noroute'); } } -} \ No newline at end of file + +} diff --git a/Model/Xmlfeed.php b/Model/Xmlfeed.php index 384ce16..735ac12 100644 --- a/Model/Xmlfeed.php +++ b/Model/Xmlfeed.php @@ -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(); @@ -45,39 +59,50 @@ public function getFeed() return $xml; } + /** + * @return string + */ public function getXmlHeader() { - header("Content-Type: application/xml; charset=utf-8"); - $xml = ''; + $xml = ''; $xml .= ''; - $xml .= ''.$this->_helper->getConfig('google_default_title').''; - $xml .= ''.$this->_helper->getConfig('google_default_url').''; - $xml .= ''.$this->_helper->getConfig('google_default_description').''; + $xml .= '' . $this->_helper->getConfig('google_default_title') . ''; + $xml .= '' . $this->_helper->getConfig('google_default_url') . ''; + $xml .= '' . $this->_helper->getConfig('google_default_description') . ''; return $xml; } + /** + * @return string + */ public function getXmlFooter() { - return ''; + return ''; } + /** + * @return string + */ public function getProductsXml() { $productCollection = $this->_productFeedHelper->getFilteredProducts(); $xml = ""; - foreach ($productCollection as $product) - { - $xml .= "".$this->buildProductXml($product).""; + foreach ($productCollection as $product) { + $xml .= "" . $this->buildProductXml($product) . ""; } return $xml; } + /** + * @param $product + * @return bool|string + */ public function buildProductXml($product) { $_description = $this->fixDescription($product->getDescription()); @@ -85,26 +110,44 @@ public function buildProductXml($product) $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; @@ -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 = ""; } - $node = "<".$nodeName.">".$cDataStart.$value.$cDataEnd.""; + $node = "<" . $nodeName . ">" . $cDataStart . $value . $cDataEnd . ""; return $node; }