Get product_Id using following query,

public function getProductsIdBySku($skus)
        $select = $this->getReadConnection()
            ->from($this->getTable('catalog/product'), array('entity_id'))
            ->where('sku IN (?)', (array) $skus);

        return $this->getReadConnection()->fetchCol($select);

use \Magento\Catalog\Model\ResourceModel\Product\Collection;

I'm trying to do that in magento2...

$collection = $this->_productCollection->getCollection()->addFieldToSelect('*');

             ->where('sku = ?', (array) $skus);
                 return $collection;

Its not working for me....If any mistakes in my query ..



It's actually a lot easier than that, although I don't know which class $this->_productCollection exactly has:

$collection = $this->_productCollection->getCollection()->addAttributeToFilter('sku', $skus)->getAllIds()

...same as it was in Magento 1.


For magento 2

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

    $collection = $objectManager->Create('\Magento\Catalog\Model\ResourceModel\Product\Collection')
                  ->addAttributeToFilter('sku', $skus)->getAllIds();              

        echo $collection[0];    

get Product id


Try this

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
echo $product->getId();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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