Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Unbxd\MSISupport\Model\Product\DataSourceProvider;
- use Unbxd\ProductFeed\Model\Indexer\Product\Full\DataSourceProviderInterface;
- use Unbxd\ProductFeed\Logger\LoggerInterface;
- use Magento\Framework\DB\Adapter\AdapterInterface;
- use Magento\Catalog\Api\ProductRepositoryInterface;
- use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
- use Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku;
- use Unbxd\ProductFeed\Helper\Data as HelperData;
- use Unbxd\ProductFeed\Helper\AttributeHelper;
- use Magento\Store\Model\StoreManagerInterface;
- use Magento\InventorySales\Model\StockByWebsiteIdResolver;
- use Exception;
- class MSICustomDataProvider implements DataSourceProviderInterface
- {
- const DATA_SOURCE_CODE = 'msi_productfeed_extension';
- private $logger;
- private $helperData;
- protected $productRepository;
- private $getSalableQuantityDataBySku;
- private $getSourceItemsDataBySku;
- protected $attributeHelper;
- private $storeManager;
- private $stockByWebsiteId;
- public function __construct(
- ProductRepositoryInterface $productRepository,
- LoggerInterface $logger,
- HelperData $helperData,
- GetSalableQuantityDataBySku $getSalableQuantityDataBySku,
- GetSourceItemsDataBySku $getSourceItemsDataBySku,
- AttributeHelper $attributeHelper,
- StoreManagerInterface $storeManager,
- StockByWebsiteIdResolver $stockByWebsiteId
- ) {
- $this->productRepository = $productRepository;
- $this->logger = $logger->create("feed");
- $this->helperData = $helperData;
- $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
- $this->getSourceItemsDataBySku = $getSourceItemsDataBySku;
- $this->attributeHelper = $attributeHelper;
- $this->storeManager = $storeManager;
- $this->stockByWebsiteId = $stockByWebsiteId;
- }
- public function getDataSourceCode()
- {
- return self::DATA_SOURCE_CODE;
- }
- public function appendData($storeId, array $indexData)
- {
- $stores = $this->attributeHelper->getMultiStoreEnabledStores() ?: [ $storeId ];
- $stockStoreMap = [];
- foreach ($stores as $multiStoreId) {
- $websiteId = $this->storeManager->getStore($multiStoreId)->getWebsiteId();
- $stock = $this->stockByWebsiteId->execute($websiteId);
- $stockStoreMap[$stock->getStockId()][] = $multiStoreId;
- }
- $numberAttributeTypes = [];
- $boolAttributeTypes = [];
- $textAttributeTypes = []; // <-- new bucket for our text fields
- foreach (array_keys($indexData) as $productId) {
- try {
- if ($productId === 'fields') {
- continue;
- }
- // ——— existing MSI logic ———
- $productArray = $indexData[$productId];
- $sku = $productArray['sku'] ?? null;
- if ($sku) {
- // 1) source-level quantities
- $sourceData = $this->getSourceItemsDataBySku->execute($sku);
- foreach ($sourceData as $sourceStock) {
- $attr = str_replace(' ', '-', strtolower($sourceStock["source_code"])) . "-sourceQty";
- $numberAttributeTypes[] = $attr;
- $indexData[$productId][$attr] = $sourceStock["quantity"];
- }
- // 2) salable quantities + inStock flags per store
- $salable = $this->getSalableQuantityDataBySku->execute($sku);
- foreach ($salable as $warehouseStock) {
- $wAttr = str_replace(' ', '-', strtolower($warehouseStock["stock_name"])) . "-salableQty";
- $numberAttributeTypes[] = $wAttr;
- $indexData[$productId][$wAttr] = $warehouseStock["qty"];
- $inStock = ($warehouseStock["qty"] > 0);
- $stockId = $warehouseStock['stock_id'];
- foreach ($stockStoreMap[$stockId] as $sId) {
- $key = 'instock' . $sId;
- $boolAttributeTypes[] = $key;
- $indexData[$productId][$key] = $inStock;
- }
- }
- }
- // ——— NEW: fetch Magento product for currency + color_hash ———
- $magentoProduct = $this->productRepository->getById($productId, false, $storeId);
- // A) Currency per store
- $currencyCode = $this->storeManager
- ->getStore($storeId)
- ->getCurrentCurrency()
- ->getCurrencyCode();
- $indexData[$productId]['currency'] = $currencyCode;
- $textAttributeTypes[] = 'currency';
- // B) color_hash attribute
- $colorAttr = $magentoProduct->getCustomAttribute('color_hash');
- if ($colorAttr) {
- $indexData[$productId]['color_hash'] = $colorAttr->getValue();
- $textAttributeTypes[] = 'color_hash';
- }
- } catch (Exception $e) {
- $this->logger->error("MSI Custom Provider error [{$productId}]: {$e->getMessage()}");
- }
- }
- // register all our fields in the schema
- foreach (array_unique($numberAttributeTypes) as $n) {
- $this->addIndexedFields($indexData, $n, "number");
- }
- foreach (array_unique($boolAttributeTypes) as $b) {
- $this->addIndexedFields($indexData, $b, "bool");
- }
- foreach (array_unique($textAttributeTypes) as $t) {
- $this->addIndexedFields($indexData, $t, "text");
- }
- return $indexData;
- }
- private function addIndexedFields(array &$indexData, $attrName, $fieldType = "text")
- {
- $existing = $indexData['fields'] ?? [];
- $existing[$attrName] = [
- 'fieldName' => $attrName,
- 'dataType' => $fieldType,
- 'multiValued' => false,
- 'autoSuggest' => false
- ];
- $indexData['fields'] = $existing;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement