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

Promotions

Every retailer with whom we work finds it beneficial to offer promotions from time to time. From free shipping to coupons, promotions can help consumers choose your products over competitors, particularly if they feel they're getting a great bargain in the process.

As with sales tax, promotions are rule-based, meaning that the application and calculation of a promotion are based on rules you create. In Magento, there are two types of promotion rules:

  • Catalog price rules apply to any product that meets certain criteria. The discount is applied automatically.
  • Cart price rules, on the other hand, can be set to be only applied when a valid coupon is entered by the customer during checkout. Shopping cart rules can also be applied automatically. Furthermore, shopping cart rules — or coupons — can be publicized by adding them to an RSS feed for your site.

Creating a catalog price rule

As described, a catalog price rule is a discount that is applied to selected products automatically. For example, let's assume you wish to provide a 5% discount on all women's apparel priced over $50 sold between November 24, 2016 through November 26, 2016, as a special Black Friday promotion.

Tip

Black Friday and Cyber Monday

For years in the United States, the day after Thanksgiving — the last Friday of November, Black Friday — has been one of the busiest shopping days of the Christmas season. Retailers offer huge discounts and shoppers begin lining up well before dawn just to be the first one in the door. To counter the brick and mortar retailers, online retailers created Cyber Monday, the Monday following Black Friday, likewise offering huge discounts and special offers. Personally, we like the sound of Cyber Monday over the morbid moniker of Black Friday. We also like e-commerce!

To create a catalog price rule, you need to perform the following steps:

  1. Go to Marketing | Catalog Price Rules.
  2. Click on Add New Rule.
  3. Enter the following in the Rule Information screen:
    Creating a catalog price rule
  4. Click on Conditions in the left tab menu. Now, here's where it gets interesting!
  5. Click the green plus sign to add a condition to your rule.

    Note

    Any underlined items in these screens can be clicked to reveal additional choices. Don't hesitate to click and discover!

  6. In the drop-down menu that appears, choose Category.
  7. Click on is and select is one of.
  8. Click the ellipsis ().
  9. Click the small, square icon to the right to reveal a hierarchy of categories. Select the categories you wish to apply the discount to. Your selections will add the category IDs into the empty field.
    Creating a catalog price rule
  10. Click on the green checkmark icon to save your category selections.
  11. Click on the green plus sign again and choose Price from the drop-down menu.

    Note

    If you don't see Price in the drop down of choices, you may need to change the Price product attribute. Find Price under Stores | Product. In the Storefront Properties panel, change User for Promo Rule Conditions to Yes and click on Save Attribute.

  12. Click on is. Select greater than from the drop-down menu.
  13. Click the following ellipsis. Enter 50.00 in the field and click outside the field.
    Creating a catalog price rule

    Note

    The amount of the discount is based on the default currency for the website. If your site's default currency is Euros, then the discount would be 500 Euros.

Now, we need to create the discount calculation:

  1. Click on the Actions menu tab.
  2. Since we're going to apply a percentage discount to the price of the product, we can leave Apply as is.

    Note

    The four choices for Apply give you tremendous flexibility but may not be clearly understood.

  3. By Percentage of the Original Price reduces the price of the product by the percentage specified, such as 15%off the price.
  4. By Fixed Amount will reduce the product's price by a specified amount, such as 50 dollars off.
  5. To Percentage of the Original Price, by contrast to By Percentage of the Original Price, sets the price at the percentage you enter. For example, if you enter 75, the price of the product would be 75% of its original price.
  6. Likewise, To Fixed Amount, sets the price of the product at the price you enter instead of reducing the price by a fixed amount.
  7. In Discount Amount, enter 5.
  8. If you want the discounts to apply to not only a complex product type (for example, configurable, bundle, or group) but to their associated products as well, set Subproduct discounts to Yes and configure the amount of discount to apply to any subproducts.
  9. If we do not want to apply any other promotion rules to these applicable products, choose Yes for Discard subsequent rules. For example, you may not want a customer to receive multiple discounts on the same product.
  10. Click on Save Rule.

You now have a new rule that will automatically take effect at 12:01 AM on Friday, November 24th and run until 11:59 PM on Sunday, November 26th. However, there is one final action to take!

Before your new rule can take effect, click on Apply Rules at the top of the Catalog Price Rules page. Magento needs to set itself to apply the rules to the applicable products.

Note

Please note that immediate price changes (promotions without a set start date) will be visible when Magento re-indexes its prices, usually within a few minutes.

