1

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.

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

Use this:

/**
* Get all product types
* @return array
*/
public function getProductTypes() {
   $product_types = \Drupal\commerce_product\Entity\ProductType::loadMultiple();
   return array_keys($product_types);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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