|
| 1 | +<?php |
| 2 | +namespace ${Vendorname}\${Modulename}\Setup; |
| 3 | + |
| 4 | +use Magento\Customer\Setup\CustomerSetup; |
| 5 | +use Magento\Customer\Setup\CustomerSetupFactory; |
| 6 | +use Magento\Framework\Setup\InstallDataInterface; |
| 7 | +use Magento\Framework\Setup\ModuleContextInterface; |
| 8 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
| 9 | +use Magento\Quote\Setup\QuoteSetup; |
| 10 | +use Magento\Quote\Setup\QuoteSetupFactory; |
| 11 | +use Magento\Sales\Setup\SalesSetup; |
| 12 | +use Magento\Sales\Setup\SalesSetupFactory; |
| 13 | +use Magento\Config\Model\ResourceModel\Config; |
| 14 | + |
| 15 | +/** |
| 16 | + * Upgrade Data script |
| 17 | + * |
| 18 | + * @codeCoverageIgnore |
| 19 | + */ |
| 20 | +class InstallData implements InstallDataInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var CustomerSetupFactory |
| 24 | + */ |
| 25 | + protected $customerSetupFactory; |
| 26 | + /** |
| 27 | + * @var QuoteSetupFactory |
| 28 | + */ |
| 29 | + private $quoteSetupFactory; |
| 30 | + /** |
| 31 | + * @var SalesSetupFactory |
| 32 | + */ |
| 33 | + private $salesSetupFactory; |
| 34 | + /** |
| 35 | + * @var Config |
| 36 | + */ |
| 37 | + private $config; |
| 38 | + |
| 39 | + public function __construct( |
| 40 | + CustomerSetupFactory $customerSetupFactory, |
| 41 | + QuoteSetupFactory $quoteSetupFactory, |
| 42 | + SalesSetupFactory $salesSetupFactory, |
| 43 | + Config $config |
| 44 | + ) { |
| 45 | + $this->customerSetupFactory = $customerSetupFactory; |
| 46 | + $this->quoteSetupFactory = $quoteSetupFactory; |
| 47 | + $this->salesSetupFactory = $salesSetupFactory; |
| 48 | + $this->config = $config; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritdoc} |
| 53 | + * @codingStandardsIgnoreStart |
| 54 | + */ |
| 55 | + public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) //@codingStandardsIgnoreLine |
| 56 | + { |
| 57 | + $setup->startSetup(); |
| 58 | + |
| 59 | + $this->addCustomerAddressAttribute($setup, '${fieldname}', '${fieldname}', 120, true, false); |
| 60 | + $this->createQuoteAndSalesAttributes($setup, '${fieldname}'); |
| 61 | + $this->addAttributeInAddressTemplates(); |
| 62 | + |
| 63 | + $setup->endSetup(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param ModuleDataSetupInterface $setup |
| 68 | + * @param string $attributeCode |
| 69 | + * @param string $label |
| 70 | + * @param int $sortOrder |
| 71 | + * @param bool $showOnFrontend |
| 72 | + * @param bool $required |
| 73 | + * @param string $type |
| 74 | + * @param string $input |
| 75 | + * @param string|null $sourceModel |
| 76 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 77 | + */ |
| 78 | + private function addCustomerAddressAttribute( |
| 79 | + ModuleDataSetupInterface $setup, |
| 80 | + string $attributeCode, |
| 81 | + string $label, |
| 82 | + int $sortOrder, |
| 83 | + bool $showOnFrontend = false, |
| 84 | + bool $required = false, |
| 85 | + string $type = 'varchar', |
| 86 | + string $input = 'text', |
| 87 | + $sourceModel = null |
| 88 | + ) { |
| 89 | + /** @var CustomerSetup $customerSetup */ |
| 90 | + $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); |
| 91 | + |
| 92 | + $customerSetup->addAttribute( |
| 93 | + 'customer_address', |
| 94 | + $attributeCode, |
| 95 | + [ |
| 96 | + 'type' => $type, |
| 97 | + 'label' => $label, |
| 98 | + 'input' => $input, |
| 99 | + 'required' => $required, |
| 100 | + 'sort_order' => 0, |
| 101 | + 'visible' => true, |
| 102 | + 'user_defined' => 0, |
| 103 | + 'system' => false, |
| 104 | + 'is_used_in_grid' => false, |
| 105 | + 'source' => $sourceModel, |
| 106 | + ] |
| 107 | + ); |
| 108 | + |
| 109 | + $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', $attributeCode); |
| 110 | + $forms = ['adminhtml_customer']; |
| 111 | + if ($showOnFrontend) { |
| 112 | + $forms = ['adminhtml_customer_address', 'customer_register_address', 'customer_address_edit']; |
| 113 | + } |
| 114 | + $attribute->setData('used_in_forms', $forms); |
| 115 | + $attribute->save(); //@codingStandardsIgnoreLine |
| 116 | + $customerSetup->updateAttribute('customer_address', $attributeCode, 'sort_order', $sortOrder); |
| 117 | + } |
| 118 | + |
| 119 | + private function createQuoteAndSalesAttributes( |
| 120 | + ModuleDataSetupInterface $setup, |
| 121 | + string $attributeCode, |
| 122 | + string $type = 'varchar', |
| 123 | + string $length = '255' |
| 124 | + ) { |
| 125 | + /** @var QuoteSetup $quoteSetup */ |
| 126 | + $quoteSetup = $this->quoteSetupFactory->create(['setup' => $setup]); |
| 127 | + /** @var SalesSetup $salesSetup */ |
| 128 | + $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]); |
| 129 | + |
| 130 | + $quoteSetup->addAttribute('quote_address', $attributeCode, ['type' => $type, 'length' => $length]); |
| 131 | + $salesSetup->addAttribute('order_address', $attributeCode, ['type' => $type, 'length' => $length]); |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + private function addAttributeInAddressTemplates() |
| 136 | + { |
| 137 | + $this->config->saveConfig( |
| 138 | + 'customer/address_templates/text', |
| 139 | + <<<'TEMPLATE' |
| 140 | +{{depend salutation}}{{var salutation}} {{/depend}}{{var firstname}} {{var lastname}} |
| 141 | +{{depend company}}{{var company}}{{/depend}} |
| 142 | +{{depend company_addition}}{{var company_addition}}{{/depend}} |
| 143 | +{{if street1}}{{var street1}} |
| 144 | +{{/if}} |
| 145 | +{{depend street2}}{{var street2}}{{/depend}} |
| 146 | +{{depend street3}}{{var street3}}{{/depend}} |
| 147 | +{{depend street4}}{{var street4}}{{/depend}} |
| 148 | +{{if postcode}}{{var postcode}} {{/if}}{{if city}}{{var city}}{{/if}}{{if region}}, {{var region}}{{/if}} |
| 149 | +{{var country}} |
| 150 | +Tel: {{var telephone_prefix}} {{var telephone}} |
| 151 | +{{depend ${fieldname}}}{{var ${fieldname}}}{{/depend}} |
| 152 | +TEMPLATE |
| 153 | + , |
| 154 | + 'default', |
| 155 | + 0 |
| 156 | + ); |
| 157 | + $this->config->saveConfig( |
| 158 | + 'customer/address_templates/html', |
| 159 | + <<<'TEMPLATE' |
| 160 | +{{depend salutation}}{{var salutation}} {{/depend}}{{var firstname}} {{var lastname}}<br/> |
| 161 | +{{depend company}}{{var company}}<br />{{/depend}} |
| 162 | +{{depend company_addition}}{{var company_addition}}<br />{{/depend}} |
| 163 | +{{if street1}}{{var street1}}<br />{{/if}} |
| 164 | +{{depend street2}}{{var street2}}<br />{{/depend}} |
| 165 | +{{depend street3}}{{var street3}}<br />{{/depend}} |
| 166 | +{{depend street4}}{{var street4}}<br />{{/depend}} |
| 167 | +{{if postcode}}{{var postcode}} {{/if}}{{if city}}{{var city}}{{/if}}{{if region}}, {{var region}}{{/if}}<br/> |
| 168 | +{{var country}}<br/> |
| 169 | +Tel: {{var telephone_prefix}} {{var telephone}} |
| 170 | +{{depend ${fieldname}}}<br/>{{var ${fieldname}}}{{/depend}} |
| 171 | +TEMPLATE |
| 172 | + , |
| 173 | + 'default', |
| 174 | + 0 |
| 175 | + ); |
| 176 | + //@codingStandardsIgnoreStart |
| 177 | + $this->config->saveConfig( |
| 178 | + 'customer/address_templates/oneline', |
| 179 | + <<<'TEMPLATE' |
| 180 | +{{depend salutation}}{{var salutation}} {{/depend}}{{var firstname}} {{var lastname}}{{depend company}}, {{var company}}<br />{{/depend}}{{depend company_addition}}, {{var company_addition}}{{/depend}}{{if street1}}, {{var street1}}{{/if}}{{depend street2}}, {{var street2}}{{/depend}}{{depend street3}}, {{var street3}}{{/depend}}{{depend street4}}, {{var street4}}{{/depend}}, {{if postcode}}{{var postcode}} {{/if}}{{if city}}{{var city}}{{/if}}{{if region}}, {{var region}}{{/if}}, {{var country}} |
| 181 | +TEMPLATE |
| 182 | + , |
| 183 | + 'default', |
| 184 | + 0 |
| 185 | + ); |
| 186 | + //@codingStandardsIgnoreEnd |
| 187 | + $this->config->saveConfig( |
| 188 | + 'customer/address_templates/pdf', |
| 189 | + <<<'TEMPLATE' |
| 190 | +{{depend salutation}}{{var salutation}} {{/depend}}{{var firstname}} {{var lastname}}| |
| 191 | +{{depend company}}{{var company}}|{{/depend}} |
| 192 | +{{depend company_addition}}{{var company_addition}}|{{/depend}} |
| 193 | +{{if street1}}{{var street1}}|{{/if}} |
| 194 | +{{depend street2}}{{var street2}}|{{/depend}} |
| 195 | +{{depend street3}}{{var street3}}|{{/depend}} |
| 196 | +{{depend street4}}{{var street4}}|{{/depend}} |
| 197 | +{{if postcode}}{{var postcode}} {{/if}}{{if city}}{{var city}}{{/if}}{{if region}}, {{var region}}{{/if}}| |
| 198 | +{{var country}}| |
| 199 | +Tel: {{var telephone_prefix}} {{var telephone}} |
| 200 | +{{depend ${fieldname}}}{{var ${fieldname}}}{{/depend}} |
| 201 | +TEMPLATE |
| 202 | + , |
| 203 | + 'default', |
| 204 | + 0 |
| 205 | + ); |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments