Magento 2, Get Product type from the Product id using Product Repository interface.
Magento supports multiple product types include, simple, Bundle, Grouped, Configurable, Virtual, Downloadable, Gift Card(Magento Commerce).
You want to know the product type from the product id, You need to refer below code snippet,
<?php namespace Jesadiya\ProductType\Model; use Psr\Log\LoggerInterface; use Magento\Catalog\Api\ProductRepositoryInterface; class ProductType { /** * @var ProductRepositoryInterface */ protected $productRepository; /** * @var LoggerInterface */ private $logger; public function __construct( LoggerInterface $logger, ProductRepositoryInterface $productRepository ) { $this->logger = $logger; $this->productRepository = $productRepository; } /** * Product type * * @param int $productId * @return string|null */ public function getProductType($productId) { $productType = null; try { $productType = $this->productRepository->getById($productId)->getTypeId(); } catch (\Exception $exception) { $this->logger->error($exception->getMessage()); } return $productType; } }
You can get the product type with passing the product id.
$productId = 10;
$this->getProductType($productId);
Output:
simple, virtual, downloadable, configurable, bundle or grouped based on the product type.