Custom DB Prefix
-
There’s a bug in MailerLite for WooCommerce that triggers SQL errors and makes the settings page very slow when WordPress uses a custom table prefix.
Environment:
- WP with custom prefix
bcksz_(notwp_) - WooCommerce up to date
- Query Monitor enabled
Error from Query Monitor:
Unknown column wp_wc_customer_lookup.customer_id in 'field list'Root cause:
File:includes/models/WooMailerLiteCustomer.php
Method:getAll($limit = 100)The SQL selects use hard-coded
wp_table names, e.g.:select("wp_wc_customer_lookup.customer_id, wp_wc_customer_lookup.email, max(wp_wc_order_stats.order_id) AS last_order_id, max(wp_wc_order_stats.date_created) AS last_order, count(DISTINCT wp_wc_order_stats.order_id) AS orders_count, sum(wp_wc_order_stats.total_sales) AS total_spent, ...")While the method already retrieves
$prefix = db()->prefix;and uses{$prefix}in other parts of the query.Expected behavior:
All WooCommerce table references must use the dynamic prefix ($wpdb->prefix/db()->prefix) instead of the hard-codedwp_.Proposed fix (idea):
Replacewp_wc_customer_lookup/wp_wc_order_statswith variables built from the prefix:$prefix = db()->prefix; $tbl_customers = "{$prefix}wc_customer_lookup"; $tbl_orderstats = "{$prefix}wc_order_stats"; select("{$tbl_customers}.customer_id, {$tbl_customers}.email, max({$tbl_orderstats}.order_id) AS last_order_id, max({$tbl_orderstats}.date_created) AS last_order, count(DISTINCT {$tbl_orderstats}.order_id) AS orders_count, sum({$tbl_orderstats}.total_sales) AS total_spent, ...") - WP with custom prefix
The topic ‘Custom DB Prefix’ is closed to new replies.