创建产品

  1. /**
  2. * uid [Integer]
  3. * Foreign key of the user that created the product.
  4. *
  5. * type [String] - [DEFAULT = default]
  6. * Foreign key of the product type being used.
  7. *
  8. * title [String]
  9. * The product title.
  10. *
  11. * stores [Array(\Drupal\commerce_store\Entity\StoreInterface)]
  12. * Array of stores this product belongs to.
  13. *
  14. * variations [Array(\Drupal\commerce_product\Entity\ProductVariationInterface)]
  15. * Array of variations that belong to this product.
  16. */
  17. // The variations that belong to this product.
  18. $variations = [
  19. $variation_blue_large,
  20. ];
  21. $product = \Drupal\commerce_product\Entity\Product::create([
  22. 'uid' => 1,
  23. 'type' => 'my_custom_product_type',
  24. 'title' => 'My Custom Product',
  25. 'stores' => [$store],
  26. 'variations' => $variations,
  27. ]);
  28. $product->save();
  29. // You can also add a variation to a product using the addVariation() method.
  30. $product->addVariation($variation_red_medium);
  31. $product->save();
  32. // Adding a variation to a product automatically creates a backreference on the variation.
  33. // You can access the product of a variation via:
  34. $variation->getProduct();

创建产品变体/sku

  1. /**
  2. * type [String] - [DEFAULT = default]
  3. * Foreign key of the variation type to use.
  4. *
  5. * sku [String]
  6. * The sku for this variation.
  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. * price [\Drupal\commerce_price\Price] - [OPTIONAL]
  13. * The price for this variation.
  14. *
  15. * title [String] - [POTENTIALLY NOT REQUIRED]
  16. * The title for the product variation.
  17. * If the variation type is set to generate a title, this is not used.
  18. * Otherwise, a title must be given.
  19. */
  20. $variation = \Drupal\commerce_product\Entity\ProductVariation::create([
  21. 'type' => 'my_custom_variation_type',
  22. 'sku' => 'test-product-01',
  23. 'status' => TRUE,
  24. 'price' => new \Drupal\commerce_price\Price('24.99', 'USD'),
  25. ]);
  26. $variation->save();

创建产品属性值

  1. /**
  2. * attribute [String]
  3. * Foreign key of the attribute we want.
  4. *
  5. * name [String]
  6. * The name of this value.
  7. */
  8. $red = \Drupal\commerce_product\Entity\ProductAttributeValue::create([
  9. 'attribute' => 'color',
  10. 'name' => 'Red',
  11. ]);
  12. $red->save();
  13. $blue = \Drupal\commerce_product\Entity\ProductAttributeValue::create([
  14. 'attribute' => 'color',
  15. 'name' => 'Blue',
  16. ]);
  17. $blue->save();
  18. $medium = \Drupal\commerce_product\Entity\ProductAttributeValue::create([
  19. 'attribute' => 'size',
  20. 'name' => 'Medium',
  21. ]);
  22. $medium->save();
  23. $large = \Drupal\commerce_product\Entity\ProductAttributeValue::create([
  24. 'attribute' => 'size',
  25. 'name' => 'Large',
  26. ]);
  27. $large->save();

读产品

  1. // Loading is based off of the primary key [Integer]
  2. // 1 would be the first one saved, 2 the next, etc.
  3. $product = \Drupal\commerce_product\Entity\Product::load(1);

读变体/sku

  1. // Loading is based off of the primary key [Integer]
  2. // 1 would be the first one saved, 2 the next, etc.
  3. $red = \Drupal\commerce_product\Entity\ProductAttributeValue::load(1);

读产品属性值

  1. // Loading is based off of the primary key [Integer]
  2. // 1 would be the first one saved, 2 the next, etc.
  3. $variation = \Drupal\commerce_product\Entity\ProductVariation::load(1);

某个产品是否存在某个属性值

  1. // Look up while filtering by Attribute
  2. $productAttributeId = \Drupal::entityTypeManager()
  3. ->getStorage('commerce_product_attribute_value')
  4. ->getQuery()
  5. ->condition('attribute', 'attribute_machine_name')
  6. ->condition('field_value', field_value)
  7. ->execute();

通过属性值创建变体

  1. /**
  2. * attribute_<ATTRIBUTE_ID> [\Drupal\commerce_product\Entity\ProductAttributeValueInterface]
  3. * The attribute value entity to use for the attribute type.
  4. */
  5. $variation_red_medium = \Drupal\commerce_product\Entity\ProductVariation::create([
  6. 'type' => 'my_custom_variation_type',
  7. 'sku' => 'product-red-medium',
  8. 'price' => new \Drupal\commerce_price\Price('10.00', 'USD'),
  9. 'attribute_color' => $red,
  10. 'attribute_size' => $medium,
  11. ]);
  12. $variation_red_medium->save();
  13. $variation_blue_large = \Drupal\commerce_product\Entity\ProductVariation::create([
  14. 'type' => 'my_custom_variation_type',
  15. 'sku' => 'product-blue-large',
  16. 'price' => new \Drupal\commerce_price\Price('10.00', 'USD'),
  17. 'attribute_color' => $blue,
  18. 'attribute_size' => $large,
  19. ]);
  20. $variation_blue_large->save();

修改产品标题字段

如果你想改产品名称改成内容名称,可以通过hook:

  1. use Drupal\Core\Entity\EntityTypeInterface;
  2. /**
  3. * Implements hook_entity_base_field_info_alter().
  4. */
  5. function mymodule_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  6. if ($entity_type->id() == 'commerce_product') {
  7. // Change the title field label.
  8. $fields['title']->setLabel(t('Product name'));
  9. }
  10. }