$wpdb and WooCommerce — a practical guide to WooCommerce $wpdb with clear examples you can reuse in real projects.
WooCommerce Plugin Development Series (71/101). Prefer one article? Read the complete WooCommerce plugin development tutorial.
Short description
Prepare SQL, allowlist columns, and prefer Woo APIs for mutations.
Prepared query
global $wpdb;
$count = (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->prefix}acme_woo_licenses WHERE product_id = %d",
$product_id
)
);