Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SimpleProductTemplateGenerator.php
Go to the documentation of this file.
1 <?php
8 
13 use Magento\Catalog\Model\ProductFactory;
14 
20 {
24  private $fixture;
25 
29  private $productFactory;
30 
35  public function __construct(ProductFactory $productFactory, array $fixture)
36  {
37  $this->fixture = $fixture;
38  $this->productFactory = $productFactory;
39  }
40 
44  public function generateEntity()
45  {
46  $attributeSet = $this->fixture['attribute_set_id'];
47  $product = $this->getProductTemplate(
49  $this->fixture['additional_attributes']($attributeSet, 0, 0)
50  );
51  $product->save();
52 
53  return $product;
54  }
55 
63  private function getProductTemplate($attributeSet, $additionalAttributes = [])
64  {
65  $productRandomizerNumber = crc32(random_int(1, PHP_INT_MAX));
66  $product = $this->productFactory->create([
67  'data' => [
68  'attribute_set_id' => $attributeSet,
69  'type_id' => Type::TYPE_SIMPLE,
70  'name' => 'template name' . $productRandomizerNumber,
71  'url_key' => 'template-url' . $productRandomizerNumber,
72  'sku' => 'template_sku_simple' . $productRandomizerNumber,
73  'price' => 10,
74  'visibility' => Visibility::VISIBILITY_BOTH,
75  'status' => Status::STATUS_ENABLED,
76  'website_ids' => (array)$this->fixture['website_ids'](1, 0),
77  'category_ids' => isset($this->fixture['category_ids']) ? [2] : null,
78  'weight' => 1,
79  'description' => 'description',
80  'short_description' => 'short description',
81  'tax_class_id' => 2, //'taxable goods',
82  'stock_data' => [
83  'use_config_manage_stock' => 1,
84  'qty' => 100500,
85  'is_qty_decimal' => 0,
86  'is_in_stock' => 1
87  ],
88  ]
89  ]);
90 
91  foreach ($additionalAttributes as $attributeCode => $attributeValue) {
92  $product->setData($attributeCode, $attributeValue);
93  }
94 
95  return $product;
96  }
97 }
$attributeCode
Definition: extend.phtml:12