0

Get product_Id using following query,

public function getProductsIdBySku($skus)
    {
        $select = $this->getReadConnection()
            ->select()
            ->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('*');

        $collection->getSelect()
                ->reset(\Magento\Framework\DB\Select::COLUMNS)
                ->columns('entity_id');
             ->where('sku = ?', (array) $skus);
                 return $collection;

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

Thanks.

Mahi M
  • 2,540
  • 2
  • 36
  • 83

3 Answers3

2

For magento 2

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

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

        //print_r($collection[0]);  
        echo $collection[0];    

get Product id

Nikhil Vaghela
  • 1,341
  • 9
  • 19
1

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.

simonthesorcerer
  • 4,813
  • 2
  • 19
  • 32
0

Try this

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