2

I am creating a module that needs to add a number of fields to all commerce product types. In Drupal 7, I used the function commerce_product_types() to get all the types and than used that, but I can't seem to find the equialent in Drupal 8. So, I have commerce product type 'default', but I also have 'radios', and 'computers'. I don't know how many types there will be on the website, so it needs to automatically detect all types.

3
  • 1
    Use the entityTypeManager service to get them all
    – Kevin
    Commented Jul 21, 2018 at 14:30
  • Could you please give an example? Commented Jul 23, 2018 at 9:47
  • 1
    See this answer but use the entity type commerce_product_type.
    – Andy
    Commented Jul 23, 2018 at 11:31

1 Answer 1

2

Use this:

/**
* Get all product types
* @return array
*/
public function getProductTypes() {
   $product_types = \Drupal\commerce_product\Entity\ProductType::loadMultiple();
   return array_keys($product_types);
}
2
  • Yes, And for getting all variation types use \Entity\ProductVariationTypes:loadMultiple Commented Jan 5, 2020 at 16:11
  • $product_types = $this->entityTypeManager->getStorage('commerce_product_type')->loadMultiple();
    – leymannx
    Commented Dec 17, 2021 at 13:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.