创建产品类型

  1. /**
  2. * id [String]
  3. * Primary key for this product type.
  4. *
  5. * label [String]
  6. * Label for this product type
  7. *
  8. * description [String]
  9. * Description for this product.
  10. *
  11. * variationType [String] - [DEFAULT = default]
  12. * Foreign key for the variation type used.
  13. *
  14. * multipleVariations [Bool] - [OPTIONAL, DEFAULTS TO TRUE]
  15. * Whether products of this type can have multiple variations.
  16. *
  17. * injectVariationFields [Bool] - [OPTIONAL, DEFAULTS TO TRUE]
  18. * Whether or not to inject the variation fields.
  19. */
  20. // Create the product type.
  21. $product_type = \Drupal\commerce_product\Entity\ProductType::create([
  22. 'id' => 'my_custom_product_type',
  23. 'label' => "My custom product type",
  24. 'description' => '',
  25. 'variationType' => 'my_custom_variation_type',
  26. 'multipleVariations' => TRUE,
  27. 'injectVariationFields' => TRUE,
  28. ]);
  29. $product_type->save();
  30. // These three functions must be called to add the appropriate fields to the type
  31. commerce_product_add_variations_field($product_type);
  32. commerce_product_add_stores_field($product_type);
  33. commerce_product_add_body_field($product_type);

创建产品变体类型

  1. /**
  2. * id [String]
  3. * The primary key for this variation type.
  4. *
  5. * label [String]
  6. * The label for this variation type.
  7. *
  8. * status [Bool] - [OPTIONAL, DEFAULTS TO TRUE]
  9. * [AVAILABLE = FALSE, TRUE]
  10. * Whether or not it's enabled or disabled. 1 for enabled.
  11. *
  12. * orderItemType [String] - [DEFAULT = default]
  13. * Foreign key for the order item type to use.
  14. *
  15. * generateTitle [Bool] - [DEFAULT = TRUE]
  16. * Whether or not it should generate the title based off of product label and attributes.
  17. */
  18. $variation_type = \Drupal\commerce_product\Entity\ProductVariationType::create([
  19. 'id' => 'my_custom_variation_type',
  20. 'label' => 'Variation Type With Color',
  21. 'status' => TRUE,
  22. 'orderItemType' => 'default',
  23. 'generateTitle' => TRUE,
  24. ]);
  25. $variation_type->save();

创建产品属性

  1. /**
  2. * id [String]
  3. * The primary key for this attribute.
  4. *
  5. * label [String]
  6. * The label for this attribute.
  7. */
  8. $color_attribute = \Drupal\commerce_product\Entity\ProductAttribute::create([
  9. 'id' => 'color',
  10. 'label' => 'Color',
  11. ]);
  12. $color_attribute->save();
  13. $size_attribute = \Drupal\commerce_product\Entity\ProductAttribute::create([
  14. 'id' => 'size',
  15. 'label' => 'Size',
  16. ]);
  17. $size_attribute->save();
  18. // We load a service that adds the attributes to the variation type we made previously.
  19. $attribute_field_manager = \Drupal::service('commerce_product.attribute_field_manager');
  20. $attribute_field_manager->createField($color_attribute, 'my_custom_variation_type');
  21. $attribute_field_manager->createField($size_attribute, 'my_custom_variation_type');

读产品类型

  1. // Loading is based off of the primary key [String] that was defined when creating it.
  2. $product_type = \Drupal\commerce_product\Entity\ProductType::load('my_custom_product_type');

读变体类型

  1. // Loading is based off of the primary key [String] that was defined when creating it.
  2. $variation_type = \Drupal\commerce_product\Entity\ProductVariationType::load('my_custom_variation_type');

可购买实体

其实,所谓产品变体为什么能够被认定为可购买实体,是因为实现了PurchasableEntityInterface接口。反过来,任何drupal的实体,如果实现了PurchasableEntityInterface,那么它就可以被认定为购买实体。

如果您开发了实现此PurchasableEntityInterface的内容实体类型,则可以设置一个订单项目类型,以允许客户购买您的自定义实体类型,而不是标准产品变体。

  1. <?php
  2. namespace Drupal\commerce;
  3. use Drupal\Core\Entity\ContentEntityInterface;
  4. /**
  5. * Defines the interface for purchasable entities.
  6. *
  7. * Lives in Drupal\commerce instead of Drupal\commerce_order so that entity
  8. * type providing modules such as commerce_product don't need to depend
  9. * on commerce_order.
  10. */
  11. interface PurchasableEntityInterface extends ContentEntityInterface {
  12. /**
  13. * Gets the stores through which the purchasable entity is sold.
  14. *
  15. * @return \Drupal\commerce_store\Entity\StoreInterface[]
  16. * The stores.
  17. */
  18. public function getStores();
  19. /**
  20. * Gets the purchasable entity's order item type ID.
  21. *
  22. * Used for finding/creating the appropriate order item when purchasing a
  23. * product (adding it to an order).
  24. *
  25. * @return string
  26. * The order item type ID.
  27. */
  28. public function getOrderItemTypeId();
  29. /**
  30. * Gets the purchasable entity's order item title.
  31. *
  32. * Saved in the $order_item->title field to protect the order items of
  33. * completed orders against changes in the referenced purchased entity.
  34. *
  35. * @return string
  36. * The order item title.
  37. */
  38. public function getOrderItemTitle();
  39. /**
  40. * Gets the purchasable entity's price.
  41. *
  42. * @return \Drupal\commerce_price\Price|null
  43. * The price, or NULL.
  44. */
  45. public function getPrice();
  46. }