Commit d5e7c308743 for woocommerce

commit d5e7c30874357ecfe8066ddf613d8110aaa20db2
Author: Tung Du <dinhtungdu@gmail.com>
Date:   Tue Nov 17 12:04:41 2026 +0700

    Allow setting the global products per page from Product Collection (#70531)

    * Add products per page to Product Collection blocks using the default query

    * Add changelog entry for Product Collection archive products per page

    * Fix archive products-per-page template resolution

    * Add changelog entries for archive products per page

    * Use one store-wide products per page instead of a value per template

    Replace the archive template lookup with the woocommerce_catalog_products_per_page option, as agreed in #54240. On block themes, the loop_shop_per_page filter is always added and reads the option once. When nothing is saved, it keeps the store's current page size. The Customizer hides Rows per page while the filter is in use, because this setting replaces it. Classic themes are not affected.

    * Add a store-wide Products per page control under Default sort by

    The control saves the site option, the same way Default sort by does. It has its own help text, and Reset deletes the option so the store default comes back. The editor preview uses the saved value.

    * Update changelog entries for the store-wide products per page

    Replace the per-template entries with ones for the single store-wide setting.

    ---------

    Co-authored-by: John Coy <john.coy@automattic.com>

diff --git a/plugins/woocommerce/changelog/add-product-collection-default-products-per-page b/plugins/woocommerce/changelog/add-product-collection-default-products-per-page
new file mode 100644
index 00000000000..25e4e03e3e0
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-product-collection-default-products-per-page
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Set how many products each shop and category page shows, from the Product Collection block.
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/product-collection/edit/inspector-controls/default-query-products-per-page-control.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/product-collection/edit/inspector-controls/default-query-products-per-page-control.tsx
new file mode 100644
index 00000000000..46b317d8cf5
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/product-collection/edit/inspector-controls/default-query-products-per-page-control.tsx
@@ -0,0 +1,90 @@
+/**
+ * External dependencies
+ */
+import { __ } from '@wordpress/i18n';
+import { getSettingWithCoercion } from '@woocommerce/settings';
+import { isNumber } from '@woocommerce/types';
+import { store as coreStore } from '@wordpress/core-data';
+import { useDispatch, useSelect } from '@wordpress/data';
+import {
+	RangeControl,
+	// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
+	__experimentalToolsPanelItem as ToolsPanelItem,
+} from '@wordpress/components';
+
+/**
+ * Internal dependencies
+ */
+import { CoreFilterNames, TrackInteraction } from '../../types';
+
+const MIN_PRODUCTS_PER_PAGE = 1;
+const MAX_PRODUCTS_PER_PAGE = 100;
+const OPTION_NAME = 'woocommerce_catalog_products_per_page';
+
+const label = __( 'Products per page', 'woocommerce' );
+
+const DefaultQueryProductsPerPageControl = ( {
+	trackInteraction,
+}: {
+	trackInteraction: TrackInteraction;
+} ) => {
+	const storedValue = useSelect(
+		( select ) =>
+			(
+				select( coreStore ).getEditedEntityRecord(
+					'root',
+					'site',
+					undefined
+				) as Record< string, unknown > | undefined
+			 )?.[ OPTION_NAME ],
+		[]
+	);
+	const { editEntityRecord } = useDispatch( coreStore );
+	const storeDefault = getSettingWithCoercion(
+		'loopShopPerPage',
+		12,
+		isNumber
+	);
+	const hasValue = isNumber( storedValue );
+
+	const setValue = ( newValue: number | null ) => {
+		void editEntityRecord( 'root', 'site', undefined, {
+			[ OPTION_NAME ]: newValue,
+		} );
+		trackInteraction( CoreFilterNames.PRODUCTS_PER_PAGE );
+	};
+
+	return (
+		<ToolsPanelItem
+			label={ label }
+			isShownByDefault
+			hasValue={ () => hasValue }
+			onDeselect={ () => setValue( null ) }
+		>
+			<RangeControl
+				__next40pxDefaultSize
+				__nextHasNoMarginBottom
+				label={ label }
+				help={ __(
+					'Applies to all Product Collection blocks using the Default Query.',
+					'woocommerce'
+				) }
+				min={ MIN_PRODUCTS_PER_PAGE }
+				max={ MAX_PRODUCTS_PER_PAGE }
+				onChange={ ( newValue?: number ) => {
+					if (
+						newValue === undefined ||
+						newValue < MIN_PRODUCTS_PER_PAGE ||
+						newValue > MAX_PRODUCTS_PER_PAGE
+					) {
+						return;
+					}
+					setValue( newValue );
+				} }
+				value={ hasValue ? storedValue : storeDefault }
+			/>
+		</ToolsPanelItem>
+	);
+};
+
+export default DefaultQueryProductsPerPageControl;
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/product-collection/edit/inspector-controls/index.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/product-collection/edit/inspector-controls/index.tsx
index 7ac6d944b35..99e7de0c9fc 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/product-collection/edit/inspector-controls/index.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/product-collection/edit/inspector-controls/index.tsx
@@ -48,6 +48,7 @@ import LinkedProductControl from './linked-product-control';
 import WidthOptionsControl from './width-options-control';
 import RelatedByControl from './related-by-control';
 import ProductsPerPageControl from './products-per-page-control';
+import DefaultQueryProductsPerPageControl from './default-query-products-per-page-control';
 import OffsetControl from './offset-control';
 import MaxPagesToShowControl from './max-pages-to-show-control';

@@ -103,6 +104,9 @@ const ProductCollectionInspectorControls = (
 	const showProductsPerPageControl =
 		showCustomQueryControls &&
 		shouldShowFilter( CoreFilterNames.PRODUCTS_PER_PAGE );
+	const showDefaultProductsPerPageControl =
+		showDefaultOrderControl &&
+		shouldShowFilter( CoreFilterNames.PRODUCTS_PER_PAGE );
 	const showOnSaleControl = shouldShowFilter( CoreFilterNames.ON_SALE );
 	const showStockStatusControl = shouldShowFilter(
 		CoreFilterNames.STOCK_STATUS
@@ -207,6 +211,11 @@ const ProductCollectionInspectorControls = (
 						trackInteraction={ trackInteraction }
 					/>
 				) }
+				{ showDefaultProductsPerPageControl && (
+					<DefaultQueryProductsPerPageControl
+						trackInteraction={ trackInteraction }
+					/>
+				) }
 				{ ! isEmailEditor && (
 					<LayoutOptionsControl { ...displayControlProps } />
 				) }
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/product-template/edit.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/product-template/edit.tsx
index a0329c03ca8..1693531df77 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/product-template/edit.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/product-template/edit.tsx
@@ -329,16 +329,20 @@ const ProductTemplateEdit = (
 						}
 					}
 				}
