문제

Magento 2 provides product types as simple, bundle, configurable, grouped, gift card, etc.

Is there any way programmatically to fetch all the product types which exist in the Magento codebase including custom product type also?

This should probably scan the product-type.xml which is located in the respective module.

도움이 되었습니까?

해결책

Refer below code can be useful in any class.

use Magento\Catalog\Api\ProductTypeListInterface;
use Magento\Catalog\Model\ProductTypes\ConfigInterface;

Below code for construct method.

public function __construct(
     ConfigInterface $productTypeConfig,
     \Magento\Catalog\Api\Data\ProductTypeInterfaceFactory $productTypeFactory
) {
     $this->productTypeConfig = $productTypeConfig;
     $this->productTypeFactory = $productTypeFactory;
}

Core function to get all product types

public function getProductTypes()
{
    if ($this->productTypes === null) {
        $productTypes = [];
        foreach ($this->productTypeConfig->getAll() as $productTypeData) {
            /** @var \Magento\Catalog\Api\Data\ProductTypeInterface $productType */
            $productType = $this->productTypeFactory->create();
            $productType->setName($productTypeData['name'])
                ->setLabel($productTypeData['label']);
            $productTypes[] = $productType;
        }
        $this->productTypes = $productTypes;
    }
    return $this->productTypes;
}

Refer: /app/code/Magento/Catalog/Model/ProductTypeList.php

다른 팁

Cleaner way: inject Magento\Catalog\Model\Product\Type and call toOptionArray()

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top