1

I am trying to assign new sources to stock but it's not working. I checked the logs and there is no error found. Below is my code. Can anyone please check and suggest me where am I wrong?

 public function assignSourceToStock($stockId, $sourceCode)
    {
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('source_code', $sourceCode)
            ->create();

        $sources = $this->sourceRepository->getList($searchCriteria)->getItems();

        if (!empty($sources)) {
            foreach ($sources as $source) {
                $source->setStockId($stockId);
                $this->sourceRepository->save($source);
            }

            echo "Source '{$sourceCode}' assigned to stock with ID '{$stockId}' successfully.";
        } else {
            echo "Source '{$sourceCode}' not found.";
        }
    }

I am calling this method assignSourceToStock() with the store code and stock id.

Any help will be appreciated.

1 Answer 1

2

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

4
  • Thanks, @Tarun Bhalodia. I will try and get back.
    – MagentoDev
    Commented Jul 17, 2023 at 12:12
  • 1
    Hello @MagentoDev, in case you have any issues with the above code please feel free to comment here. Commented Jul 17, 2023 at 12:19
  • Great. I tried the code and it's working, I am able to assign the sources to stock. One small question which is related to this source stock assignment, currently, it's not setting the website for stock, can we do that also?
    – MagentoDev
    Commented Jul 17, 2023 at 12:31
  • 1
    Hello @MagentoDev, Thank you for your confirmation. You can set the website while saving the stock on stockRepository Commented Jul 18, 2023 at 9:11

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.