Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApplyAttributesUpdate.php
Go to the documentation of this file.
1 <?php
8 
14 use Magento\Eav\Setup\EavSetupFactory;
15 
21 {
25  private $moduleDataSetup;
26 
30  private $eavSetupFactory;
31 
38  public function __construct(
39  ModuleDataSetupInterface $moduleDataSetup,
40  \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
41  ) {
42  $this->moduleDataSetup = $moduleDataSetup;
43  $this->eavSetupFactory = $eavSetupFactory;
44  }
45 
50  public function apply()
51  {
53  $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
54  $fieldList = [
55  'price',
56  'special_price',
57  'special_from_date',
58  'special_to_date',
59  'minimal_price',
60  'cost',
61  'tier_price',
62  'weight',
63  ];
64  foreach ($fieldList as $field) {
65  $applyTo = explode(
66  ',',
67  $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field, 'apply_to')
68  );
69  if (!in_array('bundle', $applyTo)) {
70  $applyTo[] = 'bundle';
71  $eavSetup->updateAttribute(
72  \Magento\Catalog\Model\Product::ENTITY,
73  $field,
74  'apply_to',
75  implode(',', $applyTo)
76  );
77  }
78  }
79 
80  $applyTo = explode(',', $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'cost', 'apply_to'));
81  unset($applyTo[array_search('bundle', $applyTo)]);
82  $eavSetup->updateAttribute(\Magento\Catalog\Model\Product::ENTITY, 'cost', 'apply_to', implode(',', $applyTo));
83 
87  $eavSetup->addAttribute(
88  \Magento\Catalog\Model\Product::ENTITY,
89  'price_type',
90  [
91  'type' => 'int',
92  'backend' => '',
93  'frontend' => '',
94  'label' => '',
95  'input' => '',
96  'class' => '',
97  'source' => '',
98  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
99  'visible' => true,
100  'required' => true,
101  'user_defined' => false,
102  'default' => '',
103  'searchable' => false,
104  'filterable' => false,
105  'comparable' => false,
106  'visible_on_front' => false,
107  'used_in_product_listing' => true,
108  'unique' => false,
109  'apply_to' => 'bundle'
110  ]
111  );
112 
113  $eavSetup->addAttribute(
114  \Magento\Catalog\Model\Product::ENTITY,
115  'sku_type',
116  [
117  'type' => 'int',
118  'backend' => '',
119  'frontend' => '',
120  'label' => '',
121  'input' => '',
122  'class' => '',
123  'source' => '',
124  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
125  'visible' => false,
126  'required' => true,
127  'user_defined' => false,
128  'default' => '',
129  'searchable' => false,
130  'filterable' => false,
131  'comparable' => false,
132  'visible_on_front' => false,
133  'unique' => false,
134  'apply_to' => 'bundle'
135  ]
136  );
137 
138  $eavSetup->addAttribute(
139  \Magento\Catalog\Model\Product::ENTITY,
140  'weight_type',
141  [
142  'type' => 'int',
143  'backend' => '',
144  'frontend' => '',
145  'label' => '',
146  'input' => '',
147  'class' => '',
148  'source' => '',
149  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
150  'visible' => false,
151  'required' => true,
152  'user_defined' => false,
153  'default' => '',
154  'searchable' => false,
155  'filterable' => false,
156  'comparable' => false,
157  'visible_on_front' => false,
158  'used_in_product_listing' => true,
159  'unique' => false,
160  'apply_to' => 'bundle'
161  ]
162  );
163 
164  $eavSetup->addAttribute(
165  \Magento\Catalog\Model\Product::ENTITY,
166  'price_view',
167  [
168  'group' => 'Advanced Pricing',
169  'type' => 'int',
170  'backend' => '',
171  'frontend' => '',
172  'label' => 'Price View',
173  'input' => 'select',
174  'class' => '',
175  'source' => \Magento\Bundle\Model\Product\Attribute\Source\Price\View::class,
176  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
177  'visible' => true,
178  'required' => true,
179  'user_defined' => false,
180  'default' => '',
181  'searchable' => false,
182  'filterable' => false,
183  'comparable' => false,
184  'visible_on_front' => false,
185  'used_in_product_listing' => true,
186  'unique' => false,
187  'apply_to' => 'bundle'
188  ]
189  );
190 
191  $eavSetup->addAttribute(
192  \Magento\Catalog\Model\Product::ENTITY,
193  'shipment_type',
194  [
195  'type' => 'int',
196  'backend' => '',
197  'frontend' => '',
198  'label' => 'Shipment',
199  'input' => '',
200  'class' => '',
201  'source' => '',
202  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
203  'visible' => false,
204  'required' => true,
205  'user_defined' => false,
206  'default' => '',
207  'searchable' => false,
208  'filterable' => false,
209  'comparable' => false,
210  'visible_on_front' => false,
211  'used_in_product_listing' => true,
212  'unique' => false,
213  'apply_to' => 'bundle'
214  ]
215  );
216  }
217 
221  public static function getDependencies()
222  {
223  return [];
224  }
225 
229  public static function getVersion()
230  {
231  return '2.0.0';
232  }
233 
237  public function getAliases()
238  {
239  return [];
240  }
241 }
__construct(ModuleDataSetupInterface $moduleDataSetup, \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)