Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BundleProductsFixture.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Fixtures;
8 
12 
28 {
32  const SKU_PATTERN = 'Bundle Product %s - %s';
33 
37  protected $priority = 42;
38 
42  private $productGenerator;
43 
47  private $bundleProductGenerator;
48 
52  private $productCollectionFactory;
53 
57  private $productStartIndex;
58 
62  private $productsAmountProvider;
63 
67  private $websiteCategoryProvider;
68 
72  private $priceProvider;
73 
83  public function __construct(
85  \Magento\Setup\Model\FixtureGenerator\ProductGenerator $productGenerator,
86  \Magento\Setup\Model\FixtureGenerator\BundleProductGenerator $bundleProductGenerator,
87  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
88  ProductsAmountProvider $productsAmountProvider,
89  WebsiteCategoryProvider $websiteCategoryProvider,
90  PriceProvider $priceProvider
91  ) {
92  parent::__construct($fixtureModel);
93  $this->productGenerator = $productGenerator;
94  $this->bundleProductGenerator = $bundleProductGenerator;
95  $this->productCollectionFactory = $productCollectionFactory;
96  $this->productsAmountProvider = $productsAmountProvider;
97  $this->websiteCategoryProvider = $websiteCategoryProvider;
98  $this->priceProvider = $priceProvider;
99  }
100 
105  public function execute()
106  {
107  $bundlesAmount = $this->fixtureModel->getValue('bundle_products', 0);
108  $bundleOptions = $this->fixtureModel->getValue('bundle_products_options', 1);
109  $bundleProductsPerOption = $this->fixtureModel->getValue('bundle_products_variation', 10);
110  $bundleOptionSuffix = $bundleOptions . '-' . $bundleProductsPerOption;
111  $variationCount = $bundleOptions * $bundleProductsPerOption;
112  $bundlesAmount = $this->productsAmountProvider->getAmount(
113  $bundlesAmount,
114  $this->getBundleSkuPattern($bundleOptionSuffix)
115  );
116 
117  if (!$bundlesAmount) {
118  return;
119  }
120  $variationSkuClosure = function ($productId, $entityNumber) use ($bundleOptionSuffix, $variationCount) {
121  $productIndex = $this->getBundleProductIndex($entityNumber, $variationCount);
122  $variationIndex = $this->getBundleVariationIndex($entityNumber, $variationCount);
123 
124  return sprintf($this->getBundleOptionItemSkuPattern($bundleOptionSuffix), $productIndex, $variationIndex);
125  };
126  $fixtureMap = [
127  'name' => $variationSkuClosure,
128  'sku' => $variationSkuClosure,
129  'price' => function ($index, $entityNumber) {
130  return $this->priceProvider->getPrice($entityNumber);
131  },
132  'website_ids' => function ($index, $entityNumber) use ($variationCount) {
133  $configurableIndex = $this->getBundleProductIndex($entityNumber, $variationCount);
134 
135  return $this->websiteCategoryProvider->getWebsiteIds($configurableIndex);
136  },
137  'visibility' => Visibility::VISIBILITY_NOT_VISIBLE,
138  ];
139  $this->productGenerator->generate($bundlesAmount * $bundleOptions * $bundleProductsPerOption, $fixtureMap);
140 
141  $optionPriceType = [
144  ];
145  $priceTypeClosure = function ($index) use ($optionPriceType) {
146  return $optionPriceType[$index % count($optionPriceType)];
147  };
148  $skuClosure = function ($index, $entityNumber) use ($bundleOptionSuffix) {
149  return sprintf(
150  $this->getBundleSkuPattern($bundleOptionSuffix),
151  $entityNumber + $this->getNewProductStartIndex()
152  );
153  };
154  $fixtureMap = [
155  '_bundle_options' => $bundleOptions,
156  '_bundle_products_per_option' => $bundleProductsPerOption,
157  '_bundle_variation_sku_pattern' => sprintf(
158  $this->getBundleOptionItemSkuPattern($bundleOptionSuffix),
159  $this->getNewProductStartIndex(),
160  '%s'
161  ),
162  'type_id' => Type::TYPE_CODE,
163  'name' => $skuClosure,
164  'sku' => $skuClosure,
165  'meta_title' => $skuClosure,
166  'price' => function ($index) use ($priceTypeClosure) {
167  return $priceTypeClosure($index) === LinkInterface::PRICE_TYPE_PERCENT
168  ? mt_rand(10, 90)
169  : $this->priceProvider->getPrice($index);
170  },
171  'priceType' => $priceTypeClosure,
172  'website_ids' => function ($index, $entityNumber) {
173  return $this->websiteCategoryProvider->getWebsiteIds($entityNumber + $this->getNewProductStartIndex());
174  },
175  'category_ids' => function ($index, $entityNumber) {
176  return $this->websiteCategoryProvider->getCategoryId($entityNumber + $this->getNewProductStartIndex());
177  },
178  ];
179  $this->bundleProductGenerator->generate($bundlesAmount, $fixtureMap);
180  }
181 
188  private function getBundleOptionItemSkuPattern($bundleOptionSuffix)
189  {
190  return $this->getBundleSkuPattern($bundleOptionSuffix) . ' - option %s';
191  }
192 
199  private function getBundleSkuPattern($bundleOptionSuffix)
200  {
201  return sprintf(self::SKU_PATTERN, '%s', $bundleOptionSuffix);
202  }
203 
209  private function getNewProductStartIndex()
210  {
211  if (null === $this->productStartIndex) {
212  $this->productStartIndex = $this->productCollectionFactory->create()
213  ->addFieldToFilter('type_id', Type::TYPE_CODE)
214  ->getSize() + 1;
215  }
216 
217  return $this->productStartIndex;
218  }
219 
227  private function getBundleProductIndex($entityNumber, $variationCount)
228  {
229  return floor($entityNumber / $variationCount) + $this->getNewProductStartIndex();
230  }
231 
239  private function getBundleVariationIndex($entityNumber, $variationCount)
240  {
241  return $entityNumber % $variationCount + 1;
242  }
243 
247  public function getActionTitle()
248  {
249  return 'Generating bundle products';
250  }
251 
255  public function introduceParamLabels()
256  {
257  return [
258  'bundle_products' => 'Bundle products',
259  ];
260  }
261 }
__construct(FixtureModel $fixtureModel, \Magento\Setup\Model\FixtureGenerator\ProductGenerator $productGenerator, \Magento\Setup\Model\FixtureGenerator\BundleProductGenerator $bundleProductGenerator, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, ProductsAmountProvider $productsAmountProvider, WebsiteCategoryProvider $websiteCategoryProvider, PriceProvider $priceProvider)
$index
Definition: list.phtml:44