Skip to content

Commit

Permalink
Merge pull request adampmoss#3 from silici0/master
Browse files Browse the repository at this point in the history
Fix and Improve
  • Loading branch information
adampmoss authored Apr 20, 2018
2 parents 419aca8 + 8ac6caf commit 9025241
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 18 deletions.
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();
}
}
45 changes: 31 additions & 14 deletions Model/Xmlfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public function getFeed()

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 .= '<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;

Expand All @@ -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 .= "<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 = $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))
Expand All @@ -118,4 +135,4 @@ public function createNode($nodeName, $value, $cData = false)
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/

19 changes: 19 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
<label>Google Shopping Category</label>
<comment>Attribute code used for Google Shopping Category</comment>
</field>
<field id="google_default_title" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<depends>
<field id="enable">1</field>
</depends>
<label>Title on Google Tittle Tag</label>
</field>
<field id="google_default_url" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
<depends>
<field id="enable">1</field>
</depends>
<label>Url on Google link Tag</label>
<comment>Should use http:// or https://</comment>
</field>
<field id="google_default_description" translate="label" type="text" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
<depends>
<field id="enable">1</field>
</depends>
<label>Description on Google Description Tag</label>
</field>
</group>
</section>
</system>
Expand Down

0 comments on commit 9025241

Please sign in to comment.