Skip to content

Product Attributes

Code Slicer edited this page May 18, 2023 · 13 revisions

Allows managing the eav attributes related to products. Import it as follows:

private Migration\Facade\ProductAttribute $productAttribute;

public function __construct(
    Migration\Context $context,
    Migration\Facade\ProductAttribute $productAttribute
) {
    parent::__construct($context);
    $this->productAttribute = $productAttribute;
}

Inherit Methods

💡 Tip: all eav attribute capabilities are available for this entity:
https://github.com/discorgento/module-migrations/wiki/Eav-Attributes

createDropdown($code, $label, $values, $config = [])

Create a new dropdown attribute. Eg.:

$this->productAttribute->createDropdown('sizes', "Clothes Size", ['P', 'M', 'G']);

assignToAttributeSet($code, $options = [])

Simulates the process of navigating to "Stores->Attribute Set" and assigning an attribute there. Eg.:

// Assign to "Marketplace" attribute set, into the "Shipping Quote" group, after the "fragile" attribute
$this->productAttribute->assignToAttributeSet(
    'size', // attribute code to assign
    'Clothes', // (optional) attribute set, fallback to "Default"
    'Tech Specs', // (optional) group name, fallback to the default one, usually "General"
    'gender' // (optional) put your attribute after this one
);

unassignFromAttributeSet($attributeCode, $attributeSetId = null)

Pretty much what the name implies.

// Assign to "Marketplace" attribute set, into the "Shipping Quote" group, after the "fragile" attribute
$this->productAttribute->unassignFromAttributeSet(
    'size', // attribute code to assign
    'Clothes', // (optional) attribute set, fallback to "Default"
);