Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InstallDownloadableAttributes.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Eav\Setup\EavSetupFactory;
15 
21 {
25  private $moduleDataSetup;
26 
30  private $eavSetupFactory;
31 
37  public function __construct(
38  ModuleDataSetupInterface $moduleDataSetup,
39  EavSetupFactory $eavSetupFactory
40  ) {
41  $this->moduleDataSetup = $moduleDataSetup;
42  $this->eavSetupFactory = $eavSetupFactory;
43  }
44 
49  public function apply()
50  {
52  $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
56  $eavSetup->addAttribute(
57  \Magento\Catalog\Model\Product::ENTITY,
58  'links_purchased_separately',
59  [
60  'type' => 'int',
61  'backend' => '',
62  'frontend' => '',
63  'label' => 'Links can be purchased separately',
64  'input' => '',
65  'class' => '',
66  'source' => '',
67  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
68  'visible' => false,
69  'required' => true,
70  'user_defined' => false,
71  'default' => '',
72  'searchable' => false,
73  'filterable' => false,
74  'comparable' => false,
75  'visible_on_front' => false,
76  'unique' => false,
77  'apply_to' => 'downloadable',
78  'used_in_product_listing' => true
79  ]
80  );
81 
82  $eavSetup->addAttribute(
83  \Magento\Catalog\Model\Product::ENTITY,
84  'samples_title',
85  [
86  'type' => 'varchar',
87  'backend' => '',
88  'frontend' => '',
89  'label' => 'Samples title',
90  'input' => '',
91  'class' => '',
92  'source' => '',
93  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
94  'visible' => false,
95  'required' => true,
96  'user_defined' => false,
97  'default' => '',
98  'searchable' => false,
99  'filterable' => false,
100  'comparable' => false,
101  'visible_on_front' => false,
102  'unique' => false,
103  'apply_to' => 'downloadable'
104  ]
105  );
106  $eavSetup->addAttribute(
107  \Magento\Catalog\Model\Product::ENTITY,
108  'links_title',
109  [
110  'type' => 'varchar',
111  'backend' => '',
112  'frontend' => '',
113  'label' => 'Links title',
114  'input' => '',
115  'class' => '',
116  'source' => '',
117  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
118  'visible' => false,
119  'required' => true,
120  'user_defined' => false,
121  'default' => '',
122  'searchable' => false,
123  'filterable' => false,
124  'comparable' => false,
125  'visible_on_front' => false,
126  'unique' => false,
127  'apply_to' => 'downloadable'
128  ]
129  );
130  $eavSetup->addAttribute(
131  \Magento\Catalog\Model\Product::ENTITY,
132  'links_exist',
133  [
134  'type' => 'int',
135  'backend' => '',
136  'frontend' => '',
137  'label' => '',
138  'input' => '',
139  'class' => '',
140  'source' => '',
141  'global' => true,
142  'visible' => false,
143  'required' => false,
144  'user_defined' => false,
145  'default' => '0',
146  'searchable' => false,
147  'filterable' => false,
148  'comparable' => false,
149  'visible_on_front' => false,
150  'unique' => false,
151  'apply_to' => 'downloadable',
152  'used_in_product_listing' => 1
153  ]
154  );
155  $fieldList = [
156  'price',
157  'special_price',
158  'special_from_date',
159  'special_to_date',
160  'minimal_price',
161  'cost',
162  'tier_price',
163  'weight',
164  ];
165  // make these attributes applicable to downloadable products
166  foreach ($fieldList as $field) {
167  $applyTo = explode(
168  ',',
169  $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field, 'apply_to')
170  );
171  if (!in_array('downloadable', $applyTo)) {
172  $applyTo[] = 'downloadable';
173  $eavSetup->updateAttribute(
174  \Magento\Catalog\Model\Product::ENTITY,
175  $field,
176  'apply_to',
177  implode(',', $applyTo)
178  );
179  }
180  }
181  }
182 
186  public static function getDependencies()
187  {
188  return [];
189  }
190 
194  public static function getVersion()
195  {
196  return '2.0.0';
197  }
198 
202  public function getAliases()
203  {
204  return [];
205  }
206 }
__construct(ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory)