Skip to content

Commit

Permalink
Merge pull request #13 from alpin11/feature/dynamic-google-options-se…
Browse files Browse the repository at this point in the history
…lect

Google shopping category provider
  • Loading branch information
David Höck authored Apr 7, 2021
2 parents 7d2cd6a + ca78d37 commit b889227
Show file tree
Hide file tree
Showing 3 changed files with 5,520 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php


namespace CoreShop\Bundle\GoogleShoppingBundle\Pimcore\DynamicOptionsProvider;


use Pimcore\Cache;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\DynamicOptionsProvider\SelectOptionsProviderInterface;
use Symfony\Component\Config\FileLocatorInterface;

class ProductCategoryOptionsProvider implements SelectOptionsProviderInterface
{
const CACHE_KEY = "google_shopping_taxonomy_with_ids";

/**
* @var FileLocatorInterface
*/
private FileLocatorInterface $fileLocator;

public function __construct(FileLocatorInterface $fileLocator)
{
$this->fileLocator = $fileLocator;
}

/**
* @inheritDoc
*/
public function getOptions($context, $fieldDefinition)
{
$options = [];

if (($fromCache = Cache::load(self::CACHE_KEY))) {
return $fromCache;
}

$sourcePath = $this->fileLocator->locate('@CoreShopGoogleShoppingBundle/Resources/google/taxonomy-with-ids.txt');
$handle = fopen($sourcePath, 'r');

if ($handle) {
while (($line = fgets($handle)) !== false) {
$result = [];

preg_match('/(\d+) - (.+)/', $line, $result);

$options[] = [
'key' => $result[2],
'value' => $result[1]
];
}

fclose($handle);
}

Cache::save($options, self::CACHE_KEY);

return $options;
}

/**
* @inheritDoc
*/
public function hasStaticOptions($context, $fieldDefinition)
{
return true;
}

/**
* @inheritDoc
*/
public function getDefaultValue($context, $fieldDefinition)
{
return $fieldDefinition->getDefaultValue();
}
}
31 changes: 18 additions & 13 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
imports:
- { resource: services/data_collector.yml }
- { resource: services/object_filter.yml }
- { resource: services/object_transformer.yml }
- { resource: services/distributor.yml }
- { resource: services/data_collector.yml }
- { resource: services/object_filter.yml }
- { resource: services/object_transformer.yml }
- { resource: services/distributor.yml }

services:
_defaults:
public: true
_defaults:
public: true

CoreShop\Bundle\GoogleShoppingBundle\Command\GoogleShoppingCommand:
arguments:
- '@coreshop.google_shopping.data_collector.default'
- '@coreshop.google_shopping.object_transformer'
- '@coreshop.google_shopping.distributor'
tags:
- { name: console.command }
CoreShop\Bundle\GoogleShoppingBundle\Command\GoogleShoppingCommand:
arguments:
- '@coreshop.google_shopping.data_collector.default'
- '@coreshop.google_shopping.object_transformer'
- '@coreshop.google_shopping.distributor'
tags:
- { name: console.command }

coreshop.google_shopping.dynamic_options_provider.product_category: '@CoreShop\Bundle\GoogleShoppingBundle\Pimcore\DynamicOptionsProvider\ProductCategoryOptionsProvider'
CoreShop\Bundle\GoogleShoppingBundle\Pimcore\DynamicOptionsProvider\ProductCategoryOptionsProvider:
arguments:
- '@file_locator'
Loading

0 comments on commit b889227

Please sign in to comment.