From 9b4dc4f867dd709271b369a4cd499d82d69d40dd Mon Sep 17 00:00:00 2001 From: Rafael Cunha Date: Wed, 19 Jul 2017 08:01:24 -0300 Subject: [PATCH] Bring all products that are vissible in catalog/search and show all nodes required for googleMerchantCenter --- Helper/Products.php | 36 ++++++++++++++++++++++++++++++++---- Model/Xmlfeed.php | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/Helper/Products.php b/Helper/Products.php index 4f92511..d9bc3f8 100644 --- a/Helper/Products.php +++ b/Helper/Products.php @@ -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; } @@ -66,4 +89,9 @@ public function getProductValue($product, $attributeCode) return false; } -} \ No newline at end of file + + public function getCurrentCurrencySymbol() + { + return $this->_storeManager->getStore()->getCurrentCurrencyCode(); + } +} diff --git a/Model/Xmlfeed.php b/Model/Xmlfeed.php index 4def692..384ce16 100644 --- a/Model/Xmlfeed.php +++ b/Model/Xmlfeed.php @@ -47,7 +47,7 @@ public function getFeed() public function getXmlHeader() { - + header("Content-Type: application/xml; charset=utf-8"); $xml = ''; @@ -80,23 +80,40 @@ public function getProductsXml() 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("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 .= "".$product->getId().""; - $xml .= "".$product->getId().""; - $xml .= "".$product->getSku().""; - $xml .= "".$product->getId().""; - $xml .= "".$product->getId().""; - $xml .= "";*/ - + $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 = $product->getAttributeText('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:id", $product->getId()); + $xml .= $this->createNode("g:brand", $product->getAttributeText('manufacturer')); + $xml .= $this->createNode("g:mpn", $product->getSku()); + return $xml; } + public function fixDescription($data) + { + $description = $data; + $encode = mb_detect_encoding($data); + $description = mb_convert_encoding($description, 'UTF-8', $encode); + + return $description; + } + public function createNode($nodeName, $value, $cData = false) { if (empty($value) || empty ($nodeName)) @@ -118,4 +135,4 @@ public function createNode($nodeName, $value, $cData = false) return $node; } -} \ No newline at end of file +}