New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move discount codes to custom table #5277

Closed
pippinsplugins opened this Issue Dec 6, 2016 · 46 comments

Comments

@pippinsplugins
Member

pippinsplugins commented Dec 6, 2016

For numerous reasons, including performance and flexibility, discount codes should be moved to a custom table.

This will help lay the foundation for moving payments and sale logs to custom tables.

Related:
#5217
#1427
#4576

Pull Request: #6224

New database schema

There are two new tables being introduced:

  • wp_edd_discounts
  • wp_edd_discountmeta

Schema for wp_edd_discounts:

Field Type Null Key Default Extra
id bigint(20) unsigned NO PRI NULL auto_increment
name varchar(200) NO   NULL  
code varchar(50) NO MUL NULL  
status varchar(20) NO   NULL  
type varchar(20) NO   NULL  
amount mediumtext NO   NULL  
description longtext NO   NULL  
max_uses bigint(20) NO   NULL  
use_count bigint(20) NO   NULL  
once_per_customer int(1) NO   NULL  
min_cart_price mediumtext NO   NULL  
scope varchar(30) NO   all  
date_created datetime NO    
start_date datetime NO   NULL  
end_date datetime NO   NULL  

Schema for wp_edd_discountmeta:

Field Type Null Key Default Extra
meta_id bigint(20) NO PRI NULL auto_increment
edd_discount_id bigint(20) NO MUL NULL  
meta_key varchar(255) YES MUL NULL  
meta_value longtext YES   NULL  

New files

  • includes/class-db-discounts.php
  • includes/class-db-discount-meta.php

These files hold the DB interaction layers for discounts and discount meta.

Changes to EDD_Discount

The EDD_Discount class has been significantly updated in order to achieve a few goals:

  1. Bring its properties in line with the DB structure. A number of properties have been renamed. For example, is_not_global is now called applies_globally and its value has been adjusted accordingly.
  2. Reduce duplicate logic for adding and updating discounts.
  3. Make it more consistent with EDD_Payment, EDD_Commission, EDD_SL_License, EDD_Subscription, etc.
  4. Magic methods have been heavily utilized to provide a complete backwards compatibility layer for previous method and property names. For example, the expiration property no longer exists as it is now called end_date. Calling $discount->expiration will continue to function exactly the same, however, due to the BC layer that has been added.

The existing helper functions for discounts in includes/discount-functions.php have been left mostly unchanged. Only a few adjustments to account for data type enforcement.

Backwards compatibility layer

  1. EDD_Discount. As a lot of properties and methods have changed, we need to ensure the old names still work when accessed directly. For example, $discount->start doesn't technically exist anymore because it's been renamed to start_date. Calling $discount->start returns the value of $discount->start_date. This has been done for properties and methods.

  2. Post meta. I have implemented a BC layer that redirects calls to get_post_meta(), add_post_meta(), and update_post_meta().

  3. All legacy parameters previously used in edd_store_discount() still work. Those parameters can be passed to edd_store_discount(), EDD_Discount->add(), and EDD_Discount->update() and they will be automatically converted to the new parameters.

  4. A BC layer will be implemented with get_posts() and WP_Query. It will hijack the WP SQL query and instead query the wp_edd_discounts table but return WP_Post objects with the data filled from the wp_edd_discounts. A BC layer is not available for get_post() due to the way it's implemented in WordPress Core.

Upgrade routine

A full upgrade and migration routine has been added that moves discounts from the old wp_posts table to the new wp_edd_discounts table. This upgrade can be run via the UI or through WP CLI with wp edd migrate_discounts.

The upgrade routine includes a second step for removing legacy data.

Unit tests

The discount tests have been up into two sections:

  • New tests that directly test EDD_Discount
  • Legacy tests

So long as the legacy tests continue to pass without changes, we're pretty safe in knowing the BC layer is good.

tests-discounts-db.php and tests-discount-meta.php have been introduced that holds tests specific to the databases.

Todo

  • More unit tests that cover DB layer
  • New unit tests file for discount meta DB
  • More unit tests for EDD_Discount
  • Implement discount searching
  • Add debug log statements to migration routine
  • Change applies_globally to scope=global
  • Move date methods ahead of max_uses
  • Move excluded products into meta
  • Add documentation to query discounts correctly (with reference to the message displayed on pre_get_posts)

Notes for testing

  1. Please test on brand new installs
  2. Please test on existing installs that need to be migrated
  3. When testing, consider the following items as the most important
    a. Creating discounts
    b. Editing discounts
    c. Applying discounts to the cart (test all permutations of discount restrictions)
  4. Please test the upgrade routine from the UI and from WP CLI (if able)
  5. Please test any known extensions that interact with discount codes, such as Discount Code Generator and EDD Social Discounts

@pippinsplugins pippinsplugins added this to the 2.8 milestone Dec 6, 2016

@pippinsplugins pippinsplugins modified the milestone: 2.8 Feb 14, 2017

@pippinsplugins pippinsplugins added this to the 3.0 milestone Dec 4, 2017

pippinsplugins added a commit that referenced this issue Dec 5, 2017

pippinsplugins added a commit that referenced this issue Dec 5, 2017

pippinsplugins added a commit that referenced this issue Dec 6, 2017

pippinsplugins added a commit that referenced this issue Dec 6, 2017

sunnyratilal added a commit that referenced this issue Mar 31, 2018

sunnyratilal added a commit that referenced this issue Mar 31, 2018

sunnyratilal added a commit that referenced this issue Apr 3, 2018

sunnyratilal added a commit that referenced this issue Apr 3, 2018

sunnyratilal added a commit that referenced this issue Apr 3, 2018

sunnyratilal added a commit that referenced this issue Apr 3, 2018

@sunnyratilal sunnyratilal moved this from Discounts to Done in 3DD™: Custom Tables Apr 6, 2018

@sunnyratilal sunnyratilal moved this from Schema Changes to Done in Release 3.0 Sprint May 1, 2018

@JJJ

This comment has been minimized.

Show comment
Hide comment
@JJJ

JJJ May 7, 2018

Collaborator

This issue is technically done, so let's close it! 🥇

If bugs are discovered, new issues should be created.

<3

Collaborator

JJJ commented May 7, 2018

This issue is technically done, so let's close it! 🥇

If bugs are discovered, new issues should be created.

<3

@JJJ JJJ closed this May 7, 2018

sunnyratilal added a commit that referenced this issue Jun 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment