Skip to content

Instantly share code, notes, and snippets.

@rparsi
Created July 24, 2018 20:36
Show Gist options
  • Select an option

  • Save rparsi/f6fd9f27b5f40eda49b00a9e3851b501 to your computer and use it in GitHub Desktop.

Select an option

Save rparsi/f6fd9f27b5f40eda49b00a9e3851b501 to your computer and use it in GitHub Desktop.
Magento 2.2.x add custom product attribute
<?php
namespace MyCustomVendor\Catalog\Setup;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Setup\CategorySetup as Base;
use MyCustomVendor\Catalog\Api\CustomProductInterface; // interface with constants defining the keys for all the custom attributes
class ProductSetup extends Base
{
public function createBooleanAttributeData($label = 'replace this value', $sortOrder = 1)
{
return [
'type' => 'int',
'label' => $label,
'input' => 'select',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'sort_order' => $sortOrder,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
];
}
public function createTextAttributeData($label = 'replace this value', $sortOrder = 1)
{
return [
'type' => 'varchar',
'label' => $label,
'input' => 'text',
'sort_order' => $sortOrder,
'required' => false,
'user_defined' => true,
'default' => null,
'visible' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
];
}
public function createSelectAttributeData($label = 'replace this value', $source, $sortOrder = 1)
{
return [
'type' => 'varchar', // or int, if the values are entity ids
'label' => $label,
'input' => 'select',
'source' => $source, // must extend \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
'sort_order' => $sortOrder,
'required' => false,
'user_defined' => true,
'default' => null,
'visible' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
// if 'group' is not set, then the attribute is added as top level, under "General Information"
];
}
public function getCustomAttributesData()
{
$sortOrder = 1;
$isProduct = $this->createBooleanAttributeData('Is a product', $sortOrder++);
$isProduct['type'] = 'varchar';
$isProduct['default'] = null;
$isProduct['global'] = \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL;
$startingPrice = $this->createTextAttributeData('Starting Price', $sortOrder++);
$startingPrice['type'] = 'text';
$startingPrice['global'] = \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL;
$templateStateId = $this->createTextAttributeData('Template State ID', $sortOrder);
$startingPrice['type'] = 'text';
$startingPrice['global'] = \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL;
// the interface defines all the attribute codes, can include methods (getters, setters, etc) and implemented as a DataObject
return [
CustomProductInterface::KEY_IS_PRODUCT => $isProduct,
CustomProductInterface::KEY_STARTING_PRICE => $startingPrice,
CustomProductInterface::KEY_TEMPLATE_STATE_ID => $templateStateId
];
}
public function addCustomAttributes()
{
$customAttributesData = $this->getCustomAttributesData();
foreach ($customAttributesData as $attributeCode => $customAttributesDatum) {
$attribute = $this->getAttribute(Product::ENTITY, $attributeCode);
if (!$attribute) {
$this->addAttribute(Product::ENTITY, $attributeCode, $customAttributesDatum);
}
$this->updateAttribute(Product::ENTITY, $attributeCode, $customAttributesDatum);
}
}
}
/**
* Your MyCustomVendor/Catalog/Setup/InstallData::install would look like this
* same for UpgradeData, as this approach to maintaining custom attributes is version independent
*
* public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
* {
* $setup->startSetup();
*
* // @var CategorySetup $categorySetup
* $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
*
* // @var ProductSetup $productSetup
* $productSetup = $this->productSetupFactory->create(['setup' => $setup]);
*
* $categorySetup->addCustomAttributes();
* $productSetup->addCustomAttributes();
*
* $setup->endSetup();
* }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment