Advertisement
KunalkaushikV

Untitled

May 20th, 2025
197
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.03 KB | None | 0 0
  1. // send me the MSI.php code
Advertisement
Comments
  • krishna1713
    3 days
    # text 7.42 KB | 0 0
    1. <?php
    2.  
    3. namespace Unbxd\MSISupport\Model\Product\DataSourceProvider;
    4.  
    5. /**
    6. * @author Jag S <[email protected]>
    7. */
    8.  
    9. use Unbxd\ProductFeed\Model\Indexer\Product\Full\DataSourceProviderInterface;
    10. use Unbxd\ProductFeed\Logger\LoggerInterface;
    11. use Magento\Framework\DB\Adapter\AdapterInterface;
    12. use Magento\Catalog\Api\ProductRepositoryInterface;
    13. use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
    14. use Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku;
    15. use Unbxd\ProductFeed\Helper\Data as HelperData;
    16. use Unbxd\ProductFeed\Helper\AttributeHelper;
    17. use Magento\Store\Model\StoreManagerInterface;
    18. use Magento\InventorySales\Model\StockByWebsiteIdResolver;
    19. use Exception;
    20.  
    21. class MSICustomDataProvider implements DataSourceProviderInterface
    22. {
    23. /**
    24. * Related data source code
    25. */
    26. const DATA_SOURCE_CODE = 'msi_productfeed_extension';
    27.  
    28.  
    29. /** Define the schema for the attribute */
    30.  
    31. /**
    32. * @var LoggerInterface
    33. */
    34. private $logger;
    35.  
    36. /**
    37. * @var HelperData
    38. */
    39. private $helperData;
    40.  
    41. protected $productRepository;
    42.  
    43. private $getSalableQuantityDataBySku;
    44.  
    45. private $getSourceItemsDataBySku;
    46.  
    47. /**
    48. * @var AttributeHelper
    49. */
    50. protected $attributeHelper;
    51.  
    52. private $storeManager;
    53.  
    54. private $stockByWebsiteId;
    55.  
    56.  
    57. /**
    58. * Constructor.
    59. */
    60. public function __construct(
    61. ProductRepositoryInterface $productRepository,
    62. LoggerInterface $logger,
    63. HelperData $helperData,
    64. GetSalableQuantityDataBySku $getSalableQuantityDataBySku,
    65. GetSourceItemsDataBySku $getSourceItemsDataBySku,
    66. AttributeHelper $attributeHelper,
    67. StoreManagerInterface $storeManager,
    68. StockByWebsiteIdResolver $stockByWebsiteId
    69. ) {
    70. $this->productRepository = $productRepository;
    71. $this->logger = $logger->create("feed");
    72. $this->helperData = $helperData;
    73. $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
    74. $this->getSourceItemsDataBySku = $getSourceItemsDataBySku;
    75. $this->attributeHelper = $attributeHelper;
    76. $this->storeManager = $storeManager;
    77. $this->stockByWebsiteId = $stockByWebsiteId;
    78. }
    79.  
    80. /**
    81. * {@inheritdoc}
    82. */
    83. public function getDataSourceCode()
    84. {
    85. return self::DATA_SOURCE_CODE;
    86. }
    87.  
    88. /**
    89. * Add custom code here
    90. *
    91. * {@inheritdoc}
    92. */
    93. public function appendData($storeId, array $indexData)
    94. {
    95. $stores = $this->attributeHelper->getMultiStoreEnabledStores();
    96. $stockStoreMap = [];
    97. if (empty($stores)) {
    98. $stores[] = $storeId;
    99. }
    100. foreach ($stores as $multiStoreId) {
    101. $websiteId = $this->storeManager->getStore($multiStoreId)->getWebsiteId();
    102. $stock = $this->stockByWebsiteId->execute($websiteId);
    103. if (array_key_exists($stock->getStockId(), $stockStoreMap)) {
    104. $stockStoreMap[$stock->getStockId()][] = $multiStoreId;
    105. } else {
    106. $stockStoreMap[$stock->getStockId()] = [$multiStoreId];
    107. }
    108. }
    109.  
    110. $numberAttributeTypes = [];
    111. $boolAttributeTypes = [];
    112. /* product ID is the entity id in magento , add your custom logic for your custom attribute */
    113. foreach (array_keys($indexData) as $productId) {
    114. try {
    115. if ($productId != "fields") {
    116. /**
    117. * Replace the logic within this if condition to that of yours
    118. */
    119. $product = $indexData[$productId];
    120. $sku = $product["sku"]; //pass your product sku
    121. if ($sku) {
    122. $inStock = false;
    123. $sourceData = $this->getSourceItemsDataBySku->execute($sku);
    124. foreach ($sourceData as $sourceStock) {
    125. $attributeName = str_replace(' ', '-', strtolower($sourceStock["source_code"])) . "-sourceQty";
    126. if (!in_array($attributeName, $numberAttributeTypes)) {
    127. $numberAttributeTypes[] = $attributeName;
    128. }
    129. $indexData[$productId][$attributeName] = $sourceStock["quantity"];
    130. if (!$inStock) {
    131. $inStock = ($sourceStock["quantity"] > 0);
    132. }
    133. }
    134. $salable = $this->getSalableQuantityDataBySku->execute($sku);
    135. $inStock = false;
    136.  
    137. foreach ($salable as $warehouseStock) {
    138. $attributeName = str_replace(' ', '-', strtolower($warehouseStock["stock_name"])) . "-salableQty";
    139. if (!in_array($attributeName, $numberAttributeTypes)) {
    140. $numberAttributeTypes[] = $attributeName;
    141. }
    142. $indexData[$productId][$attributeName] = $warehouseStock["qty"];
    143. if (!$inStock) {
    144. $inStock = ($warehouseStock["qty"] > 0);
    145. }
    146. $stockId = $warehouseStock['stock_id'];
    147. if (array_key_exists($stockId, $stockStoreMap)) {
    148. $storeIdList = $stockStoreMap[$stockId];
    149. foreach ($storeIdList as $storeid) {
    150. $attributeKey = 'instock' . $storeid;
    151. if (!in_array($attributeKey, $boolAttributeTypes)) {
    152. $boolAttributeTypes[] = $attributeKey;
    153. }
    154. $indexData[$productId][$attributeKey] = $inStock;
    155. }
    156. }
    157. }
    158. }
    159. }
    160. } catch (\Exception $e) {
    161. $this->logger->error('Error while saleable quantitys product -' . $productId . $e->__toString());
    162. }
    163. }
    164. foreach ($numberAttributeTypes as $numberAttribute) {
    165. $this->addIndexedFields($indexData, $numberAttribute, "number");
    166. }
    167. foreach ($boolAttributeTypes as $boolAttribute) {
    168. $this->addIndexedFields($indexData, $boolAttribute, "bool");
    169. }
    170. /**
    171. * Replace the second argument with that of the attribute name which was included in line 75
    172. * addIndexedFields accepts a third optional argument which should one of the following values ('text','longText','decimal','number','link','date','bool','sku','path')
    173. * */
    174. return $indexData;
    175. }
    176.  
    177. /**
    178. * @param $indexData
    179. * @return
    180. */
    181. private function addIndexedFields(array &$indexData, $attrName, $fieldType = "number")
    182. {
    183. $alreadyExistFields = array_key_exists('fields', $indexData) ? $indexData['fields'] : [];
    184. $indexData['fields'] = array_merge($alreadyExistFields, [$attrName => [
    185. 'fieldName' => $attrName,
    186. 'dataType' => $fieldType,
    187. 'multiValued' => false, /*set it to true if the attribute will carry more than one value for the same product */
    188. 'autoSuggest' => false
    189. ]]);
    190. }
    191. }
    192.  
    193.  
Add Comment
Please, Sign In to add comment
Advertisement