Skip to content

Commit

Permalink
Add support for Google Product Category
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz-Silpion committed Mar 21, 2019
1 parent 5c2fc9b commit 92effd5
Show file tree
Hide file tree
Showing 12 changed files with 11,018 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Model/Config/Source/CeneoCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public function getAllOptions()
}

$ceneoXml = $this->getCeneoXml();
$categories = ['label' => 'Please select', 'value' => ''];
$categories = [['label' => __('Please select'), 'value' => '']];

foreach ($ceneoXml as $category) {
$categories[] = array('label' => (string) $category->Name, 'value' => (string) $category->Name);
$categories[] = ['label' => (string) $category->Name, 'value' => (string) $category->Name];
$categories = array_merge($categories, $this->appendSubcategories($category, 1, $category->Name));
}

Expand Down
93 changes: 93 additions & 0 deletions Model/Config/Source/GoogleCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* Customizable datafeeds extension for Magento 2
*
* @category LCB
* @package LCB_Feeds
* @author Silpion Tomasz Gregorczyk <tom@leftcurlybracket.com>
*/

namespace LCB\Feeds\Model\Config\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class GoogleCategory extends AbstractSource
{

/**
* @var \Magento\Framework\Module\Dir\Reader
*/
protected $moduleReader;

/**
* @var \Magento\Backend\Model\Auth\Session $authSession
*/
protected $authSession;

/**
* @var array
*/
public $categories;

/**
* @param \Magento\Framework\Module\Dir\Reader $moduleReader
* @param \Magento\Backend\Model\Auth\Session $authSession
*/
public function __construct(
\Magento\Framework\Module\Dir\Reader $moduleReader,
\Magento\Backend\Model\Auth\Session $authSession
) {
$this->moduleReader = $moduleReader;
$this->authSession = $authSession;
}

/**
* Get Ceneo Categories
*
* @return array
*/
public function getAllOptions()
{

if ($this->categories) {
return $this->categories;
}

$locale = $this->authSession->getUser()->getInterfaceLocale();
$googleTaxonomy = $this->getGoogleTaxonomy($locale);
$categories = [['label' => __('Please select'), 'value' => '']];

$taxonomyArray = [];
foreach($googleTaxonomy as $taxonomyRow) {
$taxonomyRow = explode(" - ", trim($taxonomyRow));
if(isset($taxonomyRow[0]) && isset($taxonomyRow[1])) {
$categories[] = ['label' => $taxonomyRow[1], 'value' => $taxonomyRow[0]];
}
}

$this->categories = $categories;

return $this->categories;
}

/**
* @return array
*/
public function getGoogleTaxonomy($locale = 'en_US')
{
$sourceDir = $this->moduleReader->getModuleDir(
\Magento\Framework\Module\Dir::MODULE_ETC_DIR, 'LCB_Feeds'
);

if (!file_exists($sourceDir . "/source/google/$locale.txt")) {
$locale = 'en_US';
}

$taxonomyRaw = file_get_contents($sourceDir . "/source/google/$locale.txt", true);
$taxonomyArray = [];
return (array) explode("\n", trim($taxonomyRaw));

}

}
28 changes: 27 additions & 1 deletion Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function getSpecialPriceWithCurrency()
*/
public function getCurrencyCode()
{
return $this->_storeManager->getStore()->getCurrentCurrency()->getCode();
return $this->_storeManager->getCurrentCurrency()->getCode();
}

/**
Expand Down Expand Up @@ -248,6 +248,7 @@ public function getCeneoCategory()
if (!$this->getCategoryIds()) {
return '';
}

$categoryIds = array_reverse($this->getCategoryIds());
foreach ($categoryIds as $categoryId) {
$ceneoCategory = $this->categoryModel->load($categoryId)->getCeneoCategory();
Expand All @@ -259,4 +260,29 @@ public function getCeneoCategory()
return $ceneoCategory;
}

/**
* Get Google category
*
* @return string
*/
public function getGoogleCategory()
{

$googleCategory = '';

if (!$this->getCategoryIds()) {
return '';
}

$categoryIds = array_reverse($this->getCategoryIds());
foreach ($categoryIds as $categoryId) {
$googleCategory = $this->categoryModel->load($categoryId)->getGoogleCategory();
if ($googleCategory) {
break;
}
}

return $googleCategory;
}

}
1 change: 1 addition & 0 deletions Setup/Uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function uninstall(
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create();
$eavSetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'ceneo_category');
$eavSetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'google_category');
$setup->endSetup();
}

Expand Down
22 changes: 19 additions & 3 deletions Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public function __construct(EavSetupFactory $eavSetupFactory)
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if (version_compare($context->getVersion(), '1.0.4') < 0) {
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.0.4') < 0) {

$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY, 'ceneo_category', [
Expand All @@ -47,6 +46,23 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
);
}

if (version_compare($context->getVersion(), '1.0.6') < 0) {

$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY, 'google_category', [
'type' => 'text',
'label' => 'Google Category',
'input' => 'select',
'required' => false,
'sort_order' => 949,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'wysiwyg_enabled' => false,
'is_html_allowed_on_front' => false,
'group' => 'SEO',
]
);
}

$setup->endSetup();
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"type": "magento2-module",
"version": "1.0.5",
"version": "1.0.6",
"license": [
"OSL-3.0"
],
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="LCB_Feeds" setup_version="1.0.5"></module>
<module name="LCB_Feeds" setup_version="1.0.6"></module>
<sequence>
<module name="Magento_Backend"/>
<module name="Magento_Catalog"/>
Expand Down
Loading

0 comments on commit 92effd5

Please sign in to comment.