Commit bbfea9befe0 for woocommerce

commit bbfea9befe068a4e1428aec646af5c09033aed57
Author: Kadir Er <3rkadir@gmail.com>
Date:   Fri Dec 4 17:59:45 2026 +0300

    Replace deprecated SQL_CALC_FOUND_ROWS in most stocked report (#71708)

    * Refactor SQL query preparation in stock report

    Refactor SQL queries to improve readability and maintainability.

    * Update Most stocked report to remove deprecated SQL usage

    Avoid deprecated SQL_CALC_FOUND_ROWS usage in the Most stocked report.

    * Fix SQL query for counting matching products

    * Refactor get_items method for stock query

    * Fix Most stocked report count query

    Update the Most stocked report count query to use prepared statements and match projected columns.

    * Add docblock for stock criteria method

    * Fix stock quantity query in most stocked report

    * Fix SQL query preparation and improve error handling

    * Update plugins/woocommerce/includes/admin/reports/class-wc-report-most-stocked.php

    Co-authored-by: louwie17 <lourensschep@gmail.com>

    ---------

    Co-authored-by: louwie17 <lourensschep@gmail.com>

diff --git a/plugins/woocommerce/changelog/fix-most-stocked-report-count-query b/plugins/woocommerce/changelog/fix-most-stocked-report-count-query
new file mode 100644
index 00000000000..600f8fef479
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-most-stocked-report-count-query
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Update Most stocked report count query to use prepared statement and match projected columns.
diff --git a/plugins/woocommerce/includes/admin/reports/class-wc-report-most-stocked.php b/plugins/woocommerce/includes/admin/reports/class-wc-report-most-stocked.php
index 26058d43cf4..d3967f9854d 100644
--- a/plugins/woocommerce/includes/admin/reports/class-wc-report-most-stocked.php
+++ b/plugins/woocommerce/includes/admin/reports/class-wc-report-most-stocked.php
@@ -1,18 +1,22 @@
 <?php
 /**
- * WC_Report_Most_Stocked.
+ * Most stocked report.
  *
  * @package WooCommerce\Admin\Reports
  */

-defined( 'ABSPATH' ) || exit;
+if ( ! defined( 'ABSPATH' ) ) {
+	exit;
+}

 if ( ! class_exists( 'WC_Report_Stock' ) ) {
-	require_once dirname( __FILE__ ) . '/class-wc-report-stock.php';
+	require_once __DIR__ . '/class-wc-report-stock.php';
 }

 /**
- * WC_Report_Most_Stocked.
+ * WC_Report_Most_Stocked
+ *
+ * @package WooCommerce\Admin\Reports
  */
 class WC_Report_Most_Stocked extends WC_Report_Stock {

@@ -20,7 +24,7 @@ class WC_Report_Most_Stocked extends WC_Report_Stock {
 	 * Get Products matching stock criteria.
 	 *
 	 * @param int $current_page Current page number.
-	 * @param int $per_page How many results to show per page.
+	 * @param int $per_page     Number of items to show per page.
 	 */
 	public function get_items( $current_page, $per_page ) {
 		global $wpdb;
@@ -28,24 +32,48 @@ class WC_Report_Most_Stocked extends WC_Report_Stock {
 		$this->max_items = 0;
 		$this->items     = array();

-		$stock = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 0 ) );
+		$low_stock_amount = max( 0, (int) get_option( 'woocommerce_notify_low_stock_amount', 0 ) );

+		// Get products using criteria.
 		$query_from = apply_filters(
 			'woocommerce_report_most_stocked_query_from',
 			$wpdb->prepare(
-				"
-				FROM {$wpdb->posts} as posts
+				" FROM {$wpdb->posts} as posts
 				INNER JOIN {$wpdb->wc_product_meta_lookup} AS lookup ON posts.ID = lookup.product_id
 				WHERE 1=1
 				AND posts.post_type IN ( 'product', 'product_variation' )
 				AND posts.post_status = 'publish'
 				AND lookup.stock_quantity > %d
 				",
-				$stock
+				$low_stock_amount
 			)
 		);

-		$this->items     = $wpdb->get_results( $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS posts.ID as id, posts.post_parent as parent {$query_from} ORDER BY lookup.stock_quantity DESC, id ASC LIMIT %d, %d;", ( $current_page - 1 ) * $per_page, $per_page ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
-		$this->max_items = $wpdb->get_var( 'SELECT FOUND_ROWS();' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+		if ( ! is_string( $query_from ) || '' === trim( $query_from ) ) {
+			return;
+		}
+
+		$offset = max( 0, ( (int) $current_page - 1 ) * (int) $per_page );
+		$limit  = max( 1, (int) $per_page );
+
+		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+		$items_sql = $wpdb->prepare(
+			"SELECT posts.ID as id, posts.post_parent as parent {$query_from} ORDER BY lookup.stock_quantity DESC, id ASC LIMIT %d, %d;",
+			$offset,
+			$limit
+		);
+
+		$count_sql = $wpdb->prepare(
+			"SELECT COUNT(%d) FROM ( SELECT posts.ID as id, posts.post_parent as parent {$query_from} ) AS matching_products;",
+			1
+		);
+		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+
+		if ( ! $items_sql || ! $count_sql ) {
+			return;
+		}
+
+		$this->items     = $wpdb->get_results( $items_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+		$this->max_items = (int) $wpdb->get_var( $count_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
 	}
 }