Advertisement
KunalkaushikV

full new code

May 20th, 2025
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.42 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Unbxd\MSISupport\Model\Product\DataSourceProvider;
  4.  
  5. use Unbxd\ProductFeed\Model\Indexer\Product\Full\DataSourceProviderInterface;
  6. use Unbxd\ProductFeed\Logger\LoggerInterface;
  7. use Magento\Framework\DB\Adapter\AdapterInterface;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
  10. use Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku;
  11. use Unbxd\ProductFeed\Helper\Data as HelperData;
  12. use Unbxd\ProductFeed\Helper\AttributeHelper;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Magento\InventorySales\Model\StockByWebsiteIdResolver;
  15. use Exception;
  16.  
  17. class MSICustomDataProvider implements DataSourceProviderInterface
  18. {
  19.     const DATA_SOURCE_CODE = 'msi_productfeed_extension';
  20.  
  21.     private $logger;
  22.     private $helperData;
  23.     protected $productRepository;
  24.     private $getSalableQuantityDataBySku;
  25.     private $getSourceItemsDataBySku;
  26.     protected $attributeHelper;
  27.     private $storeManager;
  28.     private $stockByWebsiteId;
  29.  
  30.     public function __construct(
  31.         ProductRepositoryInterface     $productRepository,
  32.         LoggerInterface               $logger,
  33.         HelperData                    $helperData,
  34.         GetSalableQuantityDataBySku   $getSalableQuantityDataBySku,
  35.         GetSourceItemsDataBySku       $getSourceItemsDataBySku,
  36.         AttributeHelper               $attributeHelper,
  37.         StoreManagerInterface         $storeManager,
  38.         StockByWebsiteIdResolver      $stockByWebsiteId
  39.     ) {
  40.         $this->productRepository           = $productRepository;
  41.         $this->logger                      = $logger->create("feed");
  42.         $this->helperData                  = $helperData;
  43.         $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
  44.         $this->getSourceItemsDataBySku     = $getSourceItemsDataBySku;
  45.         $this->attributeHelper             = $attributeHelper;
  46.         $this->storeManager                = $storeManager;
  47.         $this->stockByWebsiteId            = $stockByWebsiteId;
  48.     }
  49.  
  50.     public function getDataSourceCode()
  51.     {
  52.         return self::DATA_SOURCE_CODE;
  53.     }
  54.  
  55.     public function appendData($storeId, array $indexData)
  56.     {
  57.         $stores        = $this->attributeHelper->getMultiStoreEnabledStores() ?: [ $storeId ];
  58.         $stockStoreMap = [];
  59.         foreach ($stores as $multiStoreId) {
  60.             $websiteId = $this->storeManager->getStore($multiStoreId)->getWebsiteId();
  61.             $stock     = $this->stockByWebsiteId->execute($websiteId);
  62.             $stockStoreMap[$stock->getStockId()][] = $multiStoreId;
  63.         }
  64.  
  65.         $numberAttributeTypes = [];
  66.         $boolAttributeTypes   = [];
  67.         $textAttributeTypes   = [];  // <-- new bucket for our text fields
  68.  
  69.         foreach (array_keys($indexData) as $productId) {
  70.             try {
  71.                 if ($productId === 'fields') {
  72.                     continue;
  73.                 }
  74.  
  75.                 // ——— existing MSI logic ———
  76.                 $productArray = $indexData[$productId];
  77.                 $sku          = $productArray['sku'] ?? null;
  78.  
  79.                 if ($sku) {
  80.                     // 1) source-level quantities
  81.                     $sourceData = $this->getSourceItemsDataBySku->execute($sku);
  82.                     foreach ($sourceData as $sourceStock) {
  83.                         $attr = str_replace(' ', '-', strtolower($sourceStock["source_code"])) . "-sourceQty";
  84.                         $numberAttributeTypes[] = $attr;
  85.                         $indexData[$productId][$attr] = $sourceStock["quantity"];
  86.                     }
  87.  
  88.                     // 2) salable quantities + inStock flags per store
  89.                     $salable = $this->getSalableQuantityDataBySku->execute($sku);
  90.                     foreach ($salable as $warehouseStock) {
  91.                         $wAttr = str_replace(' ', '-', strtolower($warehouseStock["stock_name"])) . "-salableQty";
  92.                         $numberAttributeTypes[] = $wAttr;
  93.                         $indexData[$productId][$wAttr] = $warehouseStock["qty"];
  94.  
  95.                         $inStock = ($warehouseStock["qty"] > 0);
  96.                         $stockId = $warehouseStock['stock_id'];
  97.                         foreach ($stockStoreMap[$stockId] as $sId) {
  98.                             $key = 'instock' . $sId;
  99.                             $boolAttributeTypes[] = $key;
  100.                             $indexData[$productId][$key] = $inStock;
  101.                         }
  102.                     }
  103.                 }
  104.  
  105.                 // ——— NEW: fetch Magento product for currency + color_hash ———
  106.                 $magentoProduct = $this->productRepository->getById($productId, false, $storeId);
  107.  
  108.                 // A) Currency per store
  109.                 $currencyCode = $this->storeManager
  110.                     ->getStore($storeId)
  111.                     ->getCurrentCurrency()
  112.                     ->getCurrencyCode();
  113.                 $indexData[$productId]['currency'] = $currencyCode;
  114.                 $textAttributeTypes[] = 'currency';
  115.  
  116.                 // B) color_hash attribute
  117.                 $colorAttr = $magentoProduct->getCustomAttribute('color_hash');
  118.                 if ($colorAttr) {
  119.                     $indexData[$productId]['color_hash'] = $colorAttr->getValue();
  120.                     $textAttributeTypes[] = 'color_hash';
  121.                 }
  122.  
  123.             } catch (Exception $e) {
  124.                 $this->logger->error("MSI Custom Provider error [{$productId}]: {$e->getMessage()}");
  125.             }
  126.         }
  127.  
  128.         // register all our fields in the schema
  129.         foreach (array_unique($numberAttributeTypes) as $n) {
  130.             $this->addIndexedFields($indexData, $n, "number");
  131.         }
  132.         foreach (array_unique($boolAttributeTypes) as $b) {
  133.             $this->addIndexedFields($indexData, $b, "bool");
  134.         }
  135.         foreach (array_unique($textAttributeTypes) as $t) {
  136.             $this->addIndexedFields($indexData, $t, "text");
  137.         }
  138.  
  139.         return $indexData;
  140.     }
  141.  
  142.     private function addIndexedFields(array &$indexData, $attrName, $fieldType = "text")
  143.     {
  144.         $existing = $indexData['fields'] ?? [];
  145.         $existing[$attrName] = [
  146.             'fieldName'   => $attrName,
  147.             'dataType'    => $fieldType,
  148.             'multiValued' => false,
  149.             'autoSuggest' => false
  150.         ];
  151.         $indexData['fields'] = $existing;
  152.     }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement