0

I just want to load the product if that is available on the current website else not on the custom page. here is my code which is not working product still loading if that not exists in the current store...

 public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager,\Magento\Catalog\Api\ProductRepositoryInterface $productRepository) {

        $this->storeManager = $storeManager;
        $this->productRepository = $productRepository;

    }
public function getProduct($product_id){
    try{
        return $this->productRepository->getById($product_id,false,$this->storeManager->getStore()->getId());
    }catch (\Exception $ex){
        $this->log($ex->getMessage());
    }
}
6
  • Hey did you get proper collection? Commented Feb 20, 2020 at 9:42
  • I want to check using a single SKU not in the collection ... I have a solution for collection but what if I want to check by single ID and SKU Commented Feb 20, 2020 at 12:54
  • You can't check it by single id or sku. Because, addWebsiteFilter() can able to use when we load collection. Commented Feb 20, 2020 at 13:02
  • If you will use this method then, it will not affect to site speed. Because, I added filter of addFieldToFilter(). So, It's load only single record. So, you can use my answer also. Commented Feb 20, 2020 at 13:03
  • let me review again and apply code Commented Feb 20, 2020 at 13:29

2 Answers 2

0

Replace this below code in your file :

protected $collectionFactory;

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
) {
    $this->collectionFactory = $collectionFactory;
}

public function getProduct($product_id){
    try{
        $product = $this->collectionFactory->create()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('entity_id', ['eq' => $product_id])
            ->addWebsiteFilter('your_website_id');
        return $product;
    }catch (\Exception $ex){
        $this->log($ex->getMessage());
    }
}

After that, you can get count of collection :

$productId = "10";
if(count($block->getProduct($productId)) > 0)
{
   echo "Product Exist";
} else {
   echo "Product Not Exist";
}

Remove generated and clean cache.

-1

The getProduct function takes the store id and the product id as parameter and it runs the product model for that specific store.

And the getProductCollection function takes store id as parameter and returns the product collection for that specific store view.


public function __construct(
    ...
    \Magento\Catalog\Model\ProductFactory $productFactory,
    ...
) {
    ...
    $this->productFactory = $productFactory;
    ...
}

public function getProduct($storeId, $productId) {
    $product = $this->productFactory->create()
                            ->setStoreId($storeId)
                            ->load($productId);
    return $product;
}

public function getProductCollection($storeId) {
    $productCollection = $this->productFactory->create()
                                    ->setStoreId($storeId)
                                    ->getCollection()
                                    ->addAttributeToSelect('*');
    return $productCollection;
}

4
  • If above code is works for you please accept and vote up for other people to get easier to find appropriate solution
    – Mr Zid
    Commented Feb 19, 2020 at 9:34
  • not working.... Commented Feb 19, 2020 at 10:12
  • before down vote , let me know the reason
    – Mr Zid
    Commented Feb 24, 2020 at 5:46
  • bcz it not working... Commented Feb 24, 2020 at 13:56

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.