1

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?

1 Answer 1

4

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.