Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BundleProductTemplateGenerator.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Bundle\Api\Data\LinkInterfaceFactory;
10 use Magento\Bundle\Api\Data\OptionInterfaceFactory;
15 use Magento\Catalog\Model\ProductFactory;
16 
22 {
26  private $fixture;
27 
31  private $productFactory;
32 
36  private $optionFactory;
37 
41  private $linkFactory;
42 
49  public function __construct(
50  ProductFactory $productFactory,
51  array $fixture,
52  OptionInterfaceFactory $optionFactory,
53  LinkInterfaceFactory $linkFactory
54  ) {
55  $this->fixture = $fixture;
56  $this->productFactory = $productFactory;
57  $this->optionFactory = $optionFactory;
58  $this->linkFactory = $linkFactory;
59  }
60 
64  public function generateEntity()
65  {
66  $product = $this->getProductTemplate(
67  $this->fixture['attribute_set_id']
68  );
69  $product->save();
70 
71  return $product;
72  }
73 
80  private function getProductTemplate($attributeSet)
81  {
82  $bundleOptions = $this->fixture['_bundle_options'];
83  $bundleProductsPerOption = $this->fixture['_bundle_products_per_option'];
84  $bundleVariationSkuPattern = $this->fixture['_bundle_variation_sku_pattern'];
85  $productRandomizerNumber = crc32(random_int(1, PHP_INT_MAX));
86  $bundleProduct = $this->productFactory->create([
87  'data' => [
88  'attribute_set_id' => $attributeSet,
89  'type_id' => Type::TYPE_BUNDLE,
90  'name' => 'template name' . $productRandomizerNumber,
91  'url_key' => 'template-url' . $productRandomizerNumber,
92  'sku' => 'template_sku_bundle' . $productRandomizerNumber,
93  'price' => 10,
94  'visibility' => Visibility::VISIBILITY_BOTH,
95  'status' => Status::STATUS_ENABLED,
96  'website_ids' => [1],
97  'category_ids' => isset($this->fixture['category_ids']) ? [2] : null,
98  'weight' => 1,
99  'description' => 'description',
100  'short_description' => 'short description',
101  'tax_class_id' => 2, //'taxable goods',
102  'price_type' => \Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED,
103  'price_view' => 1,
104  'stock_data' => [
105  'use_config_manage_stock' => 1,
106  'qty' => 100500,
107  'is_qty_decimal' => 0,
108  'is_in_stock' => 1
109  ],
110  'can_save_bundle_selections' => true,
111  'affect_bundle_product_selections' => true,
112 
113  ]
114  ]);
115 
116  $bundleProductOptions = [];
117  $variationN = 0;
118  for ($i = 1; $i <= $bundleOptions; $i++) {
119  $option = $this->optionFactory->create(['data' => [
120  'title' => 'Bundle Product Items ' . $i,
121  'default_title' => 'Bundle Product Items ' . $i,
122  'type' => 'select',
123  'required' => 1,
124  'delete' => '',
125  'position' => $bundleOptions - $i,
126  'option_id' => '',
127  ]]);
128  $option->setSku($bundleProduct->getSku());
129 
130  $links = [];
131  for ($linkN = 1; $linkN <= $bundleProductsPerOption; $linkN++) {
132  $variationN++;
133  $link = $this->linkFactory->create(['data' => [
134  'sku' => sprintf($bundleVariationSkuPattern, $variationN),
135  'qty' => 1,
136  'can_change_qty' => 1,
137  'position' => $linkN - 1,
138  'price_type' => 0,
139  'price' => 0.0,
140  'option_id' => '',
141  'is_default' => $linkN === 1,
142  ]]);
143  $links[] = $link;
144  }
145  $option->setProductLinks($links);
146  $bundleProductOptions[] = $option;
147  }
148 
149  $extension = $bundleProduct->getExtensionAttributes();
150  $extension->setBundleProductOptions($bundleProductOptions);
151  $bundleProduct->setExtensionAttributes($extension);
152  // Need for set "has_options" field
153  $bundleProduct->setBundleOptionsData($bundleProductOptions);
154  $bundleSelections = array_map(function ($option) {
155  return array_map(function ($link) {
156  return $link->getData();
157  }, $option->getProductLinks());
158  }, $bundleProductOptions);
159  $bundleProduct->setBundleSelectionsData($bundleSelections);
160 
161  return $bundleProduct;
162  }
163 }
__construct(ProductFactory $productFactory, array $fixture, OptionInterfaceFactory $optionFactory, LinkInterfaceFactory $linkFactory)
$bundleProduct
$i
Definition: gallery.phtml:31