Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartPriceRulesFixture.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Fixtures;
8 
22 {
26  protected $priority = 80;
27 
31  protected $cartPriceRulesCount = 0;
32 
37 
41  protected $cartRulesAdvancedType = false;
42 
46  private $ruleFactory;
47 
54  public function __construct(
56  \Magento\SalesRule\Model\RuleFactory $ruleFactory = null
57  ) {
58  parent::__construct($fixtureModel);
59  $this->ruleFactory = $ruleFactory ?: $this->fixtureModel->getObjectManager()
60  ->get(\Magento\SalesRule\Model\RuleFactory::class);
61  }
62 
67  public function execute()
68  {
69  $this->fixtureModel->resetObjectManager();
70  $this->cartPriceRulesCount = $this->fixtureModel->getValue('cart_price_rules', 0);
71  if (!$this->cartPriceRulesCount) {
72  return;
73  }
74 
75  $this->cartRulesAdvancedType = $this->fixtureModel->getValue('cart_price_rules_advanced_type', false);
76  $this->cartPriceRulesProductsFloor = $this->fixtureModel->getValue(
77  'cart_price_rules_floor',
78  3
79  );
80 
82  $storeManager = $this->fixtureModel->getObjectManager()->create(\Magento\Store\Model\StoreManager::class);
84  $category = $this->fixtureModel->getObjectManager()->get(\Magento\Catalog\Model\Category::class);
85 
86  //Get all websites
87  $categoriesArray = [];
88  $websites = $storeManager->getWebsites();
89  foreach ($websites as $website) {
90  //Get all groups
91  $websiteGroups = $website->getGroups();
92  foreach ($websiteGroups as $websiteGroup) {
93  $websiteGroupRootCategory = $websiteGroup->getRootCategoryId();
94  $category->load($websiteGroupRootCategory);
95  $categoryResource = $category->getResource();
96  //Get all categories
97  $resultsCategories = $categoryResource->getAllChildren($category);
98  foreach ($resultsCategories as $resultsCategory) {
99  $category->load($resultsCategory);
100  $structure = explode('/', $category->getPath());
101  if (count($structure) > 2) {
102  $categoriesArray[] = [$category->getId(), $website->getId()];
103  }
104  }
105  }
106  }
107  asort($categoriesArray);
108  $categoriesArray = array_values($categoriesArray);
109 
110  if ($this->cartRulesAdvancedType == false) {
111  $this->generateRules($this->ruleFactory, $categoriesArray);
112  } else {
113  $this->generateAdvancedRules($this->ruleFactory, $categoriesArray);
114  }
115  }
116 
122  public function generateCondition($ruleId, $categoriesArray)
123  {
124  return [
125  'conditions' => [
126  1 => [
127  'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
128  'aggregator' => 'all',
129  'value' => '1',
130  'new_child' => '',
131  ],
132  '1--1' => [
133  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
134  'attribute' => 'total_qty',
135  'operator' => '>=',
136  'value' => $this->cartPriceRulesProductsFloor + $ruleId,
137  ],
138  '1--2' => [
139  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
140  'value' => '1',
141  'aggregator' => 'all',
142  'new_child' => '',
143  ],
144  '1--2--1' => [
145  'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
146  'attribute' => 'category_ids',
147  'operator' => '==',
148  'value' => $categoriesArray[$ruleId % count($categoriesArray)][0],
149  ],
150  ],
151  'actions' => [
152  1 => [
153  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Combine::class,
154  'aggregator' => 'all',
155  'value' => '1',
156  'new_child' => '',
157  ],
158  ],
159  ];
160  }
161 
167  public function generateRules($ruleFactory, $categoriesArray)
168  {
169  for ($i = 0; $i < $this->cartPriceRulesCount; $i++) {
170  $ruleName = sprintf('Cart Price Rule %1$d', $i);
171  $data = [
172  'rule_id' => null,
173  'product_ids' => '',
174  'name' => $ruleName,
175  'description' => '',
176  'is_active' => '1',
177  'website_ids' => $categoriesArray[$i % count($categoriesArray)][1],
178  'customer_group_ids' => [
179  0 => '0',
180  1 => '1',
181  2 => '2',
182  3 => '3',
183  ],
184  'coupon_type' => '1',
185  'coupon_code' => '',
186  'uses_per_customer' => '',
187  'from_date' => '',
188  'to_date' => '',
189  'sort_order' => '',
190  'is_rss' => '1',
191  'rule' => $this->generateCondition($i, $categoriesArray),
192  'simple_action' => 'by_percent',
193  'discount_amount' => '10',
194  'discount_qty' => '0',
195  'discount_step' => '',
196  'apply_to_shipping' => '0',
197  'simple_free_shipping' => '0',
198  'stop_rules_processing' => '0',
199  'reward_points_delta' => '',
200  'store_labels' => [
201  0 => '',
202  1 => '',
203  2 => '',
204  3 => '',
205  4 => '',
206  5 => '',
207  6 => '',
208  7 => '',
209  8 => '',
210  9 => '',
211  10 => '',
212  11 => '',
213  ],
214  'page' => '1',
215  'limit' => '20',
216  'in_banners' => '',
217  'banner_id' => [
218  'from' => '',
219  'to' => '',
220  ],
221  'banner_name' => '',
222  'visible_in' => '',
223  'banner_is_enabled' => '',
224  'related_banners' => [],
225  ];
226  if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
227  && isset($data['discount_amount'])
228  ) {
229  $data['discount_amount'] = min(100, $data['discount_amount']);
230  }
231  if (isset($data['rule']['conditions'])) {
232  $data['conditions'] = $data['rule']['conditions'];
233  }
234  if (isset($data['rule']['actions'])) {
235  $data['actions'] = $data['rule']['actions'];
236  }
237  unset($data['rule']);
238 
239  $model = $ruleFactory->create();
240  $model->loadPost($data);
241  $useAutoGeneration = (int)!empty($data['use_auto_generation']);
242  $model->setUseAutoGeneration($useAutoGeneration);
243  $model->save();
244  }
245  }
246 
252  public function generateAdvancedCondition($ruleId, $categoriesArray)
253  {
254  // Generate only 200 region rules, the rest are based on category
255  if ($ruleId < ($this->cartPriceRulesCount - 200)) {
256  // Category
257  $firstCondition = [
258  'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
259  'attribute' => 'category_ids',
260  'operator' => '==',
261  'value' => $categoriesArray[($ruleId / 4) % count($categoriesArray)][0],
262  ];
263 
264  $subtotal = [0, 5, 10, 15];
265  // Subtotal
266  $secondCondition = [
267  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
268  'attribute' => 'base_subtotal',
269  'operator' => '>=',
270  'value' => $subtotal[$ruleId % 4],
271  ];
272 
273  return [
274  'conditions' => [
275  1 => [
276  'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
277  'aggregator' => 'all',
278  'value' => '1',
279  'new_child' => '',
280  ],
281  '1--1'=> [
282  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
283  'aggregator' => 'all',
284  'value' => '1',
285  'new_child' => '',
286  ],
287  '1--1--1' => $firstCondition,
288  '1--2' => $secondCondition
289  ],
290  'actions' => [
291  1 => [
292  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Combine::class,
293  'aggregator' => 'all',
294  'value' => '1',
295  'new_child' => '',
296  ],
297  ]
298  ];
299  } else {
300  // Shipping Region
301  $regions = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',
302  'Delaware', 'District of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois',
303  'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts',
304  'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
305  'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
306  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
307  'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia',
308  'Wisconsin', 'Wyoming'];
309  $firstCondition = [
310  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
311  'attribute' => 'region',
312  'operator' => '==',
313  'value' => $regions[($ruleId / 4) % 50],
314  ];
315 
316  $subtotals = [0, 5, 10, 15];
317  // Subtotal
318  $secondCondition = [
319  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
320  'attribute' => 'base_subtotal',
321  'operator' => '>=',
322  'value' => $subtotals[$ruleId % 4],
323  ];
324  return [
325  'conditions' => [
326  1 => [
327  'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
328  'aggregator' => 'all',
329  'value' => '1',
330  'new_child' => '',
331  ],
332  '1--1' => $firstCondition,
333  '1--2' => $secondCondition
334  ],
335  'actions' => [
336  1 => [
337  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Combine::class,
338  'aggregator' => 'all',
339  'value' => '1',
340  'new_child' => '',
341  ],
342  ]
343  ];
344  }
345  }
346 
352  public function generateAdvancedRules($ruleFactory, $categoriesArray)
353  {
354  $j = 0;
355  for ($i = 0; $i < $this->cartPriceRulesCount; $i++) {
356  if ($i < ($this->cartPriceRulesCount - 200)) {
357  $ruleName = sprintf('Cart Price Advanced Catalog Rule %1$d', $j);
358  } else {
359  $ruleName = sprintf('Cart Price Advanced Region Rule %1$d', $j);
360  }
361  $j++;
362  $data = [
363  'rule_id' => null,
364  'product_ids' => '',
365  'name' => $ruleName,
366  'description' => '',
367  'is_active' => '1',
368  'website_ids' => $categoriesArray[$i % count($categoriesArray)][1],
369  'customer_group_ids' => [
370  0 => '0',
371  1 => '1',
372  2 => '2',
373  3 => '3',
374  ],
375  'coupon_type' => '1',
376  'coupon_code' => '',
377  'uses_per_customer' => '',
378  'from_date' => '',
379  'to_date' => '',
380  'sort_order' => '',
381  'is_rss' => '1',
382  'rule' => $this->generateAdvancedCondition($i, $categoriesArray),
383  'simple_action' => 'cart_fixed',
384  'discount_amount' => '1',
385  'discount_qty' => '0',
386  'discount_step' => '',
387  'apply_to_shipping' => '0',
388  'simple_free_shipping' => '0',
389  'stop_rules_processing' => '0',
390  'reward_points_delta' => '',
391  'store_labels' => [
392  0 => '',
393  1 => '',
394  2 => '',
395  3 => '',
396  4 => '',
397  5 => '',
398  6 => '',
399  7 => '',
400  8 => '',
401  9 => '',
402  10 => '',
403  11 => '',
404  ],
405  'page' => '1',
406  'limit' => '20',
407  'in_banners' => '',
408  'banner_id' => [
409  'from' => '',
410  'to' => '',
411  ],
412  'banner_name' => '',
413  'visible_in' => '',
414  'banner_is_enabled' => '',
415  'related_banners' => [],
416  ];
417  if (isset($data['simple_action']) && $data['simple_action'] == 'cart_fixed'
418  && isset($data['discount_amount'])
419  ) {
420  $data['discount_amount'] = min(1, $data['discount_amount']);
421  }
422  if (isset($data['rule']['conditions'])) {
423  $data['conditions'] = $data['rule']['conditions'];
424  }
425  if (isset($data['rule']['actions'])) {
426  $data['actions'] = $data['rule']['actions'];
427  }
428  unset($data['rule']);
429 
430  $model = $ruleFactory->create();
431  $model->loadPost($data);
432  $useAutoGeneration = (int)!empty($data['use_auto_generation']);
433  $model->setUseAutoGeneration($useAutoGeneration);
434  $model->save();
435  }
436  }
437 
441  public function getActionTitle()
442  {
443  return 'Generating cart price rules';
444  }
445 
449  public function introduceParamLabels()
450  {
451  return [
452  'cart_price_rules' => 'Cart Price Rules'
453  ];
454  }
455 }
generateAdvancedRules($ruleFactory, $categoriesArray)
$storeManager
__construct(FixtureModel $fixtureModel, \Magento\SalesRule\Model\RuleFactory $ruleFactory=null)
$i
Definition: gallery.phtml:31