Magento 2 Experten — Hyvä Theme, Tailwind CSS & SEO aus einer Hand ›

Magento 2 Module Tutorial: Loyalty & Rewards from A to Z | Mironsoft

The Complete Magento 2 Module: Loyalty & Rewards from A to Z

One single, continuous loyalty-points-and-rewards program as the thread running through building every building block of a custom Magento 2 module: db_schema.xml, Model/ResourceModel/Repository, an EAV entity, custom attributes on product, category, customer, company, and order, observers and cron, plugins and preferences, controllers, widgets and Page Builder, custom payment and shipping methods, a custom product type, and REST/GraphQL APIs - 106 chapters in 12 blocks.

Das lernst du

  • How a custom Magento 2 module is built from the ground up: namespace, module.xml, declarative schema, model/resource model/collection, and the repository pattern instead of direct database access
  • How a custom EAV entity is structured after the pattern of catalog_product_entity, given custom attributes, and queried efficiently
  • How custom attributes are added to product, category, customer, company (B2B), and order, complete with source models, backend models, and the right scope
  • How observers, a custom cron group, plugins, and preferences work together - and when each of the four tools is the right one
  • How custom controllers, a custom router, blocks, view models, widgets, and Page Builder content types come together into a complete frontend experience
  • How custom payment and shipping methods, plus an entirely new product type, get integrated into checkout and order processing
  • How the same business logic gets exposed via REST, GraphQL, and customer section data, and how it's secured with unit and integration tests plus PHPStan

Voraussetzungen

  • Solid PHP knowledge and a basic understanding of Magento 2 module structure is assumed - the module skeleton itself is still briefly revisited in chapter 2 before moving on to the database table and the actual code

