From c68583b67ceb59555c932540bdaaa38b464a2565 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 17 Aug 2023 16:26:49 +0100 Subject: [PATCH] Fix: sync_endpoint_with_page should use a matching slug if it exists instead of creating a new page (#10640) --- src/BlockTemplatesController.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 4713ef892f2..ba7d00ed286 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -848,6 +848,15 @@ public function update_template_permalink( $value, $option ) { * @return string THe actual permalink assigned to the page. May differ from $permalink if it was already taken. */ protected function sync_endpoint_with_page( $page, $page_slug, $permalink ) { + $matching_page = get_page_by_path( $permalink ); + + if ( $matching_page && 'publish' === $matching_page->post_status ) { + // Existing page matches given permalink; use its ID. + update_option( 'woocommerce_' . $page_slug . '_page_id', $matching_page->ID ); + return $permalink; + } + + // No matching page; either update current page (by ID stored in option table) or create a new page. if ( ! $page ) { $updated_page_id = wc_create_page( esc_sql( $permalink ),