Skip to content

Commit

Permalink
Added creating attribute sets to docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mlof committed Oct 1, 2021
1 parent 44e06ec commit 31ec2bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ Alright, this one's a doozy. There's no real way to get an option ID for an attr

### Creating a attribute set

```csharp
var hasDvi = new AttributeModel(Context, "monitor_has_dvi") {
DefaultFrontendLabel = "Has DVI",
FrontendInput = AttributeFrontendInput.Select
};
hasDvi.AddOptions("Yes", "No");
await hasDvi.SaveAsync();

var resolution = new AttributeModel(Context, "monitor_resolution")
{
DefaultFrontendLabel = "Resolution",
FrontendInput = AttributeFrontendInput.Select
};
resolution.AddOptions("1366x768", "1920x1080", "2560x1080", "2560x1440");
await resolution.SaveAsync();
var attributeSet = new AttributeSetModel(Context, "Monitors", EntityType.CatalogProduct);
attributeSet.AddGroup("Panel");
attributeSet.AssignAttribute("Panel", resolution.AttributeCode);
attributeSet.AddGroup("Connections");
attributeSet.AssignAttribute("Connections", hasDvi.AttributeCode);

await attributeSet.SaveAsync();
```



### Creating a category
Expand Down
13 changes: 11 additions & 2 deletions source/Magento.RestClient/Domain/Models/AttributeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public class AttributeModel : IDomainModel
private bool _frontendInputChanged;
private List<Option> _options;

public AttributeModel(IAdminContext context, string attributeCode)
public AttributeModel(IAdminContext context, string attributeCode, string label = "")
{
this.Validator = new AttributeModelValidator();

_context = context;
this.AttributeCode = attributeCode;
this.DefaultFrontendLabel = label;
Refresh().GetAwaiter().GetResult();
}

Expand Down Expand Up @@ -74,7 +76,6 @@ public async Task Refresh()
}
}



public async Task SaveAsync()
{
Expand Down Expand Up @@ -125,6 +126,14 @@ public Task Delete()
return _context.Attributes.DeleteProductAttribute(this.AttributeCode);
}

public void AddOptions(params string[] options)
{
foreach (var option in options)
{
AddOption(option);
}
}

public void AddOption(string option)
{
if (option != null)
Expand Down

0 comments on commit 31ec2bd

Please sign in to comment.