Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Unbxd\MSISupport\Model\Product\DataSourceProvider;
- use Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory as SwatchCollectionFactory;
- use Magento\Eav\Api\AttributeRepositoryInterface;
- // … your other use statements …
- class MSICustomDataProvider implements DataSourceProviderInterface
- {
- // … your existing properties …
- /**
- * @var SwatchCollectionFactory
- */
- private $swatchCollectionFactory;
- /**
- * @var AttributeRepositoryInterface
- */
- private $attributeRepository;
- public function __construct(
- // … your existing constructor args …,
- SwatchCollectionFactory $swatchCollectionFactory,
- AttributeRepositoryInterface $attributeRepository
- ) {
- // … your existing assignments …
- $this->swatchCollectionFactory = $swatchCollectionFactory;
- $this->attributeRepository = $attributeRepository;
- }
- public function appendData($storeId, array $indexData)
- {
- // … your existing MSI + currency logic …
- foreach (array_keys($indexData) as $productId) {
- try {
- // … MSI stock + currency …
- // ----- NEW: pull color hex from swatch table -----
- // 1) load the attribute metadata
- $colorAttr = $this->attributeRepository
- ->get('catalog_product', 'color');
- if ($colorAttr->getFrontendInput() === 'select') {
- // 2) grab the product’s raw option_id
- $optionId = $this->productRepository
- ->getById($productId, false, $storeId)
- ->getData('color');
- if ($optionId) {
- // 3) load swatch row for our attribute_id + option_id
- $swatch = $this->swatchCollectionFactory->create()
- ->addFieldToFilter('attribute_id', $colorAttr->getAttributeId())
- ->addFieldToFilter('option_id', $optionId)
- ->getFirstItem();
- $hex = $swatch->getData('value'); // e.g. “#FF0000”
- if ($hex) {
- $indexData[$productId]['color_hex'] = $hex;
- $textAttributeTypes[] = 'color_hex';
- }
- }
- }
- // ----------------------------------------------------
- } catch (\Exception $e) {
- $this->logger->error("Error fetching color hex for {$productId}: {$e->getMessage()}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement