Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 71eb452

Browse files
authored
Merge pull request #8395 from dineshvb/issue-8347-add-new-product-attribute-documentation
Issue-8347 - added new product attribute documentation
2 parents e5de40b + 71b9880 commit 71eb452

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

src/_videos/fundamentals/add-new-product-attribute.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
8787

8888
{% endcollapsible %}
8989

90-
## Step 2 Create an InstallData script
90+
## Step 2 Create an InstallData script {#CreateProductAttributeByUpgradeScript}
9191

9292
Next, we need to create the InstallData script.
9393
Because adding an attribute technically adds records into several tables, such as `eav_attribute` and `catalog_eav_attribute,` this is data manipulation, not a schema change.
@@ -196,7 +196,7 @@ For now, we’ll just quickly go through most important ones:
196196
* **visible_on_front:** A flag that defines whether an attribute should be shown on the “More Information” tab on the frontend
197197
* **is_html_allowed_on_front:** Defines whether an attribute value may contain HTML
198198

199-
## Step 3: Add a source model
199+
## Step 3: Add a source model {#AddSourceModel}
200200

201201
Next, we need to create the source model:
202202

@@ -337,3 +337,54 @@ backend model has executed successfully, so now we’ll set it to Wool and save
337337

338338
Having saved the product, we’ll now move to the frontend.
339339
It should be visible and in bold text.
340+
341+
## Product Attribute Option Creation
342+
343+
A product attribute of type multiselect or select will present selectable options to the user. These options may be added manually through the admin panel or by upgrade script. The script process is slightly different depending on whether the options are being added at the moment of attribute creation or whether the options are being added at a later time to an existing attribute.
344+
345+
### Add options to a new prouduct attribute {#AddOptionsAlongNewProductAttribute}
346+
347+
Basic instructions for creating a product attribute by setup or upgrade script can be found [above](#CreateProductAttributeByUpgradeScript). Before scripting the attribute creation, pick one of these two use cases for your options:
348+
349+
1. You want a set of options which cannot be modified by a user through the admin panel and which can only be changed through a future code push.
350+
1. You want a set of options which can be modified, added or deleted through the admin panel.
351+
352+
For use case `1` (an 'immutable' set of options), follow the above instructions ["Add a source model"](#AddSourceModel). You will create a model that contains and dynamically returns the attribute's selectable options to the client.
353+
354+
For use case `2` (a 'mutable' set of options), see ["EAV and extension attributes"]({{ site.baseurl }}/guides/v2.4/extension-dev-guide/attributes.html). Make sure to declare 'Magento\Eav\Model\Entity\Attribute\Source\Table' as the value for the 'source' attribute option. This ensures that Magento will store options in the appropriate database table.
355+
356+
With `\Magento\Eav\Setup\EavSetup.php::addAttribute()` and `\Magento\Eav\Setup\EavSetup.php::addAttributeOptions()` you can add a series of options with the following array:
357+
358+
```php
359+
'option' => ['values' => ['Option 1', 'Option 2', 'Option 3', etc.]];
360+
```
361+
362+
Alternatively, you may designate a specific option sorting order as follows:
363+
364+
```php
365+
'option' => ['values' => [8 => 'Option 1', 3 => 'Option 2', 11 => 'Option 3', etc.]]
366+
```
367+
368+
### Add options to an existing product attribute
369+
370+
* To add options to an 'immutable' set of options, modify the custom source model with the additional options you wish to provide.
371+
372+
* Adding options to a 'mutable' set of options leverages the same `EavSetup` object as you use when creating an attribute with options, but requires an additional step because `EavSetup` needs to know to which attribute you want to assign new options.
373+
374+
1. Assign an array of new options to a variable:
375+
376+
```php
377+
$options = ['attribute_id' => null, 'values' => 'Option 1', 'Option 2', etc]];
378+
```
379+
380+
1. Update your array with the attribute ID from the database:
381+
382+
```php
383+
$options['attribute_id'] = $eavSetup->getAttributeId($eavSetup->getEntityTypeId('catalog_product'), 'your_attribute_code');
384+
```
385+
386+
1. Add your options:
387+
388+
```php
389+
$eavSetup->addAttributeOption($options);
390+
```

0 commit comments

Comments
 (0)