5

I have created an extension that creates product attribute "Is Old" after installing extension. Attribute is in drop down form with Yes and No.

Attribute value is saving in "catalog_product_entity_int" table.

The main purpose of creating this attribute is that admin can decide if current product is old or not.

Now I want to fetch all those product's entity id that is set to "YES" in "Is Old" drop down value and by specific SKU.

I have fetched product's entity id by SKU but now I want to fetch entity id by sku and is_old value "Yes" as well.

Here is my code to fetch product entity id by SKU only.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$select = "Select * FROM catalog_product_entity WHERE sku='123'";
$rowArray = $connection->fetchRow($select);
$enitity_id = $rowArray['entity_id'];

enter image description here

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Mar 21, 2017 at 6:59

4 Answers 4

11

I don't understand why are you fetching a product id by filtering with SKU and is_old attribute. Because SKU is already an unique value.

I am posting my answer here with two different cases.

Case 1: Get product Id using sku.

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productId = $objectManager->get('Magento\Catalog\Model\Product')->getIdBySku('your_sku');
print_r($productId);
?>

Case 2: Get old products using sku.

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$products = $objectManager->get('Magento\Catalog\Model\Product')
 ->getCollection()
 ->addAttributeToFilter('is_old',1)
 ->addAttributeToFilter('sku',array('sku1','sku2',.....));
echo "<pre>";
print_r($products->getData());
?>
answered Mar 21, 2017 at 9:32
2
  • I need same in Magento 2.1.9 version. Can you please help. Commented Nov 9, 2017 at 9:22
  • it works for me,please use "\Magento\Framework\App\ObjectManager::getInstance();" instead of " Magento\Framework\App\ObjectManager::getInstance();" Commented May 24, 2019 at 9:50
6

Instead of calling direct query, my suggestion is to get Magento collection by using product collection factory class:

Magento\Catalog\Model\ResourceModel\Product\CollectionFactory

Then filter that collection via addAttributeTofilter

<?php
namespace [YourClassNameSpace];
class [ClasssName]
{
 protected $_productCollectionFactory;
 public function __construct(
 ....
 \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
 ...
 ) {
 $this->_productCollectionFactory = $productCollectionFactory;
 .....
 }
 public function getProductCollection(){
 $collection = $this->_productCollectionFactory->create();
 // Assume tghat yes/no attribute code
 $collection->addAttributeToFilter('Is Old_ATTRIBUTE_CODE',1); //Use 1 for yes value
 /* Filter collection by multiple sk */
 $collection->addAttributeToFilter('sku',array('Sku1','sku2'.....,'sku2'));
 }
}
answered Mar 21, 2017 at 7:44
2

You can also get the product id via SKU by using this class:

Magento\Catalog\Model\Product

protected $_productCollection;
public function __construct(
.......
\Magento\Catalog\Model\Product $productCollection,
.......
){
$this->_productCollection = $productCollection;
.....
} 
public function getProductIdBySku() {
 $sku = "your_sku";
 $productId = $this->_productCollection->getIdBySku($sku);
 print_r($productId);
}
answered Feb 13, 2019 at 10:14
0

for not big number of sku

<?php

namespace [YourClassNameSpace];

use Magento\Catalog\Model\ResourceModel\Product;

class [ClasssName] {

private Product $productModel;
public function __construct(
 ....
 Product $productModel,
 ...
) {
 $this->productModel = $productModel;
 .....
}
 public function someFunction(){
 $pId = $this->productModel->getIdBySku($sku);
 }

}

answered Jul 8, 2024 at 6:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.