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

Commit 90fbdbb

Browse files
author
Dinesh V B
committed
issue-8347-reviewed the article and made changes
1 parent 2c08cc0 commit 90fbdbb

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

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

Lines changed: 18 additions & 11 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 {#instgde-cli-multistore}
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

@@ -342,36 +342,43 @@ It should be visible and in bold text.
342342

343343
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.
344344

345-
### ADD OPTIONS ALONG WITH A NEW PRODUCT ATTRIBUTE
345+
### ADD OPTIONS ALONG WITH A NEW PRODUCT ATTRIBUTE {#AddOptionsAlongNewProductAttribute}
346346

347-
Basic instructions for creating a product attribute by setup or upgrade script can be found [in DevDocs]{#instgde-cli-multistore}. Before scripting the attribute creation, pick one of these two use cases for your options:
347+
Basic instructions for creating a product attribute by setup or upgrade script can be found [in DevDocs](#CreateProductAttributeByUpgradeScript). Before scripting the attribute creation, pick one of these two use cases for your options:
348348

349349
* 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.
350350
* You want a set of options which can be modified, added, or deleted through the admin panel by any user with admin access and proper authorization.
351351

352-
In the case of Use Case 1 (an 'immutable' set of options), follow Magento's instructions entitled "Step 3: Add a source model." You will create a model that contains and dynamically, on block rendering, returns your attribute's selectable options to the client.
352+
In the case of Use Case 1 (an 'immutable' set of options), follow Magento's instructions entitled ["Add a source model"](#AddSourceModel) You will create a model that contains and dynamically, on block rendering, returns your attribute's selectable options to the client.
353353

354-
In the case of Use Case 2 (a 'mutable' set of options), review Magento's article entitled "EAV and extension attributes," noting especially the attribute option entitled option. Also make certain 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.
354+
In the case of Use Case 2 (a 'mutable' set of options), review Magento's article entitled ["EAV and extension attributes"](_site/guides/v2.4/extension-dev-guide/attributes.html), noting especially the attribute option entitled option. Also make certain 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.
355355

356356
Investigating \Magento\Eav\Setup\EavSetup.php::addAttribute() and \Magento\Eav\Setup\EavSetup.php::addAttributeOptions() reveals that you may add a series of options with the following array:
357+
357358
'option' => ['values' => ['Option 1', 'Option 2', 'Option 3', etc.]]
358359

359360
Alternatively, you may designate a specific option sorting order as follows:
361+
360362
'option' => ['values' => [8 => 'Option 1', 3 => 'Option 2', 11 => 'Option 3', etc.]]
361363

362-
It's worth noting that store_id is hardcoded to default to 0 with no good way to change this other than overriding the class or using the plugin to intercept the database insert method, which would probably be a performance killer as often as that gets called.
364+
It's worth noting that store_id is hardcoded by default to 0 with no good way to change this other than overriding the class or using the plugin to intercept the database insert method, which would probably be a performance killer as often as that gets called.
365+
366+
### ADD OPTIONS TO AN EXISTING PRODUCT ATTRIBUTE
367+
368+
For relevant background, review the [preceding section](#AddOptionsAlongNewProductAttribute) on adding options at the same time as a new product attribute.
363369

364-
ADD OPTIONS TO AN EXISTING PRODUCT ATTRIBUTE
365-
For relevant background, review the preceding section on adding options at the same time as a new product attribute.
370+
* Adding options to an 'immutable' set of options is as simple as modifying your custom source model with the additional options you wish to provide.
366371

367-
Adding options to an 'immutable' set of options is as simple as modifying your custom source model with the additional options you wish to provide.
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. This is an example of how you might do this:
368373

369-
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. This is an example of how you might do this:
370374
Assign an array of new options to a variable:
375+
371376
$options = ['attribute_id' => null, 'values' => 'Option 1', 'Option 2', etc]];
372377

373378
Update your array with the attribute ID from the database:
379+
374380
$options['attribute_id'] = $eavSetup->getAttributeId($eavSetup->getEntityTypeId('catalog_product'), 'your_attribute_code');
375381

376382
Add your options:
383+
377384
$eavSetup->addAttributeOption($options);

0 commit comments

Comments
 (0)