Table of Contents for
Magento 2 - Build World-Class online stores

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Magento 2 - Build World-Class online stores by Jonathan Bownds Published by Packt Publishing, 2017
  1. Cover
  2. Table of Contents
  3. Magento 2 - Build World-Class online stores
  4. Magento 2 - Build World-Class online stores
  5. Credits
  6. Preface
  7. 1. Module 1
  8. 1. Magento Fundamentals
  9. XAMPP installation
  10. Magento
  11. Summary
  12. 2. Magento 2.0 Features
  13. An introduction to the Magento order management system
  14. Magento 2.0 command-line configuration
  15. The command-line utility
  16. Summary
  17. 3. Working with Search Engine Optimization
  18. Store configuration
  19. SEO and searching
  20. SEO catalog configuration
  21. Google Analytics tracking code
  22. Optimizing Magento pages
  23. Summary
  24. 4. Magento 2.0 Theme Development – the Developers' Holy Grail
  25. Magento 2.0 theme structure
  26. The Magento Luma theme
  27. Magento theme inheritance
  28. CMS blocks and pages
  29. Custom variables
  30. Creating a basic Magento 2.0 theme
  31. Summary
  32. 5. Creating a Responsive Magento 2.0 Theme
  33. Composer – the PHP dependency manager
  34. Building the CompStore theme
  35. CSS preprocessing with LESS
  36. Applying new CSS to the CompStore theme
  37. Creating the CompStore logo
  38. Applying the theme
  39. Creating CompStore content
  40. Customizing Magento 2.0 templates
  41. Summary
  42. 6. Write Magento 2.0 Extensions – a Great Place to Go
  43. Using the Zend framework
  44. Magento 2.0 extension structure
  45. Developing your first Magento extension
  46. The Twitter REST API
  47. The TweetsAbout module structure
  48. Using TwitterOAuth to authenticate our extension
  49. Developing the module
  50. Summary
  51. 7. Go Mobile with Magento 2.0!
  52. Adjusting the CompStore theme for mobile devices
  53. The Magento 2.0 responsive design
  54. The Magento UI
  55. Implementing a new CSS mixin media query
  56. Adjusting tweets about extensions for mobile devices
  57. Summary
  58. 8. Speeding up Your Magento 2.0
  59. Indexing and caching Magento
  60. Indexing and re-indexing data
  61. The Magento cron job
  62. Caching
  63. Fine-tuning the Magento hosting server
  64. Selecting the right Magento hosting service
  65. Apache web server deflation
  66. Enabling the expires header
  67. Minifying scripts
  68. Summary
  69. 9. Improving Your Magento Skills
  70. Magento knowledge center
  71. Improving your Magento skills
  72. Summary
  73. 2. Module 2
  74. 1. Magento 2 System Tools
  75. Installing Magento 2 sample data via GUI
  76. Installing Magento 2 sample data via the command line
  77. Managing Magento 2 indexes via the command line
  78. Managing Magento 2 cache via the command line
  79. Managing Magento 2 backup via the command line
  80. Managing Magento 2 set mode (MAGE_MODE)
  81. Transferring your Magento 1 database to Magento 2
  82. 2. Enabling Performance in Magento 2
  83. Configuring Redis for backend cache
  84. Configuring Memcached for session caching
  85. Configuring Varnish as the Full Page Cache
  86. Configuring Magento 2 with CloudFlare
  87. Configuring optimized images in Magento 2
  88. Configuring Magento 2 with HTTP/2
  89. Configuring Magento 2 performance testing
  90. 3. Creating Catalogs and Categories
  91. Create a Root Catalog
  92. Create subcategories
  93. Manage attribute sets
  94. Create products
  95. Manage products in a catalog grid
  96. 4. Managing Your Store
  97. Creating shipping and tax rules
  98. Managing customer groups
  99. Configuring inventories
  100. Configuring currency rates
  101. Managing advanced pricing
  102. 5. Creating Magento 2 Extensions – the Basics
  103. Initializing extension basics
  104. Working with database models
  105. Creating tables using setup scripts
  106. Creating a web route and controller to display data
  107. Creating system configuration fields
  108. Creating a backend data grid
  109. Creating a backend form to add/edit data
  110. 6. Creating Magento 2 Extensions – Advanced
  111. Using dependency injection to pass classes to your own class
  112. Modifying functions with the use of plugins – Interception
  113. Creating your own XML module configuration file
  114. Creating your own product type
  115. Working with service layers/contracts
  116. Creating a Magento CLI command option
  117. 3. Module 3
  118. 1. Planning for Magento
  119. Technical considerations
  120. Global-Website-Store methodology
  121. Planning for multiple stores
  122. Summary
  123. 2. Managing Products
  124. Managing products the customer focused way
  125. Creating products
  126. Managing inventory
  127. Pricing tools
  128. Autosettings
  129. Related products, up-sells, and cross-sells
  130. Importing products
  131. Summary
  132. 3. Designs and Themes
  133. The concept of theme inheritance
  134. Default installation of design packages and themes
  135. Installing third-party themes
  136. Inline translations
  137. Working with theme variants
  138. Customizing themes
  139. Customizing layouts
  140. Summary
  141. 4. Configuring to Sell
  142. Payment methods
  143. Shipping methods
  144. Managing taxes
  145. Transactional e-mails
  146. Summary
  147. 5. Managing Non-Product Content
  148. Summary
  149. 6. Marketing Tools
  150. Promotions
  151. Newsletters
  152. Using sitemaps
  153. Optimizing for search engines
  154. Summary
  155. 7. Extending Magento
  156. The new Magento module architecture
  157. Extending Magento functionality with Magento plugins
  158. Building your own extensions
  159. Summary
  160. 8. Optimizing Magento
  161. Indexing and caching
  162. Caching in Magento 2 – not just FPC
  163. Tuning your server for speed
  164. Summary
  165. 9. Advanced Techniques
  166. Version control
  167. Magento cron
  168. Backing up your database
  169. Upgrading Magento
  170. Summary
  171. 10. Pre-Launch Checklist
  172. System configurations
  173. Design configurations
  174. Search engine optimization
  175. Sales configurations
  176. Product configurations
  177. Maintenance configurations
  178. Summary
  179. Index

