创建产品类型
/**
* id [String]
* Primary key for this product type.
*
* label [String]
* Label for this product type
*
* description [String]
* Description for this product.
*
* variationType [String] - [DEFAULT = default]
* Foreign key for the variation type used.
*
* multipleVariations [Bool] - [OPTIONAL, DEFAULTS TO TRUE]
* Whether products of this type can have multiple variations.
*
* injectVariationFields [Bool] - [OPTIONAL, DEFAULTS TO TRUE]
* Whether or not to inject the variation fields.
*/
// Create the product type.
$product_type = \Drupal\commerce_product\Entity\ProductType::create([
'id' => 'my_custom_product_type',
'label' => "My custom product type",
'description' => '',
'variationType' => 'my_custom_variation_type',
'multipleVariations' => TRUE,
'injectVariationFields' => TRUE,
]);
$product_type->save();
// These three functions must be called to add the appropriate fields to the type
commerce_product_add_variations_field($product_type);
commerce_product_add_stores_field($product_type);
commerce_product_add_body_field($product_type);
创建产品变体类型
/**
* id [String]
* The primary key for this variation type.
*
* label [String]
* The label for this variation type.
*
* status [Bool] - [OPTIONAL, DEFAULTS TO TRUE]
* [AVAILABLE = FALSE, TRUE]
* Whether or not it's enabled or disabled. 1 for enabled.
*
* orderItemType [String] - [DEFAULT = default]
* Foreign key for the order item type to use.
*
* generateTitle [Bool] - [DEFAULT = TRUE]
* Whether or not it should generate the title based off of product label and attributes.
*/
$variation_type = \Drupal\commerce_product\Entity\ProductVariationType::create([
'id' => 'my_custom_variation_type',
'label' => 'Variation Type With Color',
'status' => TRUE,
'orderItemType' => 'default',
'generateTitle' => TRUE,
]);
$variation_type->save();
创建产品属性
/**
* id [String]
* The primary key for this attribute.
*
* label [String]
* The label for this attribute.
*/
$color_attribute = \Drupal\commerce_product\Entity\ProductAttribute::create([
'id' => 'color',
'label' => 'Color',
]);
$color_attribute->save();
$size_attribute = \Drupal\commerce_product\Entity\ProductAttribute::create([
'id' => 'size',
'label' => 'Size',
]);
$size_attribute->save();
// We load a service that adds the attributes to the variation type we made previously.
$attribute_field_manager = \Drupal::service('commerce_product.attribute_field_manager');
$attribute_field_manager->createField($color_attribute, 'my_custom_variation_type');
$attribute_field_manager->createField($size_attribute, 'my_custom_variation_type');
读产品类型
// Loading is based off of the primary key [String] that was defined when creating it.
$product_type = \Drupal\commerce_product\Entity\ProductType::load('my_custom_product_type');
读变体类型
// Loading is based off of the primary key [String] that was defined when creating it.
$variation_type = \Drupal\commerce_product\Entity\ProductVariationType::load('my_custom_variation_type');
可购买实体
其实,所谓产品变体为什么能够被认定为可购买实体,是因为实现了PurchasableEntityInterface
接口。反过来,任何drupal的实体,如果实现了PurchasableEntityInterface
,那么它就可以被认定为购买实体。
如果您开发了实现此PurchasableEntityInterface的内容实体类型,则可以设置一个订单项目类型,以允许客户购买您的自定义实体类型,而不是标准产品变体。
<?php
namespace Drupal\commerce;
use Drupal\Core\Entity\ContentEntityInterface;
/**
* Defines the interface for purchasable entities.
*
* Lives in Drupal\commerce instead of Drupal\commerce_order so that entity
* type providing modules such as commerce_product don't need to depend
* on commerce_order.
*/
interface PurchasableEntityInterface extends ContentEntityInterface {
/**
* Gets the stores through which the purchasable entity is sold.
*
* @return \Drupal\commerce_store\Entity\StoreInterface[]
* The stores.
*/
public function getStores();
/**
* Gets the purchasable entity's order item type ID.
*
* Used for finding/creating the appropriate order item when purchasing a
* product (adding it to an order).
*
* @return string
* The order item type ID.
*/
public function getOrderItemTypeId();
/**
* Gets the purchasable entity's order item title.
*
* Saved in the $order_item->title field to protect the order items of
* completed orders against changes in the referenced purchased entity.
*
* @return string
* The order item title.
*/
public function getOrderItemTitle();
/**
* Gets the purchasable entity's price.
*
* @return \Drupal\commerce_price\Price|null
* The price, or NULL.
*/
public function getPrice();
}