Creating a catalog price rule

Creating cart price rules

Magento has some very powerful and flexible tools for creating discounts that can be applied once the customer has reached the shopping cart part of their visit. In fact, we have found it difficult to identify a single discount scenario we could not accommodate in Magento.

The key features of cart price rules for which you should be aware are:

  • You can create rules for any or all of your websites. Rules apply to all store views within a website.
  • You can allow discounts for only selected customer groups. You could offer coupons to wholesalers, for instance, that could not be used for retail customers.
  • You do not have to use a coupon code. Your rules can be applied automatically as long as the shopping cart meets the required criteria.
  • You can limit the number of times a coupon is used. Or you can allow a coupon code to be used multiple times by a given customer.
  • Product attributes can be used as criteria. As noted in Chapter 3, Managing Products, a product attribute can be configured so that you can apply a discount based on the value of that attribute within one or more products in the customer's shopping cart.
  • You can apply the discount to the entire cart or to only select products in the cart. For instance, if you are giving a 20% discount to shirts only, but customers put other types of products in their cart, you can configure the discount so that it deducts 20% from the price of the shirts in the cart and not from any other product.
  • Magento can generate coupon codes. Some e-commerce systems require that you contrive the coupon codes you wish to use. In Magento, you have the ability to generate and manage as many codes as you need.

Tip

We have worked with clients that have had millions of generated coupon codes. While this many codes may require more horsepower for your server, Magento has no problem storing and using a very large number of codes.