Managing taxes

The great unavoidable factor: taxes. If you're a retailer — and even as a wholesaler in some jurisdictions — you will need to master the management of tax rates and rules in your Magento store. We've had to deal with this issue on many other platforms, and while some do provide some cool features, none, in our opinion, offer as much flexibility for taxes as Magento; especially when it comes to VAT taxes.

Note

You'll appreciate as you go through this section, that taxes can be quite complicated. Before configuring taxes in your online store, you should consult your tax professional. Making errors in taxes can not only present legal issues for your business, but also erode consumer confidence if the customers feel they're being inappropriately taxed on purchases.

How Magento manages taxes

Taxes are applied to products based on assigned tax classes. Tax classes are combined with tax zones and rates to create tax rules. Tax rules are what are applied to each product or shopping cart to determine the amount of tax charged in a transaction.

Here's a flow chart on how Magento calculates sales taxes based on a tax rule:

How Magento manages taxes

There are two types of tax classes in Magento:

  • Product tax class: A product is usually considered taxable or non-taxable. If a product is not to be taxed, a value of None is selected for its tax class. You may find it necessary to have different product tax classes if you have different taxing rules for different products.
  • Customer tax class: As we saw in creating customer groups, customers are assigned to a tax class, usually based on whether they are retail or wholesale customers.

Creating tax rules

Tax rules are created based on jurisdiction and rate: creating a zone for a country, state, and/or zip code, and a percentage used to calculate the tax.

To illustrate, let's review a tax rule included in the default installation for Magento 2:

  1. Go to Stores | Tax Rules in your Magento backend.
  2. Click on Rule 1 listed in the tax rules table.
    Creating tax rules

