2

I have created a custom module and on my custom page, I want the collection of all the sources available on my website. Let's assume I have created 3 sources from backend like "US", "UK", "INDIA". Now, I want the collection of all resources available on my website

How could I achieve this functionality?

Thanks in advance!

1 Answer 1

5

Try following way:



use Magento\Framework\Api\SearchCriteriaBuilderFactory;
use Magento\InventoryApi\Api\SourceRepositoryInterface;

/**
 * @var SourceRepositoryInterface
 */
private $sourceRepository;

/**
 * @var SearchCriteriaBuilderFactory
 */
private $searchCriteriaBuilderFactory;

/**
 * Place constructor.
 *
 * @param SourceRepositoryInterface $sourceRepository
 * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
 */
public function __construct(
    SourceRepositoryInterface $sourceRepository,
    SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
) {
    $this->sourceRepository = $sourceRepository;
    $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
}

/**
 * {@inheritdoc}
 * @codeCoverageIgnore
 */
public function getAllOptions()
{
    if (!$this->_options) {
        /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
        $searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
        $searchCriteria = $searchCriteriaBuilder->create();
        $sources = $this->sourceRepository->getList($searchCriteria)->getItems();
        foreach ($sources as $source) {
            $this->_options[] = [
                'value' => $source->getCountryId(),
                'label' => $source->getCountryId()
            ];
        }
    }
    return $this->_options;
}
1
  • Thanks @Sohel Rana Commented Feb 18, 2019 at 12:09

Your Answer

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

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