Kapitel dieser Serie

  1. 1 Project Overview: The Loyalty Points Program and an Architecture Overview of All 32 Areas ~9 Min. Lesezeit
  2. 2 Module Skeleton: registration.php, module.xml, and Namespace Conventions ~6 Min. Lesezeit
  3. 3 db_schema.xml: Designing the Points Ledger Table ~6 Min. Lesezeit
  4. 4 Writing the Model, Resource Model, and Collection for the Points Ledger ~7 Min. Lesezeit
  5. 5 The PointsCalculator Service: Central Business Logic as a Service Class, Not a Helper ~7 Min. Lesezeit
  6. 6 The Repository Pattern: PointsLedgerRepositoryInterface and Its Implementation ~8 Min. Lesezeit
  7. 7 System/Config/Setting: A Custom Configuration Page for the Loyalty Program ~7 Min. Lesezeit
  8. 8 A Custom Cache Type for the Reward Catalog: etc/cache.xml and Type::TYPE_IDENTIFIER ~6 Min. Lesezeit
  9. 9 Console Command: Manual Points Recalculation and Ledger Auditing ~7 Min. Lesezeit
  10. 10 What Is EAV? Why a Custom EAV Entity Makes Sense for Rewards ~6 Min. Lesezeit
  11. 11 Creating the Reward EAV Entity Tables (Entity + Attribute Tables) ~8 Min. Lesezeit
  12. 12 Writing the Model and Resource Model for an EAV Entity ~7 Min. Lesezeit
  13. 13 Defining Custom EAV Attributes for Rewards (Attribute Setup Script) ~8 Min. Lesezeit
  14. 14 EAV Attribute Types in Detail: varchar, int, decimal, text for Rewards ~6 Min. Lesezeit
  15. 15 Filtering the Reward Collection with EAV Joins Correctly ~7 Min. Lesezeit
  16. 16 Admin Grid for Rewards ~6 Min. Lesezeit
  17. 17 Validation and Required Fields for EAV Attributes ~6 Min. Lesezeit
  18. 18 EAV Performance Pitfalls and How to Avoid Them ~7 Min. Lesezeit
  19. 19 Product Attribute: Creating loyalty_points_multiplier on the Product ~7 Min. Lesezeit
  20. 20 Category Attribute: Bonus Multiplier per Category ~6 Min. Lesezeit
  21. 21 Customer Attribute: Points Balance and Loyalty Tier on the Customer ~8 Min. Lesezeit
  22. 22 Company Attribute: B2B Special Conditions (Company Attribute Basics) ~9 Min. Lesezeit
  23. 23 Sales Attribute: Storing Earned Points on Order and Order Item ~8 Min. Lesezeit
  24. 24 Attribute Source Models for Dropdown Lists ~7 Min. Lesezeit
  25. 25 Configuring Attribute Visibility and Scope Correctly (Website/Store/Global) ~7 Min. Lesezeit
  26. 26 Attribute Backend Models: Automatic Calculation on Save ~7 Min. Lesezeit
  27. 27 Cross-Attribute Consistency: When Category and Product Bonus Collide ~8 Min. Lesezeit
  28. 28 Attribute Block Summary: A Checklist for Your Own Attributes ~6 Min. Lesezeit
  29. 29 Understanding the Observer/Event System in Magento ~6 Min. Lesezeit
  30. 30 AwardPointsOnOrderPlaced: Crediting Points When an Order Is Placed ~9 Min. Lesezeit
  31. 31 Watching Refunds: Reversing Points on Credit Memo Creation ~8 Min. Lesezeit
  32. 32 Defining a Custom Cron Group: crontab.xml ~7 Min. Lesezeit
  33. 33 Cron Job: Point Expiry and Tier Recalculation ~8 Min. Lesezeit
  34. 34 Implementing Cron Job Error Handling and Logging Correctly ~7 Min. Lesezeit
  35. 35 Dispatching Your Own Events for Other Modules ~7 Min. Lesezeit
  36. 36 Controlling Observer Order and Priority ~6 Min. Lesezeit
  37. 37 When to Use an Observer vs. a Plugin: A Decision Guide ~7 Min. Lesezeit
  38. 38 Plugins (Interceptors) in Detail: before/after/around ~8 Min. Lesezeit
  39. 39 A Plugin on the Checkout Totals Collector: Applying a Points Discount ~9 Min. Lesezeit
  40. 40 Preference/Rewrite: When a Preference Is Needed Instead of a Plugin ~7 Min. Lesezeit
  41. 41 Writing a Custom Preference for the Points Calculation ~9 Min. Lesezeit
  42. 42 Preference vs. Plugin: Weighing Compatibility and Upgrade Risk ~7 Min. Lesezeit
  43. 43 Plugin Ordering (sortOrder) and Conflicts With Other Modules ~7 Min. Lesezeit
  44. 44 Helper Classes: When They Still Make Sense Despite the ViewModel Preference ~6 Min. Lesezeit
  45. 45 Custom Controller Structure: The Points History Page ~7 Min. Lesezeit
  46. 46 A Custom Router for Pretty URLs: /rewards/{slug} ~8 Min. Lesezeit
  47. 47 Block vs. View Model: When to Use Which ~6 Min. Lesezeit
  48. 48 A View Model for the Points Balance on the Account Page ~6 Min. Lesezeit
  49. 49 The Reward Catalog Page: List, Filters, and Detail View ~9 Min. Lesezeit
  50. 50 The Redeem Controller: Redeeming a Reward (POST Action, CSRF, Validation) ~9 Min. Lesezeit
  51. 51 Layout XML for the New Pages ~7 Min. Lesezeit
  52. 52 Adding a "My Points" Customer Account Menu Item ~6 Min. Lesezeit
  53. 53 Responsive Frontend Templates for Points Balance and History ~9 Min. Lesezeit
  54. 54 Accessibility and Best Practices in the New Frontend ~8 Min. Lesezeit
  55. 55 Widgets in Magento 2: Fundamentals and Purpose ~6 Min. Lesezeit
  56. 56 Building a "My Points" Widget for CMS Pages ~7 Min. Lesezeit
  57. 57 Widget Parameters and Admin Configuration (widget.xml) ~7 Min. Lesezeit
  58. 58 Understanding Page Builder Content Types ~5 Min. Lesezeit
  59. 59 Building a Custom "Points Banner" Content Type for Page Builder ~9 Min. Lesezeit
  60. 60 Implementing the Page Builder Preview (Template) Correctly ~6 Min. Lesezeit
  61. 61 Widget vs. Page Builder Content Type: When to Use Which ~6 Min. Lesezeit
  62. 62 Custom Payment Methods in Magento 2: Fundamentals and the Adapter Facade ~7 Min. Lesezeit
  63. 63 The "Redeem Points" Payment Method: Partial Payment Without Real Split Tender ~9 Min. Lesezeit
  64. 64 Payment Method Configuration and Availability: canUseCheckout and isAvailable ~7 Min. Lesezeit
  65. 65 Payment Method in Checkout: Registering the Knockout.js Frontend Component ~8 Min. Lesezeit
  66. 66 Custom Shipping Carriers in Magento 2: Fundamentals ~6 Min. Lesezeit
  67. 67 "Free Shipping via Points" as a Custom Carrier ~7 Min. Lesezeit
  68. 68 Shipping Rate Calculation: Building Custom Rate Result Objects ~6 Min. Lesezeit
  69. 69 Testing the Payment and Shipping Method Together in Checkout ~5 Min. Lesezeit
  70. 70 Common Mistakes with Custom Payment and Shipping Methods ~7 Min. Lesezeit
  71. 71 Custom Product Types in Magento 2: When It's Worth It ~6 Min. Lesezeit
  72. 72 Registering the "Points Package" Product Type: etc/product_types.xml ~5 Min. Lesezeit
  73. 73 Implementing the Product Type Model: Price Model and Type Model ~8 Min. Lesezeit
  74. 74 Admin Form Adjustments for the New Product Type ~7 Min. Lesezeit
  75. 75 Displaying and Selling the Product Type Correctly in the Frontend ~6 Min. Lesezeit
  76. 76 Adapting Order Processing for the New Product Type ~9 Min. Lesezeit
  77. 77 Product Type Compatibility with Price Rules and Discounts ~6 Min. Lesezeit
  78. 78 Summary: Introducing Custom Product Types the Right Way ~5 Min. Lesezeit
  79. 79 Service Contracts as the Basis for REST, SOAP, and GraphQL: Api\Data\RewardInterface and RewardRepositoryInterface ~8 Min. Lesezeit
  80. 80 A Custom REST API: webapi.xml and ACL for the Points Balance ~7 Min. Lesezeit
  81. 81 An API Endpoint for Redeeming a Reward ~8 Min. Lesezeit
  82. 82 A GraphQL Endpoint: Querying the Points Balance and Reward Catalog ~8 Min. Lesezeit
  83. 83 A GraphQL Mutation: Redeeming a Reward via GraphQL ~7 Min. Lesezeit
  84. 84 Customer Section Data: The Points Balance in the Mini-Cart/Checkout via AJAX ~6 Min. Lesezeit
  85. 85 API Versioning and Backward Compatibility ~6 Min. Lesezeit
  86. 86 Rate Limiting and Hardening Custom Endpoints ~7 Min. Lesezeit
  87. 87 API Documentation: REST Swagger and GraphQL Schema Introspection ~6 Min. Lesezeit
  88. 88 Configuration Types in Depth: Custom Config Sources Beyond System/Config ~7 Min. Lesezeit
  89. 89 Multi-Language Support: Maintaining Your Own i18n CSV Files Correctly ~5 Min. Lesezeit
  90. 90 Translations for Store Views and the Admin Interface ~5 Min. Lesezeit
  91. 91 Unit Tests: Testing the PointsCalculator Service ~7 Min. Lesezeit
  92. 92 Unit Tests: Mocking Repositories and Services ~8 Min. Lesezeit
  93. 93 Integration Tests vs. Unit Tests: When Each One Makes Sense ~6 Min. Lesezeit
  94. 94 Prioritizing Test Coverage Without 100% Dogma ~6 Min. Lesezeit
  95. 95 Continuous Integration: Running Tests Automatically ~7 Min. Lesezeit
  96. 96 Ensuring PHPStan and Code Quality in a Custom Module ~8 Min. Lesezeit
  97. 97 All Areas at a Glance: How the 32 Building Blocks Work Together ~8 Min. Lesezeit
  98. 98 Cleanly Managing Module Dependencies (the module.xml sequence) ~6 Min. Lesezeit
  99. 99 The Deployment Sequence for a Module This Large ~8 Min. Lesezeit
  100. 100 Performance Considerations for the Whole Module ~8 Min. Lesezeit
  101. 101 Security Considerations: ACL, Input Validation, and CSRF Across All Areas ~9 Min. Lesezeit
  102. 102 Extensibility: How Other Developers Can Extend This Module ~8 Min. Lesezeit
  103. 103 A Troubleshooting Guide for the Whole Project ~9 Min. Lesezeit
  104. 104 Where to Go From Here: Related Tutorial Series in This Catalog ~5 Min. Lesezeit
  105. 105 Cheat Sheet: All 32 Module Areas at a Glance ~7 Min. Lesezeit
  106. 106 Conclusion and Outlook: What Makes a Magento Module "Complete" ~7 Min. Lesezeit