i'm trying to get the product id and the SKU of each product in magento with SQL, i fount that the table catalog_product_entity
has all the SKUs but no product id.
$id = YOUR_PRODUCT_ID; $sku = 'YOUR_PRODUCT_SKU'; $_product = $block->getProductById($id); $_product = $block->getProductBySku($sku); echo $_product->getEntityId(); echo '<br />'; echo $_product->getName(); Here is one of the best practices to get product information by product ID or SKU in Magento.
By default, Magento will automatically connect to its database using class Magento\Framework\App\ResourceConnection with getConnection() function. Calling from a template or other PHP files, You need to use the $this->resourceConnection object to run any Direct SQL Query.
What is SKU in Magento. SKU stands for a Stock Keeping unit, and it is a unique identification code in alphanumeric format. It is assigned to every product in an online Magento store for inventory purposes and can identify a product according to its manufacturer, color or size options, price, etc.
Go to your store core folder and open the env. php file under the app/etc folder. Find the next code, where database_name is actual database name which you use for your Magento 2 store.
entity_id is product's unique id so if you call $product->getId();
you actually get entity_id
The reason for that column name is that product is an EAV (Entity Attribute Value) model so product is an entity - standardized EAV entity identification column name but it can bring confusion...
And the query:
SELECT entity_id as product_id, sku FROM catalog_product_entity
The column 'entity_id' is the ID for the products. So the catalog_product_entity
table does contain both items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With