Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddGiftMessageAttributes.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Setup\CategorySetupFactory;
10 use Magento\Quote\Setup\QuoteSetupFactory;
11 use Magento\Sales\Setup\SalesSetupFactory;
15 
17 {
21  private $moduleDataSetup;
22 
26  private $categorySetupFactory;
27 
31  private $quoteSetupFactory;
32 
36  private $salesSetupFactory;
37 
45  public function __construct(
46  \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
47  CategorySetupFactory $categorySetupFactory,
48  QuoteSetupFactory $quoteSetupFactory,
49  SalesSetupFactory $salesSetupFactory
50  ) {
51  $this->moduleDataSetup = $moduleDataSetup;
52  $this->categorySetupFactory = $categorySetupFactory;
53  $this->quoteSetupFactory = $quoteSetupFactory;
54  $this->salesSetupFactory = $salesSetupFactory;
55  }
56 
60  public function apply()
61  {
65  $options = ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 'visible' => false, 'required' => false];
66  $entities = ['quote', 'quote_address', 'quote_item', 'quote_address_item'];
68  $quoteSetup = $this->quoteSetupFactory->create(['setup' => $this->moduleDataSetup]);
69  foreach ($entities as $entity) {
70  $quoteSetup->addAttribute($entity, 'gift_message_id', $options);
71  }
72 
74  $salesSetup = $this->salesSetupFactory->create(['setup' => $this->moduleDataSetup]);
75  $salesSetup->addAttribute('order', 'gift_message_id', $options);
76  $salesSetup->addAttribute('order_item', 'gift_message_id', $options);
80  $salesSetup->addAttribute('order_item', 'gift_message_available', $options);
82  $catalogSetup = $this->categorySetupFactory->create(['setup' => $this->moduleDataSetup]);
83  $catalogSetup->addAttribute(
84  \Magento\Catalog\Model\Product::ENTITY,
85  'gift_message_available',
86  [
87  'group' => 'Gift Options',
88  'backend' => \Magento\Catalog\Model\Product\Attribute\Backend\Boolean::class,
89  'frontend' => '',
90  'label' => 'Allow Gift Message',
91  'input' => 'select',
92  'class' => '',
93  'source' => \Magento\Catalog\Model\Product\Attribute\Source\Boolean::class,
94  'global' => true,
95  'visible' => true,
96  'required' => false,
97  'user_defined' => false,
98  'default' => '',
99  'apply_to' => '',
100  'input_renderer' => \Magento\GiftMessage\Block\Adminhtml\Product\Helper\Form\Config::class,
101  'visible_on_front' => false,
102  'is_used_in_grid' => true,
103  'is_visible_in_grid' => false,
104  'is_filterable_in_grid' => false,
105  ]
106  );
107  $groupName = 'Autosettings';
108  $entityTypeId = $catalogSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
109  $attributeSetId = $catalogSetup->getAttributeSetId($entityTypeId, 'Default');
110  $attribute = $catalogSetup->getAttribute($entityTypeId, 'gift_message_available');
111  if ($attribute) {
112  $catalogSetup->addAttributeToGroup(
115  $groupName,
116  $attribute['attribute_id'],
117  60
118  );
119  }
120  }
121 
125  public static function getDependencies()
126  {
127  return [];
128  }
129 
133  public static function getVersion()
134  {
135  return '2.0.0';
136  }
137 
141  public function getAliases()
142  {
143  return [];
144  }
145 }
__construct(\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup, CategorySetupFactory $categorySetupFactory, QuoteSetupFactory $quoteSetupFactory, SalesSetupFactory $salesSetupFactory)
$entity
Definition: element.phtml:22