Which entity allows scoped attributes?
-
14-02-2021 - |
Question
/vendor/magento/module-eav/Model/Entity/Attribute/ScopedAttributeInterface.php
There are 3 defined scopes
/**
* @api
* @since 100.0.2
*/
interface ScopedAttributeInterface
{
const SCOPE_STORE = 0;
const SCOPE_GLOBAL = 1;
const SCOPE_WEBSITE = 2;
}
and here is the DB for
entity_type_code
mysql> SELECT entity_type_id, entity_type_code FROM eav_entity_type;
+----------------+------------------+
| entity_type_id | entity_type_code |
+----------------+------------------+
| 1 | customer |
| 2 | customer_address |
| 3 | catalog_category |
| 4 | catalog_product |
| 5 | order |
| 6 | invoice |
| 7 | creditmemo |
| 8 | shipment |
+----------------+------------------+
So above all entities, Which Entity Allows scope attributes.
customer
and customer_address
Does not support Scope Attribute [Global]
catalog_category
and catalog_product
Does support Scope Attribute [Default, Website and Store]
So the question is Sales tables does support Scope Attribute ? Because sales tables are stored as flat tables in Magento.
5: order
6: invoice
7: creditmemo
8: shipment
Solution
customer
andcustomer_address
Does not support Scope Attribute [Globle]catalog_category
andcatalog_product
Does support Scope Attribute [Default, Website and Store]
So the question is Sales tables does support Scope Attribute ? Because sales tables are stored as Flat table in Magento
Sales DOES NOT support Scope Attributes.
Confirmation 1 : Check the eav_entity_type
table in Magento 2
attribute_model
andentity_attribute_collection
is NULL for sales related entities.- Where as Customer Attributes can be created Scope wise.
- Catalog Attributes can be created Scope wise.
Confirmation 2 : Check getDefaultEntities
function in Setup
folder, It clears the doubt.
vendor/magento/module-customer/Setup/CustomerSetup.php
/**
* Retrieve default entities: customer, customer_address
*
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getDefaultEntities()
{
$entities = [
'customer' => [
'entity_type_id' => \Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'entity_model' => \Magento\Customer\Model\ResourceModel\Customer::class,
'attribute_model' => \Magento\Customer\Model\Attribute::class,
'table' => 'customer_entity',
'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
'additional_attribute_table' => 'customer_eav_attribute',
'entity_attribute_collection' => \Magento\Customer\Model\ResourceModel\Attribute\Collection::class,
'attributes' => [
'website_id' => [
'type' => 'static',
'label' => 'Associate to Website',
'input' => 'select',
'source' => \Magento\Customer\Model\Customer\Attribute\Source\Website::class,
'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Website::class,
'sort_order' => 10,
'position' => 10,
'adminhtml_only' => 1,
],
'store_id' => [
'type' => 'static',
'label' => 'Create In',
'input' => 'select',
'source' => \Magento\Customer\Model\Customer\Attribute\Source\Store::class,
'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Store::class,
'sort_order' => 20,
'visible' => false,
'adminhtml_only' => 1,
],
vendor/magento/module-catalog/Setup/CategorySetup.php
/**
* Default entities and attributes
*
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getDefaultEntities()
{
return [
'catalog_category' => [
'entity_type_id' => self::CATEGORY_ENTITY_TYPE_ID,
'entity_model' => Category::class,
'attribute_model' => Attribute::class,
'table' => 'catalog_category_entity',
'additional_attribute_table' => 'catalog_eav_attribute',
'entity_attribute_collection' =>
Collection::class,
'attributes' => [
'name' => [
'type' => 'varchar',
'label' => 'Name',
'input' => 'text',
'sort_order' => 1,
'global' => ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
],
vendor/magento/module-sales/Setup/SalesSetup.php
/**
* @return array
*/
public function getDefaultEntities()
{
$entities = [
'order' => [
'entity_type_id' => self::ORDER_ENTITY_TYPE_ID,
'entity_model' => \Magento\Sales\Model\ResourceModel\Order::class,
'table' => 'sales_order',
'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
'increment_per_store' => true,
'attributes' => [],
],
'invoice' => [
'entity_type_id' => self::INVOICE_PRODUCT_ENTITY_TYPE_ID,
'entity_model' => \Magento\Sales\Model\ResourceModel\Order\Invoice::class,
'table' => 'sales_invoice',
'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
'increment_per_store' => true,
'attributes' => [],
],
'creditmemo' => [
'entity_type_id' => self::CREDITMEMO_PRODUCT_ENTITY_TYPE_ID,
'entity_model' => \Magento\Sales\Model\ResourceModel\Order\Creditmemo::class,
'table' => 'sales_creditmemo',
'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
'increment_per_store' => true,
'attributes' => [],
],
'shipment' => [
'entity_type_id' => self::SHIPMENT_PRODUCT_ENTITY_TYPE_ID,
'entity_model' => \Magento\Sales\Model\ResourceModel\Order\Shipment::class,
'table' => 'sales_shipment',
'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
'increment_per_store' => true,
'attributes' => [],
],
];
return $entities;
}
In Conformation 2 - getDefaultEntities
is Scope wise for Customer Attributes and Catalog as well Categories But Sales DOES NOT support Scope Attributes, It is not defined in Setup.
OTHER TIPS
As per my understanding you can only create custom attribute for below entity types:
- Customer (Scope: Global)
- customer_address (Scope: Global)
- catalog_category (Scope: Default, Website and Store)
- catalog_product (Scope: Default, Website and Store)
as when you have a look on eav_entity_type
table's column additional_attribute_table
and entity_attribute_collection
you can see the values for above 4 entity_types
only.
And for Customer attribute by default scope is Global (default) so you don't have to set the scope attribute. For rest i.e Catalog Category and Product you can set any scope i.e Global (default), Website or Store
For rest I don't think you can create, but if you want to create extra column for rest of the entity you can use extension attribute feature of M2