:::: MENU ::::

Magento Tutorial | Magento Blog | Learn Magento 2

Cookies Consent Popup

Showing posts with label magento2. Show all posts
Showing posts with label magento2. Show all posts

            Try below code: 

           $obj = \Magento\Framework\App\ObjectManager::getInstance();    

            /** @var \Magento\Catalog\Model\Product $product */

            $productObject = $obj->get('Magento\Catalog\Model\Product');    

            $product = $productObject->loadByAttribute('sku', 'Test Test');    

            $linkDataAll = [];

            $skuLinks = "0012365,test1233,789456";

            $skuLinks = explode(",",$skuLinks);    

            foreach($skuLinks as $skuLink) {

                //check first that the product exist

                $linkedProduct = $productObject->loadByAttribute("sku",$skuLink);

                if($linkedProduct) {

                    /** @var  \Magento\Catalog\Api\Data\ProductLinkInterface $productLinks */

                    $productLinks = $obj->create('Magento\Catalog\Api\Data\ProductLinkInterface');

                    $linkData = $productLinks //Magento\Catalog\Api\Data\ProductLinkInterface

                        ->setSku($product->getSku())

                        ->setLinkedProductSku($skuLink)

                        ->setLinkType("related");

                    $linkDataAll[] = $linkData;

                }

            }

            if($linkDataAll) {

                print(count($linkDataAll)); //gives 3

                $product->setProductLinks($linkDataAll);

            }

            $product->save();

dont use **objectmanager** this code just for reference


You can use \Magento\InventoryApi\Api\SourceItemRepositoryInterface class with \Magento\Framework\Api\SearchCriteriaBuilder to get source item data by source code and product SKU.

Here are the sample model class


    <?php

    namespace MageExpert\Testing\Model;

    class SourceItemModel

    {

       

        private $searchCriteriaBuilder;

        private $sourceItemRepository;

        public function __construct(

            ...

            \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,

            \Magento\InventoryApi\Api\SourceItemRepositoryInterface $sourceItemRepository

            ...

        ) {

            $this->searchCriteriaBuilder = $searchCriteriaBuilder;

            $this->sourceRepository = $sourceRepository;

        }

        public function getSourcesItems($souceCode, $sku)

        {

             $searchCriteria = $this->searchCriteriaBuilder

                ->addFilter('source_code', $souceCode)

                ->addFilter('sku', $sku)

                ->create();

            $sourceItemData = $this->sourceItemRepository->getList($searchCriteria);

            return $sourceItemData->getItems();

        }

    }


Now you can use getSourcesItems() function to get all source items by sources code


    $sourceCode = 'your_store_code';

    $sku = 'Product_1'

    $sourceItems = $this->getSourcesItems($sourceCode);

    foreach ($sourceItems as $sourceItem) {

        print_r($sourceItem->getData());

    }


OUTPUT:


    Array

    (

        [source_item_id] => 96

        [source_code] => your_store_code

        [sku] => Product_1

        [quantity] => 100.0000

        [status] => 1

    )




If error in indexing after upgrade then run below sql query in phpmyadmin


CREATE  SQL SECURITY INVOKER   VIEW `inventory_stock_1`   AS  SELECT  DISTINCT    legacy_stock_status.product_id,   legacy_stock_status.website_id,   legacy_stock_status.stock_id,    legacy_stock_status.qty quantity,   legacy_stock_status.stock_status is_salable,   product.sku    FROM cataloginventory_stock_status` `legacy_stock_status`  INNER JOIN `catalog_product_entity` product    ON legacy_stock_status.product_id = product.entity_id;


and run below commands

* Clear `var/generation`

* Clear `var/cache`

* Enable Magento modules: `php bin/magento module:enable --all`

* Compile DI `php bin/magento setup:di:compile`