Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddTaxAttributeAndTaxClasses.php
Go to the documentation of this file.
1 <?php
8 
14 use Magento\Tax\Setup\TaxSetupFactory;
15 
21 {
25  private $taxSetupFactory;
26 
30  private $directoryRegionFactory;
31 
35  private $moduleDataSetup;
36 
43  public function __construct(
44  TaxSetupFactory $taxSetupFactory,
45  RegionFactory $directoryRegionFactory,
46  \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
47  ) {
48  $this->taxSetupFactory = $taxSetupFactory;
49  $this->directoryRegionFactory = $directoryRegionFactory;
50  $this->moduleDataSetup = $moduleDataSetup;
51  }
52 
56  public function apply()
57  {
59  $taxSetup = $this->taxSetupFactory->create(['resourceName' => 'tax_setup', 'setup' => $this->moduleDataSetup]);
60 
64  $taxSetup->addAttribute(
65  \Magento\Catalog\Model\Product::ENTITY,
66  'tax_class_id',
67  [
68  'group' => 'Product Details',
69  'sort_order' => 40,
70  'type' => 'int',
71  'backend' => '',
72  'frontend' => '',
73  'label' => 'Tax Class',
74  'input' => 'select',
75  'class' => '',
76  'source' => \Magento\Tax\Model\TaxClass\Source\Product::class,
77  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE,
78  'visible' => true,
79  'required' => false,
80  'user_defined' => false,
81  'default' => '2',
82  'searchable' => true,
83  'filterable' => false,
84  'comparable' => false,
85  'visible_on_front' => false,
86  'visible_in_advanced_search' => false,
87  'used_in_product_listing' => true,
88  'unique' => false,
89  'apply_to' => implode(',', $taxSetup->getTaxableItems()),
90  'is_used_in_grid' => true,
91  'is_visible_in_grid' => false,
92  'is_filterable_in_grid' => true,
93  ]
94  );
98  $data = [
99  [
100  'class_id' => 2,
101  'class_name' => 'Taxable Goods',
103  ],
104  [
105  'class_id' => 3,
106  'class_name' => 'Retail Customer',
108  ],
109  ];
110  foreach ($data as $row) {
111  $this->moduleDataSetup->getConnection()->insertForce(
112  $this->moduleDataSetup->getTable('tax_class'),
113  $row
114  );
115  }
120  $region = $this->directoryRegionFactory->create();
121  $data = [
122  [
123  'tax_calculation_rate_id' => 1,
124  'tax_country_id' => 'US',
125  'tax_region_id' => $region->loadByCode('CA', 'US')->getRegionId(),
126  'tax_postcode' => '*',
127  'code' => 'US-CA-*-Rate 1',
128  'rate' => '8.2500',
129  ],
130  [
131  'tax_calculation_rate_id' => 2,
132  'tax_country_id' => 'US',
133  'tax_region_id' => $region->loadByCode('NY', 'US')->getRegionId(),
134  'tax_postcode' => '*',
135  'code' => 'US-NY-*-Rate 1',
136  'rate' => '8.3750'
137  ],
138  ];
139  foreach ($data as $row) {
140  $this->moduleDataSetup->getConnection()->insertForce(
141  $this->moduleDataSetup->getTable('tax_calculation_rate'),
142  $row
143  );
144  }
145  }
146 
150  public static function getDependencies()
151  {
152  return [];
153  }
154 
158  public static function getVersion()
159  {
160  return '2.0.0';
161  }
162 
166  public function getAliases()
167  {
168  return [];
169  }
170 }
__construct(TaxSetupFactory $taxSetupFactory, RegionFactory $directoryRegionFactory, \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup)