Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // send me the MSI.php code
Advertisement
Comments
-
- <?php
- namespace Unbxd\MSISupport\Model\Product\DataSourceProvider;
- /**
- * @author Jag S <[email protected]>
- */
- 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
- {
- /**
- * Related data source code
- */
- const DATA_SOURCE_CODE = 'msi_productfeed_extension';
- /** Define the schema for the attribute */
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @var HelperData
- */
- private $helperData;
- protected $productRepository;
- private $getSalableQuantityDataBySku;
- private $getSourceItemsDataBySku;
- /**
- * @var AttributeHelper
- */
- protected $attributeHelper;
- private $storeManager;
- private $stockByWebsiteId;
- /**
- * Constructor.
- */
- 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;
- }
- /**
- * {@inheritdoc}
- */
- public function getDataSourceCode()
- {
- return self::DATA_SOURCE_CODE;
- }
- /**
- * Add custom code here
- *
- * {@inheritdoc}
- */
- public function appendData($storeId, array $indexData)
- {
- $stores = $this->attributeHelper->getMultiStoreEnabledStores();
- $stockStoreMap = [];
- if (empty($stores)) {
- $stores[] = $storeId;
- }
- foreach ($stores as $multiStoreId) {
- $websiteId = $this->storeManager->getStore($multiStoreId)->getWebsiteId();
- $stock = $this->stockByWebsiteId->execute($websiteId);
- if (array_key_exists($stock->getStockId(), $stockStoreMap)) {
- $stockStoreMap[$stock->getStockId()][] = $multiStoreId;
- } else {
- $stockStoreMap[$stock->getStockId()] = [$multiStoreId];
- }
- }
- $numberAttributeTypes = [];
- $boolAttributeTypes = [];
- /* product ID is the entity id in magento , add your custom logic for your custom attribute */
- foreach (array_keys($indexData) as $productId) {
- try {
- if ($productId != "fields") {
- /**
- * Replace the logic within this if condition to that of yours
- */
- $product = $indexData[$productId];
- $sku = $product["sku"]; //pass your product sku
- if ($sku) {
- $inStock = false;
- $sourceData = $this->getSourceItemsDataBySku->execute($sku);
- foreach ($sourceData as $sourceStock) {
- $attributeName = str_replace(' ', '-', strtolower($sourceStock["source_code"])) . "-sourceQty";
- if (!in_array($attributeName, $numberAttributeTypes)) {
- $numberAttributeTypes[] = $attributeName;
- }
- $indexData[$productId][$attributeName] = $sourceStock["quantity"];
- if (!$inStock) {
- $inStock = ($sourceStock["quantity"] > 0);
- }
- }
- $salable = $this->getSalableQuantityDataBySku->execute($sku);
- $inStock = false;
- foreach ($salable as $warehouseStock) {
- $attributeName = str_replace(' ', '-', strtolower($warehouseStock["stock_name"])) . "-salableQty";
- if (!in_array($attributeName, $numberAttributeTypes)) {
- $numberAttributeTypes[] = $attributeName;
- }
- $indexData[$productId][$attributeName] = $warehouseStock["qty"];
- if (!$inStock) {
- $inStock = ($warehouseStock["qty"] > 0);
- }
- $stockId = $warehouseStock['stock_id'];
- if (array_key_exists($stockId, $stockStoreMap)) {
- $storeIdList = $stockStoreMap[$stockId];
- foreach ($storeIdList as $storeid) {
- $attributeKey = 'instock' . $storeid;
- if (!in_array($attributeKey, $boolAttributeTypes)) {
- $boolAttributeTypes[] = $attributeKey;
- }
- $indexData[$productId][$attributeKey] = $inStock;
- }
- }
- }
- }
- }
- } catch (\Exception $e) {
- $this->logger->error('Error while saleable quantitys product -' . $productId . $e->__toString());
- }
- }
- foreach ($numberAttributeTypes as $numberAttribute) {
- $this->addIndexedFields($indexData, $numberAttribute, "number");
- }
- foreach ($boolAttributeTypes as $boolAttribute) {
- $this->addIndexedFields($indexData, $boolAttribute, "bool");
- }
- /**
- * Replace the second argument with that of the attribute name which was included in line 75
- * addIndexedFields accepts a third optional argument which should one of the following values ('text','longText','decimal','number','link','date','bool','sku','path')
- * */
- return $indexData;
- }
- /**
- * @param $indexData
- * @return
- */
- private function addIndexedFields(array &$indexData, $attrName, $fieldType = "number")
- {
- $alreadyExistFields = array_key_exists('fields', $indexData) ? $indexData['fields'] : [];
- $indexData['fields'] = array_merge($alreadyExistFields, [$attrName => [
- 'fieldName' => $attrName,
- 'dataType' => $fieldType,
- 'multiValued' => false, /*set it to true if the attribute will carry more than one value for the same product */
- 'autoSuggest' => false
- ]]);
- }
- }
Add Comment
Please, Sign In to add comment
Advertisement