Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Sort configurable options by sort order. #129

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed/Improved
- Vsbridge Category Indexer - Save Mode: reindex subcategories if url_key has change in category ([#122](https://github.com/DivanteLtd/magento2-vsbridge-indexer/issues/122)).
- Sort configurable options by option sort order.

## [1.3.0] (2019.09.18)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types = 1);

/**
* @package Divante\VsbridgeIndexerCatalog
* @author Agata Firlejczyk <afirlejczyk@divante.pl>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCatalog\Model\Attribute;

/**
* Class LoadOptionById
*/
class LoadOptionById
{

/**
* @var LoadOptions
*/
private $loadOptions;

/**
* LoadOptionById constructor.
*
* @param LoadOptions $loadOptions
*/
public function __construct(LoadOptions $loadOptions)
{
$this->loadOptions = $loadOptions;
}

/**
* @param string $attributeCode
* @param int $optionId
* @param int $storeId
*
* @return array
*/
public function execute(string $attributeCode, int $optionId, int $storeId): array
{
$options = $this->loadOptions->execute($attributeCode, $storeId);

foreach ($options as $option) {
if ($optionId === (int)$option['value']) {
return $option;
}
}

return [];
}
}

This file was deleted.

119 changes: 119 additions & 0 deletions src/module-vsbridge-indexer-catalog/Model/Attribute/LoadOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types = 1);

/**
* @package Divante\VsbridgeIndexerCatalog
* @author Agata Firlejczyk <afirlejczyk@divante.pl>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCatalog\Model\Attribute;

use Divante\VsbridgeIndexerCatalog\Model\ResourceModel\Product\AttributeDataProvider;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection as OptionCollection;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory;

use Magento\Eav\Model\Entity\Attribute\Source\Table as SourceTable;

/**
* Class LoadOptionLabelById
*/
class LoadOptions
{
/**
* @var AttributeDataProvider
*/
private $attributeDataProvider;

/**
* @var CollectionFactory
*/
private $collectionFactory;

/**
* @var OptionCollectionToArray
*/
private $optionCollectionToArray;

/**
* @var array
*/
private $optionsByAttribute = [];

/**
* LoadOptions constructor.
*
* @param CollectionFactory $collectionFactory
* @param OptionCollectionToArray $optionCollectionToArray
* @param AttributeDataProvider $attributeDataProvider
*/
public function __construct(
CollectionFactory $collectionFactory,
OptionCollectionToArray $optionCollectionToArray,
AttributeDataProvider $attributeDataProvider
) {
$this->collectionFactory = $collectionFactory;
$this->attributeDataProvider = $attributeDataProvider;
$this->optionCollectionToArray = $optionCollectionToArray;
}

/**
* @param string $attributeCode
* @param int $storeId
*
* @return string
*/
public function execute(string $attributeCode, int $storeId): array
{
$attributeModel = $this->attributeDataProvider->getAttributeByCode($attributeCode);
$attributeModel->setStoreId($storeId);

return $this->loadOptions($attributeModel);
}

/**
* @param Attribute $attribute
*
* @return array
*/
private function loadOptions(Attribute $attribute): array
{
$key = $attribute->getId() . '_' . $attribute->getStoreId();

if (!isset($this->optionsByAttribute[$key])) {
$source = $attribute->getSource();

if (SourceTable::class !== get_class($source) &&
$source instanceof \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
) {
$options = $source->getAllOptions();
} else {
$attributeId = $attribute->getAttributeId();
$storeId = $attribute->getStoreId();

/** @var OptionCollection $options */
$options = $this->collectionFactory->create();
$options->setOrder('sort_order', 'asc');
$options->setAttributeFilter($attributeId)->setStoreFilter($storeId);
$options = $this->toOptionArray($options);
}

$this->optionsByAttribute[$key] = $options;
}

return $this->optionsByAttribute[$key];
}

/**
* @param OptionCollection $collection
*
* @return array
*/
private function toOptionArray(OptionCollection $collection): array
{
return $this->optionCollectionToArray->execute($collection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types = 1);

/**
* @package Divante\VsbridgeIndexerCatalog
* @author Agata Firlejczyk <afirlejczyk@divante.pl>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCatalog\Model\Attribute;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection as OptionCollection;

/**
* Class OptionCollectionToArray
*/
class OptionCollectionToArray
{

/**
* @param OptionCollection $collection
*
* @return array
*/
public function execute(OptionCollection $collection): array
{
$res = [];
$additional['value'] = 'option_id';
$additional['label'] = 'value';
$additional['sort_order'] = 'sort_order';

foreach ($collection as $item) {
$data = [];

foreach ($additional as $code => $field) {
$value = $item->getData($field);

if ('sort_order' === $field) {
$value = (int)$value;
}

if ('option_id' === $field) {
$value = (string)$value;
}

$data[$code] = $value;
}

if ($data) {
$res[] = $data;
}
}

return $res;
}
}
48 changes: 48 additions & 0 deletions src/module-vsbridge-indexer-catalog/Model/Attribute/SortValues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types = 1);

/**
* @package Divante\VsbridgeIndexerCatalog
* @author Agata Firlejczyk <afirlejczyk@divante.pl>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCatalog\Model\Attribute;

/**
* Class SortValues
*/
class SortValues
{
/**
* @param array $options
*
* @return array
*/
public function execute(array $options)
{
usort($options, [$this, 'sortOptions']);

return $options;
}

/**
* @param array $a
* @param array $b
*
* @return int
*/
public function sortOptions($a, $b)
{
$aSizePos = $a['sort_order'] ?? 0;
$bSizePos = $b['sort_order'] ?? 0;

if ($aSizePos === $bSizePos) {
return 0;
}

return ($aSizePos > $bSizePos) ? 1 : -1;
}
}
Loading