Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CatalogPriceRulesFixture.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Fixtures;
8 
19 {
23  protected $priority = 90;
24 
29  public function execute()
30  {
31  $catalogPriceRulesCount = $this->fixtureModel->getValue('catalog_price_rules', 0);
32  if (!$catalogPriceRulesCount) {
33  return;
34  }
35  $this->fixtureModel->resetObjectManager();
36 
38  $storeManager = $this->fixtureModel->getObjectManager()->create(\Magento\Store\Model\StoreManager::class);
40  $category = $this->fixtureModel->getObjectManager()->get(\Magento\Catalog\Model\Category::class);
42  $model = $this->fixtureModel->getObjectManager()->get(\Magento\CatalogRule\Model\Rule::class);
44  $metadataPool = $this->fixtureModel->getObjectManager()
45  ->get(\Magento\Framework\EntityManager\MetadataPool::class);
46  $metadata = $metadataPool->getMetadata(\Magento\CatalogRule\Api\Data\RuleInterface::class);
47 
48  //Get all websites
49  $categoriesArray = [];
50  $websites = $storeManager->getWebsites();
51  foreach ($websites as $website) {
52  //Get all groups
53  $websiteGroups = $website->getGroups();
54  foreach ($websiteGroups as $websiteGroup) {
55  $websiteGroupRootCategory = $websiteGroup->getRootCategoryId();
56  $category->load($websiteGroupRootCategory);
57  $categoryResource = $category->getResource();
58  //Get all categories
59  $resultsCategories = $categoryResource->getAllChildren($category);
60  foreach ($resultsCategories as $resultsCategory) {
61  $category->load($resultsCategory);
62  $structure = explode('/', $category->getPath());
63  if (count($structure) > 2) {
64  $categoriesArray[] = [$category->getId(), $website->getId()];
65  }
66  }
67  }
68  }
69  asort($categoriesArray);
70  $categoriesArray = array_values($categoriesArray);
71  $linkField = $metadata->getLinkField();
72  $idField = $metadata->getIdentifierField();
73 
74  for ($i = 0; $i < $catalogPriceRulesCount; $i++) {
75  $ruleName = sprintf('Catalog Price Rule %1$d', $i);
76  $data = [
77  $idField => null,
78  $linkField => null,
79  'name' => $ruleName,
80  'description' => '',
81  'is_active' => '1',
82  'website_ids' => $categoriesArray[$i % count($categoriesArray)][1],
83  'customer_group_ids' => [
84  0 => '0',
85  1 => '1',
86  2 => '2',
87  3 => '3',
88  ],
89  'from_date' => '',
90  'to_date' => '',
91  'sort_order' => '',
92  'rule' => [
93  'conditions' => [
94  1 => [
95  'type' => \Magento\CatalogRule\Model\Rule\Condition\Combine::class,
96  'aggregator' => 'all',
97  'value' => '1',
98  'new_child' => '',
99  ],
100  '1--1' => [
101  'type' => \Magento\CatalogRule\Model\Rule\Condition\Product::class,
102  'attribute' => 'category_ids',
103  'operator' => '==',
104  'value' => $categoriesArray[$i % count($categoriesArray)][0],
105  ],
106  ],
107  ],
108  'simple_action' => 'by_percent',
109  'discount_amount' => '15',
110  'stop_rules_processing' => '0',
111  'page' => '1',
112  'limit' => '20',
113  'in_banners' => '1',
114  'banner_id' => [
115  'from' => '',
116  'to' => '',
117  ],
118  'banner_name' => '',
119  'visible_in' => '',
120  'banner_is_enabled' => '',
121  'related_banners' => [],
122  ];
123  if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
124  && isset($data['discount_amount'])
125  ) {
126  $data['discount_amount'] = min(100, $data['discount_amount']);
127  }
128  if (isset($data['rule']['conditions'])) {
129  $data['conditions'] = $data['rule']['conditions'];
130  }
131  if (isset($data['rule']['actions'])) {
132  $data['actions'] = $data['rule']['actions'];
133  }
134  unset($data['rule']);
135 
136  $model->loadPost($data);
137  $useAutoGeneration = (int)!empty($data['use_auto_generation']);
138  $model->setUseAutoGeneration($useAutoGeneration);
139  $model->save();
140  }
141  }
142 
146  public function getActionTitle()
147  {
148  return 'Generating catalog price rules';
149  }
150 
154  public function introduceParamLabels()
155  {
156  return [
157  'catalog_price_rules' => 'Catalog Price Rules'
158  ];
159  }
160 }
$storeManager
$i
Definition: gallery.phtml:31