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.

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.

1

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

0

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.