Let's now take a look at the layout of a tax rule:

Creating tax rules
  • Name: While Rule 1 is fine, it's not really descriptive, particularly in a list of rules. You can name the rule whatever you feel works best for you.
  • Tax Rate: This field allows you to select one or more rates you want to apply to the rule. You might have to apply several tax rates if you have multiple distribution locations or nexuses. The rate is determined by the location of the buyer, usually their Ship To address.

    Note

    A nexus is generally considered if you have an active business in a particular state or locality. For instance, if your office is in Texas and you ship your products from a warehouse in Illinois, then, for taxing purposes, you're considered to have a nexus in Texas and Illinois. Therefore, as it stands at this moment, you are required to collect sales tax on any sales to buyers in Texas and Illinois. However, there are efforts in the United States Congress to radically change taxing laws on online sales transactions. Some state legislatures are also grappling with this issue. We know you're used to hearing this, but you do need to consult a tax professional to make sure you're correctly charging sales tax.

    A leader in e-commerce sale tax is TaxJar (https://www.taxjar.com), a company that can provide integration with your store to precisely calculate required sales taxes and help with reporting to the various taxing jurisdictions. Using a tool such as TaxJar could greatly reduce your tax configuration time, while providing greater sales tax accuracy.

You can add or edit tax rates here or under Stores | Tax Zones and Rates. Let's edit the California tax rate here to understand the process:

Creating tax rules

To the right of the US-CA-*-Rate 1 item name, when you hover your mouse over the item, you will see a pencil icon. Click this to reveal the tax rate modal dialogue.

Creating tax rules

Let's have a look at the various fields here:

  • Tax Identifier: You can enter whatever you wish in this space. The naming scheme shown was created by Magento in setting up this default rate.
  • Zip/Post is Range: By checking this box, the Zip/Post Code field is replaced by range fields, into which you can enter a starting and ending code range. This is particularly useful if you need to apply a different rate to a range of zip codes within a larger region.
    Creating tax rules
  • Zip/Post Code: As indicated, an asterisk is considered a wild card. That is, any value will match. If you want to, for example, apply the rate to all zip codes beginning in 78 (for example, 78001, 78002, and so on), you could enter 78*.
  • State: Select the state for which the rate applies. The selections will change based on your country selection. An asterisk in this selection will apply the rate to all states and regions within the country.
  • Country: Select the country for which the rate applies.
  • Rate Percent: Enter the rate to be applied as a percentage amount. In other words, if the rate is 8.25%, enter 8.25, not 0.0825.
  • Tax Titles: As in so many places in Magento, you can specifically name this item as you would like it to appear in your various store views. You might, for instance, wish to call the rule CA Sales Tax for your English views, but CA La Taxe De Vente for your French store view. If you leave any of these blank, your customer will be shown the Tax Identifier value.

You can use the same field specifics for creating a new tax rate by clicking Add New Tax Rate at the bottom of the Tax Rate list field.

To view the remaining fields, expand the Additional Settings section.

  • Customer Tax Class: As discussed earlier, Customer Tax Classes allow you to assign customer groups to different tax groups. For instance, you will probably have sales taxes only apply to retail customers.

    Tip

    Wholesale Customer Tax Class

    In Magento 2, there is no one place to go to create Customer Tax Classes. Therefore, you should create a Wholesale Customer Tax class in any tax rule so that it will be available to you when managing customer groups.

    Creating tax rules
  • Product Tax Class: If you need additional Product Tax Classes, you can also add them here and they will be available for selection in product edit panels.
  • Priority: If you have more than one tax rate that might apply to a customer's shopping cart, those with the same priority will be added. That is, each tax rule will be calculated separately, and then added together. If the priorities are different, then the rates will be compounded in order of priority. Let's take an example: you have two taxes that will apply to a product — a federal excise tax of 5% and a state sales tax of 8%. If the priorities for both are the same, the shopper's purchase of $100 will be taxed a total of $13: $5 for 5% and $8 for 8%. If you put a priority of 1 for the excise tax and a priority of 2, for instance, for the state sales tax, then the customer is taxed $13.40: $5 for the excise tax and $8.40 for the 8% of $105 ($100 plus the $5 excise tax). The latter is an example of compounded sales tax. Taxes with different priorities will also be listed as separate tax line items to your customers.
  • Calculated Off Subtotal Only: Now, take what we just said about priority and consider this: if you want each applicable tax — such as GST and PST taxes in Canada — to display separately to the customer, each tax must have a different priority. However, you don't want these taxes compounding, as explained above. Therefore, to prevent compounding, yet have the tax shown as a separate line item, check this box and the tax will only be applied to the order subtotal, not as a compounded tax calculation.
  • Sort Order: If you have more than one applied tax, you can control the order in which they are listed to your customers. This will not change any compounding based on priority.

Importing tax rates

While you can't import tax rules, you can import tax rates, which may be a time saver, particularly where you have multiple taxing jurisdictions to whom you have to report tax collections.

As with many importing capabilities in Magento 2, the easiest way to begin is by exporting the current tax rules and expanding on the CSV file, then importing your changes. To import tax rates, view any tax rule. At the bottom of the screen are buttons to import and export tax rates. Export the current tax rate file, add or edit your rates, then re-import.

Value added tax configurations

In some countries, goods and services are taxed in a means similar to sales tax, but calculated and managed differently. These Value-Added Taxes (VAT) are made even more complex due to varying rates among countries, different rules for registration, and rules for taxation based on the type of product or service sold.

Note

As usual, we can't begin to counsel you on taxes. VAT rules can be complicated, especially for non-EU countries selling into EU countries. We highly suggest you consult with your tax professionals. If you doubt the complexity of EU VAT taxes, see http://goo.gl/y07Pb.

The VAT process in Magento involves three basic components — the needs of them are based on your location:

  • VAT validation: For customers who provide a VAT ID, Magento 2 is able to query the European Commission to verify their VAT ID
  • VAT tax rules: As with sales taxes, the creation of tax rules based on certain conditions or considerations
  • System configuration: Activate the rules that will manage VAT-eligible purchases

In Magento, VAT is charged if both the seller and customer are located in the same EU country. If both are EU-registered businesses, no VAT tax is collected if the seller and customer are in different countries.

When selling to consumers in EU countries, the amount of VAT collected is based on what country the seller is located (if the seller is an EU country). These are called intra-EU sales.

One exception (there's always one, yes?) is that when selling digital goods (for example, music, software), the VAT rate to be charged is that of the destination country, not the source country.

The key to effectively managing this complexity is the creation of multiple customer groups that can be automatically assigned during the checkout or registration process based on the VAT ID validation of the buyer.

Setup VAT taxes

It would be so easy for us to stop here, call it a day, and leave VAT tax configuration to your imagination. After all, we can play the "too complex for a book" card, correct?

The truth is that VAT taxes can be quite complex. If you're an EU business or exporting to the EU countries, you already understand the complexities. However, we do want you to get the most from Magento 2 and demonstrate its incredible ability to fulfill your tax calculation needs.

To that end — and perhaps the best way to demonstrate the process — we're going to set up VAT taxes as if we were a business based in France (we love Paris!).

Since we're considered, in this example, as an EU member country, we will need to provide for VAT ID validation of our customers and classify them accordingly.

Earlier in this chapter, we added four additional customer groups, specifically for VAT tax use. If we look under Stores | Customer Groups, based on our example Magento install, we should see the following:

Setup VAT taxes

Next, we want to create the product classes that we will need for applying the appropriate French VAT tax rates. France groups products into four rate categories, each of which should have a different product class added for taxing purposes:

Note

All tax rates and information shown here are for illustrative purposes only and should not be used without validation.

  • Standard VAT: These will be taxed at 20%.
  • Reduced VAT 1: Applies to books, transportation, entertainment events and hotels. Taxed at 10%.
  • Reduced VAT 2: Applies to medical, food and book products. Taxed at 5.5%.
  • Reduced VAT 3: Applies to newspaper and pharmaceuticals. Taxed at 2.1%.

We now have to consider that for customers living in other EU countries, we will charge our VAT tax rate on purchases of physical products, but charge the buyer's VAT tax rate for digital or virtual purchases.

That means that if we are selling digital products (for example, music, software, and so on) to other EU customers, we have to charge VAT tax at the rate in their country, not France. In order to do that, we have to add tax rates in Magento for every other EU country. As you can imagine, setting up all those tax rates and rules can be quite time consuming.

We have created a CSV file of all EU countries with their standard VAT tax rates (as of the time of writing). You can use this to upload the tax rates to your Magento 2 install.

For France, we need to create the four tax rates described above. After adding these under Stores | Tax Zones and Rates in our Magento 2 backend, the list of tax rates looks like this:

Setup VAT taxes

Our next chore is to create the product tax classes that are needed to separate our products according to the taxes we have to apply. For our France-based example, we will need five product tax classes:

  • Standard: These are physical products and services that don't fall into the other product classes
  • Reduced Tax Class 1: In France, these include passenger transportation, events (sports and entertainment), hotels, and restaurants
  • Reduced Tax Class 2: Medical, food, and books
  • Reduced Tax Class 3: Newspapers and pharmaceuticals
  • Virtual Products: Music, software, and other non-physical products

While you may not currently sell products that fall into all of these classes, you may want to go ahead and set them up so they're available to you.

Remember, to create additional product tax classes; just click to edit any tax rule, expand Additional Settings, and add your new classes. After adding the above classes, this section will look like the following screenshot:

Setup VAT taxes

With the preceding preparation completed, we can now create the tax rules that will be used to calculate VAT taxes on purchases. We need to create the following tax rules to meet our needs as a French online business:

  • Domestic customers purchasing standard products
  • Domestic customers purchasing Reduced Tax Class 1 products
  • Domestic customers purchasing Reduced Tax Class 2 products
  • Domestic customers purchasing Reduced Tax Class 3 products
  • All customers purchasing virtual products
  • Intra-EU customers purchasing non-virtual products

Once entered into our Magento 2 example, our Tax Rules screen would include the following:

Setup VAT taxes

Note

Remember, we are NOT tax experts. Your tax rules may differ from the example shown. The purpose of this exercise is to demonstrate the various steps needed to add VAT tax calculations to a Magento 2 store.

The final piece is to configure your Customer Configuration so that customers are automatically assigned to the proper customer group based on their VAT ID validation. To do that, go to Stores | Configuration | Customers | Customer Configuration and expand the Create New Account Options panel. Some of these settings are done at the global, website, and store view levels, so pay close attention to your store view scope setting in the upper right-hand part of the screen. For our example, we only want to automatically assign customers who visit our French store. Therefore, we will only enable this feature at the Sportswear French View.

Once you have changed to the appropriate store view level, the key fields to configure are:

  • Enable Automatic Assignment to Customer Group: Set to Yes and Magento will reveal additional configuration fields.
  • Tax Calculation Based On: Usually, you will set this to Shipping Address so that taxes are based on the customer's taxing jurisdiction, although there are exceptions.
  • Default Group: At the store view level, you may want to set this to Domestic.
  • Group for Valid VAT ID – Domestic: Set to your domestic group, Domestic.
  • Group for Valid VAT ID – Intra-Union: Set to Intra-EU.
  • Group of Invalid VAT ID: Set to Invalid VAT ID.
  • Validation Error Group. Set to Validation Error as the customer group.
  • Validate on Each Transaction: If you're going to calculate VAT taxes, then you would most like set this to Yes.
  • Default Value for Disable Automatic Group Changes Based on VAT ID: This feature allows Magento to re-assign customers if their VAT ID or address changes. If you do not want this feature, set this to Yes.

As with any store configuration, especially one as complex as VAT taxes, we highly recommend that you test your configurations thoroughly.