-				query.per_page = loopShopPerPage;
-
 				const settings = getEditedEntityRecord(
 					'root',
 					'site',
 					undefined
-				) as unknown as Record< string, string >;
+				) as unknown as Record< string, string | number >;
+
+				query.per_page = isNumber(
+					settings.woocommerce_catalog_products_per_page
+				)
+					? settings.woocommerce_catalog_products_per_page
+					: loopShopPerPage;

 				const orderProperties = getOrderPropertiesForDefaultQuery(
-					settings.woocommerce_default_catalog_orderby
+					settings.woocommerce_default_catalog_orderby as string
 				);
 				query.orderby = orderProperties.orderby;
 				query.order = orderProperties.order;
diff --git a/plugins/woocommerce/client/blocks/changelog/add-product-collection-default-products-per-page b/plugins/woocommerce/client/blocks/changelog/add-product-collection-default-products-per-page
new file mode 100644
index 00000000000..25e4e03e3e0
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/changelog/add-product-collection-default-products-per-page
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Set how many products each shop and category page shows, from the Product Collection block.
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection/Controller.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection/Controller.php
index b1d7a9e557d..2ba074751cc 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection/Controller.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection/Controller.php
@@ -14,6 +14,11 @@ class Controller extends AbstractBlock {

 	use EnableBlockJsonAssetsTrait;

+	/**
+	 * Option that holds how many products each shop and category page shows on block themes.
+	 */
+	private const PRODUCTS_PER_PAGE_OPTION = 'woocommerce_catalog_products_per_page';
+
 	/**
 	 * Block name.
 	 *
@@ -70,6 +75,11 @@ class Controller extends AbstractBlock {
 			2
 		);

+		// On block themes, Products per page in the editor replaces "Rows per page", which the Customizer hides while this filter is in use.
+		if ( wp_is_block_theme() ) {
+			add_filter( 'loop_shop_per_page', array( $this, 'handle_loop_shop_per_page' ), 20, 1 );
+		}
+
 		// Register the backend settings so they can be used in the editor.
 		add_action( 'rest_api_init', array( $this, 'register_settings' ) );

@@ -86,6 +96,26 @@ class Controller extends AbstractBlock {
 		$this->register_core_collections_and_set_handler_store();
 	}

+	/**
+	 * Use the saved products per page on shop and category pages.
+	 *
+	 * In wp-admin it keeps the store default, so the editor shows that number until a value is saved.
+	 *
+	 * @internal
+	 * @since 11.4.0
+	 *
+	 * @param int $per_page Products per page from the store settings or another filter.
+	 * @return int
+	 */
+	public function handle_loop_shop_per_page( $per_page ) {
+		$value = get_option( self::PRODUCTS_PER_PAGE_OPTION );
+		if ( is_admin() || ! is_numeric( $value ) || (int) $value < 1 || (int) $value > 100 ) {
+			return $per_page;
+		}
+
+		return (int) $value;
+	}
+
 	/**
 	 * Add interactivity to the Product Title block within Product Collection.
 	 * This enables the triggering of a custom event when the product title is clicked.
@@ -256,6 +286,25 @@ class Controller extends AbstractBlock {
 				'default'      => 'menu_order',
 			)
 		);
+
+		register_setting(
+			'options',
+			self::PRODUCTS_PER_PAGE_OPTION,
+			array(
+				'type'              => 'integer',
+				'description'       => __( 'How many products to show on each shop and category page.', 'woocommerce' ),
+				'label'             => __( 'Products per page', 'woocommerce' ),
+				'sanitize_callback' => 'absint',
+				'show_in_rest'      => array(
+					'name'   => self::PRODUCTS_PER_PAGE_OPTION,
+					'schema' => array(
+						'type'    => 'integer',
+						'minimum' => 1,
+						'maximum' => 100,
+					),
+				),
+			)
+		);
 	}

 	/**
diff --git a/plugins/woocommerce/tests/e2e/tests/blocks/product-collection/product-collection-site-templates.block_theme.spec.ts b/plugins/woocommerce/tests/e2e/tests/blocks/product-collection/product-collection-site-templates.block_theme.spec.ts
index 8e650a742f1..94bcbaf6dbc 100644
--- a/plugins/woocommerce/tests/e2e/tests/blocks/product-collection/product-collection-site-templates.block_theme.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/blocks/product-collection/product-collection-site-templates.block_theme.spec.ts
@@ -148,5 +148,37 @@ test.describe( 'Product Collection', () => {
 				'option update woocommerce_default_catalog_orderby menu_order'
 			);
 		} );
+
+		test( 'products per page applies to the whole shop page', async ( {
+			page,
+			pageObject,
+			editor,
+		} ) => {
+			await pageObject.goToEditorTemplate();
+			await pageObject.focusProductCollection();
+
+			const productsPerPageInput = pageObject
+				.locateSidebarSettings()
+				.getByRole( 'spinbutton', { name: 'Products per page' } );
+			await expect( productsPerPageInput ).toBeVisible();
+
+			await productsPerPageInput.fill( '5' );
+			await pageObject.refreshLocators( 'editor' );
+			await expect( pageObject.products ).toHaveCount( 5 );
+
+			await editor.saveSiteEditorEntities();
+			await pageObject.goToProductCatalogFrontend();
+
+			// The shop query uses this number, so pagination and the results count match the products shown.
+			await expect( pageObject.products ).toHaveCount( 5 );
+			await expect( pageObject.pagination ).toBeVisible();
+			await expect(
+				page.locator( '.wc-block-product-results-count' )
+			).toHaveText( /Showing 1[-–]5 of \d+ results/ );
+
+			await wpCLI(
+				'option delete woocommerce_catalog_products_per_page'
+			);
+		} );
 	} );
 } );
diff --git a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection/ControllerTest.php b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection/ControllerTest.php
index 5364fb54dde..d5a5977690d 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection/ControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection/ControllerTest.php
@@ -73,6 +73,45 @@ class ControllerTest extends WC_Unit_Test_Case {
 		);
 	}

+	/**
+	 * @testdox Should use the saved products per page only when it is between 1 and 100.
+	 * @testWith [null, 16]
+	 *           [24, 24]
+	 *           [0, 16]
+	 *           [101, 16]
+	 *
+	 * @param int|null $stored   Saved option value, or null when nothing is saved.
+	 * @param int      $expected Expected products per page.
+	 */
+	public function test_loop_shop_per_page_uses_store_wide_value( ?int $stored, int $expected ): void {
+		if ( null !== $stored ) {
+			update_option( 'woocommerce_catalog_products_per_page', $stored );
+		}
+
+		$this->assertSame( $expected, $this->sut->handle_loop_shop_per_page( 16 ) );
+	}
+
+	/**
+	 * @testdox Should save products per page as a site setting, refuse values above 100, and delete it on reset.
+	 */
+	public function test_products_per_page_setting_is_editable_through_rest(): void {
+		wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+		$this->sut->register_settings();
+
+		$request = new \WP_REST_Request( 'POST', '/wp/v2/settings' );
+		$request->set_body_params( array( 'woocommerce_catalog_products_per_page' => 24 ) );
+		$response = rest_do_request( $request );
+		$this->assertSame( 200, $response->get_status() );
+		$this->assertSame( 24, (int) get_option( 'woocommerce_catalog_products_per_page' ) );
+
+		$request->set_body_params( array( 'woocommerce_catalog_products_per_page' => 101 ) );
+		$this->assertSame( 400, rest_do_request( $request )->get_status(), 'The settings endpoint refuses values above 100.' );
+
+		$request->set_body_params( array( 'woocommerce_catalog_products_per_page' => null ) );
+		rest_do_request( $request );
+		$this->assertFalse( get_option( 'woocommerce_catalog_products_per_page' ), 'Sending null deletes the option, so the store default comes back.' );
+	}
+
 	/**
 	 * @testdox Should add the viewed-product action to a linked Product Collection title.
 	 */