Advertisement
KunalkaushikV

Untitled

May 22nd, 2025
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | None | 0 0
  1. <?php
  2. namespace Unbxd\MSISupport\Model\Product\DataSourceProvider;
  3.  
  4. use Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory as SwatchCollectionFactory;
  5. use Magento\Eav\Api\AttributeRepositoryInterface;
  6. // … your other use statements …
  7.  
  8. class MSICustomDataProvider implements DataSourceProviderInterface
  9. {
  10.     // … your existing properties …
  11.     /**
  12.      * @var SwatchCollectionFactory
  13.      */
  14.     private $swatchCollectionFactory;
  15.     /**
  16.      * @var AttributeRepositoryInterface
  17.      */
  18.     private $attributeRepository;
  19.  
  20.     public function __construct(
  21.         // … your existing constructor args …,
  22.         SwatchCollectionFactory     $swatchCollectionFactory,
  23.         AttributeRepositoryInterface $attributeRepository
  24.     ) {
  25.         // … your existing assignments …
  26.         $this->swatchCollectionFactory = $swatchCollectionFactory;
  27.         $this->attributeRepository     = $attributeRepository;
  28.     }
  29.  
  30.     public function appendData($storeId, array $indexData)
  31.     {
  32.         // … your existing MSI + currency logic …
  33.  
  34.         foreach (array_keys($indexData) as $productId) {
  35.             try {
  36.                 // … MSI stock + currency …
  37.  
  38.                 // ----- NEW: pull color hex from swatch table -----
  39.                 // 1) load the attribute metadata
  40.                 $colorAttr = $this->attributeRepository
  41.                     ->get('catalog_product', 'color');
  42.  
  43.                 if ($colorAttr->getFrontendInput() === 'select') {
  44.                     // 2) grab the product’s raw option_id
  45.                     $optionId = $this->productRepository
  46.                         ->getById($productId, false, $storeId)
  47.                         ->getData('color');
  48.  
  49.                     if ($optionId) {
  50.                         // 3) load swatch row for our attribute_id + option_id
  51.                         $swatch = $this->swatchCollectionFactory->create()
  52.                             ->addFieldToFilter('attribute_id', $colorAttr->getAttributeId())
  53.                             ->addFieldToFilter('option_id',    $optionId)
  54.                             ->getFirstItem();
  55.  
  56.                         $hex = $swatch->getData('value'); // e.g. “#FF0000”
  57.                         if ($hex) {
  58.                             $indexData[$productId]['color_hex'] = $hex;
  59.                             $textAttributeTypes[] = 'color_hex';
  60.                         }
  61.                     }
  62.                 }
  63.                 // ----------------------------------------------------
  64.  
  65.             } catch (\Exception $e) {
  66.                 $this->logger->error("Error fetching color hex for {$productId}: {$e->getMessage()}");
  67.             }
  68.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement