Last active
June 27, 2022 02:49
-
-
Save Alexander-Pop/51d0ec948fe6f5c4750ad76e117e6c2d to your computer and use it in GitHub Desktop.
Magento 2 - Add Quote / Order Item / custom Customer Attribute (Patch/Schema) #magento2 #quote #order #attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyNamespace\MyModule\Setup\Patch\Data; | |
use Magento\Customer\Model\Customer; | |
use Magento\Customer\Setup\CustomerSetupFactory; | |
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; | |
use Magento\Framework\Setup\Patch\DataPatchInterface; | |
class AddCustomCustomerAttribute implements DataPatchInterface | |
{ | |
/** | |
* @var CustomerSetupFactory | |
*/ | |
private $customerSetupFactory; | |
/** | |
* @var AttributeSetFactory | |
*/ | |
private $attributeSetFactory; | |
/** | |
* AddCustomCustomerAttribute constructor. | |
* @param CustomerSetupFactory $customerSetupFactory | |
* @param AttributeSetFactory $attributeSetFactory | |
*/ | |
public function __construct( | |
CustomerSetupFactory $customerSetupFactory, | |
AttributeSetFactory $attributeSetFactory | |
) { | |
$this->customerSetupFactory = $customerSetupFactory; | |
$this->attributeSetFactory = $attributeSetFactory; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public static function getDependencies() | |
{ | |
return []; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function getAliases() | |
{ | |
return []; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function apply() | |
{ | |
/** @var \Magento\Customer\Setup\CustomerSetup $customerSetup */ | |
$customerSetup = $this->customerSetupFactory->create(); | |
$customerSetup->removeAttribute(Customer::ENTITY, 'my_custom_attribute'); | |
$customerSetup->addAttribute( | |
Customer::ENTITY, | |
'my_custom_attribute', | |
[ | |
'group' => 'General', | |
'type' => 'text', | |
'label' => 'My Label', | |
'input' => 'text', | |
'position' => 160, // position defined in the admin customer form | |
'user_defined' => true, | |
'required' => false, | |
'system' => false, // Allow to save values , @see Magento\Customer\Model\Metadata\CustomerMetadata::getCustomAttributesMetadata() | |
], | |
/** Boolean example | |
[ | |
'type' => 'int', | |
'label' => 'Boolean Attribute', | |
'input' => 'boolean', | |
'backend' => \Magento\Customer\Model\Attribute\Backend\Data\Boolean::class, | |
'visible' => false, | |
'position' => 160, | |
'user_defined' => true, | |
'required' => false, | |
'system' => false, | |
'default' => 0, | |
] | |
**/ | |
); | |
$attribute = $customerSetup->getEavConfig()->getAttribute( | |
Customer::ENTITY, | |
'my_custom_attribute' | |
); | |
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY); | |
$attributeSetId = $customerEntity->getDefaultAttributeSetId(); | |
$attributeSet = $this->attributeSetFactory->create(); | |
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); | |
/** @var mixed[] $rules */ | |
$rules = [ | |
'input_validation' => 'alphanumeric', | |
'min_text_length' => 10, | |
'max_text_length' => 15, | |
]; | |
$attribute->setValidationRules($rules); | |
$attribute->addData( | |
[ | |
'attribute_set_id' => $attributeSetId, | |
'attribute_group_id' => $attributeGroupId, | |
'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'], //define the areas where the attribute field should be displayed | |
] | |
); | |
$attribute->save(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyNamespace\MyModule\Setup\Patch\Schema; | |
use Magento\Framework\DB\Ddl\Table; | |
use Magento\Framework\Setup\Patch\SchemaPatchInterface; | |
class AddCustomQuoteOrderItemAttribute implements SchemaPatchInterface | |
{ | |
/** | |
* @var \Magento\Quote\Setup\QuoteSetup | |
*/ | |
private $quoteSetup; | |
/** | |
* @var \Magento\Sales\Setup\SalesSetup | |
*/ | |
private $salesSetup; | |
/** | |
* AddCustomQuoteItemOrderItemAttribute constructor. | |
* @param \Magento\Quote\Setup\QuoteSetup $quoteSetup | |
* @param \Magento\Sales\Setup\SalesSetup $salesSetup | |
*/ | |
public function __construct( | |
\Magento\Quote\Setup\QuoteSetup $quoteSetup, | |
\Magento\Sales\Setup\SalesSetup $salesSetup | |
) { | |
$this->quoteSetup = $quoteSetup; | |
$this->salesSetup = $salesSetup; | |
} | |
/** | |
* @return string[] | |
*/ | |
public static function getDependencies() | |
{ | |
return []; | |
} | |
/** | |
* @return string[] | |
*/ | |
public function getAliases() | |
{ | |
return []; | |
} | |
/** | |
* @return AddCustomQuoteItemOrderItemAttribute|void | |
*/ | |
public function apply() | |
{ | |
$this->quoteSetup->addAttribute( | |
'quote_item', | |
'custom_attribute', | |
[ | |
'type' => Table::TYPE_TEXT, | |
'nullable' => true, | |
'comment' => 'custom attribute' | |
] | |
); | |
$this->salesSetup->addAttribute( | |
'order_item', | |
'custom_attribute', | |
[ | |
'type' => Table::TYPE_TEXT, | |
'nullable' => true, | |
'comment' => 'custom attribute' | |
] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment