Can you please try the below code
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Validation\ValidationException;
use Magento\InventoryApi\Api\Data\StockSourceLinkInterfaceFactory;
use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Inventory\Model\SourceRepository;
use Magento\Framework\Api\DataObjectHelper;
use Magento\InventoryApi\Api\StockSourceLinksSaveInterface;
/**
* Class SourceToStock
*/
class SourceToStock
{
/**
* @var SearchCriteriaBuilder
*/
private SearchCriteriaBuilder $searchCriteriaBuilder;
/**
* @var SourceRepository
*/
private SourceRepository $sourceRepository;
/**
* @var StockSourceLinkInterfaceFactory
*/
private StockSourceLinkInterfaceFactory $stockSourceLinkFactory;
/**
* @var DataObjectHelper
*/
private DataObjectHelper $dataObjectHelper;
/**
* @var StockSourceLinksSaveInterface
*/
private StockSourceLinksSaveInterface $stockSourceLinksSave;
/**
* SourceToStock constructor.
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param SourceRepository $sourceRepository
* @param StockSourceLinkInterfaceFactory $stockSourceLinkFactory
* @param DataObjectHelper $dataObjectHelper
* @param StockSourceLinksSaveInterface $stockSourceLinksSave
*/
public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
SourceRepository $sourceRepository,
StockSourceLinkInterfaceFactory $stockSourceLinkFactory,
DataObjectHelper $dataObjectHelper,
StockSourceLinksSaveInterface $stockSourceLinksSave
)
{
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->sourceRepository = $sourceRepository;
$this->stockSourceLinkFactory = $stockSourceLinkFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->stockSourceLinksSave = $stockSourceLinksSave;
}
/**
* @param $stockId
* @param $sourceCode
*/
public function assignSourceToStock($stockId, $sourceCode)
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('source_code', $sourceCode)
->create();
$sources = $this->sourceRepository->getList($searchCriteria)->getItems();
if (!empty($sources)) {
$links = [];
foreach ($sources as $source) {
$link = $this->stockSourceLinkFactory->create();
$linkData = [
StockSourceLinkInterface::STOCK_ID => $stockId,
StockSourceLinkInterface::SOURCE_CODE => $source->getSourceCode(),
StockSourceLinkInterface::PRIORITY => 1 // Set the priority whatever you want
];
$this->dataObjectHelper->populateWithArray($link, $linkData, StockSourceLinkInterface::class);
$links[] = $link;
}
try {
$this->stockSourceLinksSave->execute($links);
} catch (CouldNotSaveException $e) {
// Log your error message here
} catch (ValidationException $e) {
// Log your error message here
}
echo "Source '{$sourceCode}' assigned to stock with ID '{$stockId}' successfully.";
} else {
echo "Source '{$sourceCode}' not found.";
}
}
}
If this answer works as your expected, please mark it as accepted. That indicates your issue is solved and makes it easier for others with the same issue to find a verified answer. And that would be great if you click the upvote button for the answer in case you feel it is useful.
Happy Coding