Let's use a specific example to learn how to create a cart price rule:

  • Retail customers only. We'll assume we have 100 customers on our newsletter list.
  • Unique coupon codes for each user (we'll email them to existing customers).
  • Customer must buy $100 or more of women's tops or three or more of any women's apparel items.
  • Discount gives 20% off on women's apparel.
  • Discount also provides free ground shipping.
  • Coupon can only be used once per customer.
  • Coupon is only active starting on April 1, 2016 and expiring on August 31, 2016.
  • Coupon cannot be combined with any other discounts.

The process of building our rule will follow these steps:

  1. Adding the new rule.
  2. Defining the rule's conditions.
  3. Defining the rule's actions.
  4. Modifying the rule's labels.
  5. Generating coupon codes (if needed).
  6. Testing the rule.

Adding the new rule

To begin building our cart price rule, go to Marketing | Cart Price Rules in your Magento 2 backend. Click on Add New Rule.

The Rule Information panel for our new rule has several fields to use to set up the rule:

  • Rule Name: Enter Free Shipping & 20% off Women's Apparel as the name of your rule. This will be shown in your list of rules.
    Adding the new rule
  • Description: You can use this field to fully describe the rule. In our example, we will enter the coupon specifics we outlined for this example.
    Adding the new rule
  • Status: Select Active when you wish for the coupon to be available for use.
  • Websites: Select Sportswear Website, since our example rule only applies to products purchased in that store. You can select any number (or all) of the websites shown. Our selection also means that visitors to all three sportswear store views (English, French, and German) can use the code.
  • Customer Groups: Since we only want our coupon to be used for retail customers, we would select NOT LOGGED IN and General. If we only want logged in retail customers, we would only select General.
    Adding the new rule
  • Coupon: We will be using a coupon code; therefore, select Specific Coupon.
  • Coupon Code: Our goal is to create 100 unique codes to distribute. This may help us track code usage by customer, and it will allow us to restrict the usage of the code to only one use, since we will be distributing to customers that are not logged in. Instead of entering a specific coupon code, check the box labeled Use Auto Generation. (We will be managing the generation of codes in a moment.)
  • Uses per Coupon: Enter 1, as we want to only allow a customer to use it once before expiring.
  • Uses per Customer: This value only applies to logged in customers. We will also enter 1 in this field.

    Note

    Entering zero (0) in either of the Uses fields allows unlimited use of a coupon.

  • From/To: These are the first and last dates the coupon can be used. If you leave FROM blank, the coupon will be immediately available (if "Active"); if TO is blank, the coupon code will never expire. Using the calendar pop-up (click the small icon to the right), choose April 1, 2016 for From and August 31, 2016 for To.
    Adding the new rule
  • Priority: If you have more than one active rule, they will be applied in an order based on this field. The order is numeric, descending, with 1 being the highest priority.
  • Public In RSS Feed: If you want to publicize your discount in your store's RSS field, set this option to Yes. Since ours requires a specific coupon code and we wish to limit to known newsletter subscribers, we will select No for this field.

Tip

As with all multi-panel screens in Magento, it's probably a good idea to click Save and Continue Edit after you complete each panel. Just in case!

Defining the rule's conditions

A rule's conditions can be considered as minimum requirements. In other words, what is necessary about a customer's shopping cart for the rule to be valid?

In our example, a coupon code can only be used if the shopping cart contains $100 or more of women's tops or 3 or more women's apparel items. To create this condition, go to the Conditions panel.

Note

If we leave this panel blank (for example, do not add any conditions), then the coupon code will be valid for any products present in the shopping cart and for any amount.

Since we have two conditions, both of which are valid, click the ALL in In ALL of these conditions are TRUE. It will allow you to select ANY. These creates an "or" condition.

Note

Pay attention if you use nested conditions, as the ALL/ANY applies to only the top level conditions. You can use condition combinations to create nested and/or conditions.

Our first condition will test whether the customer has $100 or more of women's tops in their shopping cart:

  1. Click the green + icon to create your first condition.
  2. In the dropdown, choose Products subselection. We are creating a rule that will apply to only a particular selection of qualifying products.
  3. Click on the words total quantity is and select total amount.
  4. Click the word is and select equals or greater than.
  5. Click the following ellipsis () and enter 100. Hit your Enter key.
  6. Change ALL to ANY in the condition description.
  7. Click the green + icon nested within this condition.
  8. Select Category from the dropdown menu.
  9. Click is and select is one of from the dropdown menu.
  10. Click the following ellipsis (), and by either using the list icon or by typing the category IDs of the categories you wish to use, choose the Tops category and all sub-categories within, as shown in the following screenshot:
    Defining the rule's conditions

    Tip

    It's a good idea to select all subcategories as well, since you may have items that are only assigned to a sub-category and not to the top level category.

  11. Once you have selected your categories, click the green checkmark icon to save your choices.

Next, we need to add the or condition that tests whether the customer has three or more women's apparel items into their cart:

  1. Click the left-most green + icon.
  2. Select Products subselection in the resulting dropdown.
  3. Select equals or greater than for the comparison value.
  4. Enter 3 for the amount.
  5. Change ALL to ANY in the condition description.
  6. Click the nested green + icon.
  7. Select Category from the dropdown.
  8. Click is and select is one of from the dropdown choices.
  9. Click the ellipsis () and select all categories within the women's apparel section, as you can see in the following image.
    Defining the rule's conditions
  10. Click Save and Continue Edit to save your work to this point.

Once your conditions are set, your Conditions panel should look like the following image.

Defining the rule's conditions

Now we can move on to specifying the actions that will take place for shopping carts that match our conditions.

Defining the rule's actions

There are two parts to a rule's actions:

  • The amount discounted
  • For which products the discount is to be applied

This is important to understand, as many are confused by the fact that action conditions appear very similar to the rule conditions that we created before. The conditions in this panel dictate which products qualify for the discounts.

In other words — as in our case — we're using one set of conditions to qualify the shopping cart and another set, within the Actions panel, to identify to which products the discount will be applied. These conditions are different. If they were the same — for example, you're discounting a shopping cart containing $100 or more in products — then you do not have to add any conditions to the Actions panel.

Let's now configure our Actions panel for our needs:

  1. For Apply, select the default: Percent of product price discount.

    Let's go over the various choices here, so you have an idea as to which discount will work best with your purposes. Any value that is required will be entered into the Discount Amount field.

    • Percent of product price discount: Subtracts a percentage discount from the price of the applicable products.
    • Fixed amount discount: Subtracts a fixed amount from the price of the products.
    • Fixed amount discount for whole cart: The fixed amount entered here will be discounted from the entire cart total (you can specify whether the discount applies to shipping elsewhere on this panel).
    • Buy X Get Y Free (discount amount is Y): You can specify that if a customer buys X quantity of a product (entered into the Discount Qty Step (Buy X) field), they will receive Y quantity of the same product for free.
  2. Enter 20 into the Discount Amount field. You do not need to enter % for percentage discounts.
  3. Since we are not limiting the number of the same item to which the discount may be applied, we will set Maximum Qty Discount is Applied To as 0.
  4. The Discount Qty Step (Buy X) does not apply for our case (see above regarding the Buy X Get Y Free discount choice).
  5. Set Leave Apply to Shipping Amount as No.
  6. Set Discard subsequent rules to Yes. We do not want other offers to apply if this discount is used.

    Note

    If the priority of your rule is lower than another rule, the higher priority rule will still be applied. This setting only applies to lower priority rules.

  7. Set Free Shipping to For shipment with matching items, as our example discount also gives the customer free shipping for the entire qualifying order.

    Note

    For free shipping to work, you have to have at least one free shipping method configured. See Chapter 4, Configuring to Sell, for more information on shipping.

  8. Click on Save and Continue Edit to save your settings so far.

The fields mentioned above can be visualized in the following screenshot:

Defining the rule's actions

The next part of our actions configuration is to designate to which products the discount will be applied. In our case, any women's apparel (up to the three we set in the top part of the panel).

Using the same methodology we use in the Conditions panel, our Actions panel rules, when completed, will look like the following:

Defining the rule's actions

Modifying the rule's labels

While you could use the name of the rule as what displays to customers, it's probably more prudent to compose a discount label that more succinctly describes the discount. Furthermore, as in our example, we need different labels for each of the foreign language sites on which we sell sportswear.

For our example, we're going to label our discount for our customers as Save 20% & Free Shipping!

  1. In the Labels panel, enter Save 20% & Free Shipping! in Default Rule Label for All Store Views.
  2. Since we can use this same label for our English store view, we can leave Sportswear English View blank.
  3. For Sportswear French View, we might enter (and, again, our French and German are not polished at all!) Économisez 20% et livraison gratuite!
  4. In the Sportswear German View, we might use Sie sparen 20% & Kostenloser Versand!

And, of course, we click on Save and Continue Edit. Our Labels panel now looks like the following screenshot:

Modifying the rule's labels

Generating coupon codes

In our example, we want to create 100 unique coupon codes that we can send to our selected customers. Magento does an amazing job of helping you generate codes and also keeps track of how many times specific codes have been used.

To begin, go to the Manage Coupon Codes panel. Let's assume we want to create codes that are alphanumeric (both letters and numbers), 12 characters long, and have a prefix of ASW and a suffix of F20. Our coupon pattern would be ASW-XXX-XXX-F20, where "XXX" is a unique alphanumeric code. To configure this, we set up the parameters for the codes and then click on Generate to allow Magento to create the 100 unique codes we need:

  1. In Coupon Qty, enter 100.

    Note

    For testing purposes — and particularly if these are single-use coupon codes —you should generate a few more than you need. In this case, we might want to generate 110 or 120 codes, just so we can thoroughly test. See the next section about testing for more information.

  2. Our Code Length is 6 (we do not count the hyphens or the prefix and suffix, as we specify those a bit later).
  3. For Code Format, select Alphanumeric.
  4. In Code Prefix, enter ASW- (we're using this for Acme Sportswear).
  5. In Code Suffix, enter -F20 (for Free and 20% off).

    Note

    The use of a prefix and/or suffix is purely optional. If you're using unique coupon codes, you may want to use some designators that will help you identify that the code is a valid code for the order. When an order is viewed in your backend, it will show what codes, if any, were applied to the order. If you only use randomly generated codes without other designators, you might not be able to easily determine what actual discount rule was applied. If you want hyphens before or after a prefix/suffix, you need to include it in the appropriate fields.

  6. Since we want a hyphen inserted into our codes, we can enter 3 in Dash Every X Characters.

The fields mentioned above can be visualized in the following screenshot:

Generating coupon codes

Now, we can click on Generate to create our 100 unique codes. Once generated, you should see 100 records created in the lower portion of the panel. Although your codes will be different (they are randomly generated, after all), your list should look similar to ours.

Note

Customers may enter codes using either lower or upper case. asw-xqu-u7i-f20 will work just as well as ASW-XQU-U7I-F20 or AsW-xQu-U7I-f20.

Generating coupon codes

Using the export tool at the top of your list, you can now export your codes and use them with your email program to send these codes to your customers.

Note

Unfortunately, Magento 2 does not have a native means of sending your unique codes to your customers. You will have to use a third-party email solution or extension if you wish to send these codes to your customers.

Testing the rule

As with any configuration you make in Magento, you should test and re-test to make sure your settings are correct. Try various tests, too. In developer-ese, these are called use cases.

Note

If your coupon code is only valid at a future date, you should change the start date to TODAY, then change to the future date once you have completed testing.

The more you test, the more confident you'll feel that your rule is valid.