Question

How to get source names website wise from multisource inventory?

I have below code for getting all names from a collection of inventory sources:

    use Magento\Inventory\Model\ResourceModel\Source\Collection as InventoryCollection;

    public function __construct( 
        Context $context, 
        InventoryCollection $inventoryCollection
    ){
        parent::__construct($context); 
        $this->_inventoryCollection = $inventoryCollection;
    }

    public function getInventorySources()
    {       
        $sourceListArr = $this->_inventoryCollection->load();
        $sourceList=[];
        $sourceAllList=[];

        $sourceList = array();
        foreach ($sourceListArr as $sourceItemName) {
            $sourceList['sourceName'] = $sourceItemName->getName();
            $sourceAllList[] = $sourceList;
        }
        return $sourceAllList;
    }

It will return all the sources, Here I want to get website wise source names,

How can I achieve this functionality?

Was it helpful?

Solution

I did below as a solution:

use Magento\Inventory\Model\ResourceModel\Source\Collection as InventoryCollection;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\InventoryApi\Api\GetStockSourceLinksInterface;
use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Store\Model\StoreManagerInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;

 public function __construct( 
        Context $context, 
        InventoryCollection $inventoryCollection,
        InventoryCollection $inventoryCollection,
        SortOrderBuilder $sortOrderBuilder,
        GetStockSourceLinksInterface $getStockSourceLinks,
        ResourceConnection $resourceConnection,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        StoreManagerInterface $_storeManager,
        SourceRepositoryInterface $sourceRepository
    ){
    parent::__construct($context); 
    $this->_inventoryCollection = $inventoryCollection;
    $this->_inventoryCollection = $inventoryCollection;
    $this->sortOrderBuilder = $sortOrderBuilder;
    $this->getStockSourceLinks = $getStockSourceLinks;
    $this->resourceConnection = $resourceConnection;
    $this->_searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->_storeManager = $_storeManager;
    $this->_sourceRepository = $sourceRepository;
    }

public function getInventorySources()
{   
    $websiteCode = $this->_storeManager->getStore()->getWebsite()->getCode();
    $connection = $this->resourceConnection->getConnection();
    $tableName = $this->resourceConnection->getTableName('inventory_stock_sales_channel');
    $select = $connection->select()
        ->from($tableName)
        ->where('code = ?', $websiteCode);

    $StockCollection = $connection->fetchAll($select);
    $stockId = $StockCollection[0]['stock_id'];

    $sortOrder = $this->sortOrderBuilder
        ->setField(StockSourceLinkInterface::PRIORITY)
        ->setAscendingDirection()
        ->create();
    $searchCriteria = $this->_searchCriteriaBuilder
        ->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId)
        ->addSortOrder($sortOrder)
        ->create();

    $searchResult = $this->getStockSourceLinks->execute($searchCriteria);

    if ($searchResult->getTotalCount() === 0) {
        return [];
    }

    $assignedSourcesData = [];
    foreach ($searchResult->getItems() as $link) {
        $source = $this->_sourceRepository->get($link->getSourceCode());
        $assignedSourcesData[] = [
            'sourceName' => $source->getName(),
        ];
    }       
    return $assignedSourcesData;        
}

Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top