This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from DivanteLtd/feature/support-for-configurab…
…le-option-label Add support for configurable value label. #81
- Loading branch information
Showing
4 changed files
with
252 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/module-vsbridge-indexer-catalog/Model/Attribute/LoadOptionLabelById.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* @package Divante\VsbridgeIndexerCatalog | ||
* @author Agata Firlejczyk <[email protected]> | ||
* @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; | ||
|
||
/** | ||
* Class LoadOptionLabelById | ||
*/ | ||
class LoadOptionLabelById | ||
{ | ||
/** | ||
* @var AttributeDataProvider | ||
*/ | ||
private $attributeDataProvider; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $optionsByAttribute = []; | ||
|
||
/** | ||
* LoadLabelByOptionId constructor. | ||
* | ||
* @param AttributeDataProvider $attributeDataProvider | ||
*/ | ||
public function __construct(AttributeDataProvider $attributeDataProvider) | ||
{ | ||
$this->attributeDataProvider = $attributeDataProvider; | ||
} | ||
|
||
/** | ||
* @param string $attributeCode | ||
* @param int $optionId | ||
* @param int $storeId | ||
* | ||
* @return string | ||
*/ | ||
public function execute(string $attributeCode, int $optionId, int $storeId): string | ||
{ | ||
$attributeModel = $this->attributeDataProvider->getAttributeByCode($attributeCode); | ||
$attributeModel->setStoreId($storeId); | ||
$options = $this->loadOptions($attributeModel); | ||
|
||
foreach ($options as $option) { | ||
if ($optionId === (int)$option['value']) { | ||
return $option['label']; | ||
} | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
/** | ||
* @param Attribute $attribute | ||
* | ||
* @return mixed | ||
*/ | ||
private function loadOptions(Attribute $attribute) | ||
{ | ||
$key = $attribute->getId() . '_' . $attribute->getStoreId(); | ||
|
||
if (!isset($this->optionsByAttribute[$key])) { | ||
$this->optionsByAttribute[$key] = $attribute->getOptions(); | ||
} | ||
|
||
return $this->optionsByAttribute[$key]; | ||
} | ||
} |
Oops, something went wrong.