Skip to content

Fix: Growth Alert (Inline) N+1 database queries on WooCommerce shop/archive pages#133

Open
ZebaAfiaShama wants to merge 1 commit into
WPDevelopers:latestfrom
ZebaAfiaShama:83239-growth-alert-inline-duplicate-queries
Open

Fix: Growth Alert (Inline) N+1 database queries on WooCommerce shop/archive pages#133
ZebaAfiaShama wants to merge 1 commit into
WPDevelopers:latestfrom
ZebaAfiaShama:83239-growth-alert-inline-duplicate-queries

Conversation

@ZebaAfiaShama

Copy link
Copy Markdown

Problem

Growth Alert (inline) registers its render callback on woocommerce_after_shop_loop_item and woocommerce_after_shop_loop_item_title, which fire once per product in shop/archive loops. Every callback run called Inline::get_notifications_data(), whose request-level cache was commented out — so the same product-independent data was re-queried from the database for every product on the page (classic N+1). Reported by a client whose shop pages slowed down, with NotificationX\Core\Database->get_posts dominating Query Monitor.

This affects every site running NotificationX Pro with the WooCommerce module enabled — the queries fire even with zero Growth Alert campaigns created.

Fix

Restore request-level memoization in Inline::get_notifications_data() (includes/Features/Inline.php), keyed by $source so different inline sources cannot receive each other's cached data (the likely reason the previous un-keyed cache was disabled). The computed result only varies by $source$id/$settings are only passed to the nx_inline_notifications_data short-circuit filter — so per-request caching is safe. Per-product filtering still happens afterward in the extension render loop.

The old public $notifications_data property keeps its previous shape/behavior for backward compatibility.

Verification (Docker sandbox, WP + WooCommerce 10.9.1, NX 3.2.10 + Pro 3.1.2, 11 products, one active Growth Alert stock campaign)

Database->get_posts total from shop-loop callback
Before 137 132
After 11 6

Rendered Growth Alert markup is byte-identical before/after (verified by diffing the archive page HTML). With no inline campaign, per-page calls drop from 49 to 7.

Card

https://projects.startise.com/wp-admin/admin.php?page=fluent-boards#/boards/19/tasks/83239-Bug-Fix-%7C-Growth-Alert-(Inline

🤖 Generated with Claude Code

Growth Alert (inline) hooks fire once per product on WooCommerce
shop/archive pages, and each run re-queried the database for data
that only varies by source. Restore request-level memoization in
Inline::get_notifications_data(), keyed by source so different
inline sources cannot receive each other's cached data.

Measured on an 11-product archive with one active Growth Alert:
Database->get_posts calls drop from 137 to 11 per page load with
identical rendered output.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant