Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with pagination using version 0.20 and 0.21 #885

Open
gol4nsky opened this issue Aug 13, 2024 · 3 comments
Open

Problem with pagination using version 0.20 and 0.21 #885

gol4nsky opened this issue Aug 13, 2024 · 3 comments

Comments

@gol4nsky
Copy link

Describe the bug
I am experiencing an issue with pagination in versions 0.20 and 0.21. This bug did not exist in version 0.19. When I set the after parameter, I get the same results as if the after attribute were not set, regardless of the key used from pageInfo.

To Reproduce
query Products {
products(
first: 20
where: {orderby: {field: MENU_ORDER, order: ASC}, category: "foo"}
after: ""
) {
nodes {
name
}
pageInfo {
endCursor
hasNextPage
}
}
}

Plugin Versions

  • WooGraphQL Version: - 0.20, 0.21
  • WPGraphQL Version: - 1.28.0
  • WordPress Version: - 6.6.1
  • WooCommerce Version: - 9.1.4
@AliAUZ
Copy link

AliAUZ commented Oct 12, 2024

Same as mine I have the same issue

@lotariodev
Copy link

I have the same issue :(

@robbiebel
Copy link

Hi guys,

I have a patch for this if someone needs.

I saw many issues about this(#896, #885, #860, #843, #827) and I faced also.

So tried to fix it and I have done for my needs. It might not fit your use case. I left it when mine is okay. But there can be another things so if someone has any question about I can try to answer because I examined it.

Patch for WooGraphQL:

diff --git a/includes/data/connection/class-product-connection-resolver.php b/includes/data/connection/class-product-connection-resolver.php
index 8c89289..97e9bcd 100644
--- a/includes/data/connection/class-product-connection-resolver.php
+++ b/includes/data/connection/class-product-connection-resolver.php
@@ -181,20 +181,16 @@ class Product_Connection_Resolver extends AbstractConnectionResolver {
 		if ( $offset_product && 'price' === $query_args['orderby'] ) {
 			/** @var array<float>|float|null $price */
 			$price = is_a( $offset_product, 'WC_Product_Variable' )
-				? $offset_product->get_variation_price()
+				? $offset_product->get_variation_price( 'ASC' === $query_args['order'] ? 'min' : 'max' )
 				: $offset_product->get_price();
 			if ( 'ASC' === $query_args['order'] ) {
-				if ( is_array( $price ) ) {
-					$price = reset( $price );
-				}
 				$query_args['graphql_cursor_compare_by_price_key']   = 'wc_product_meta_lookup.min_price';
 				$query_args['graphql_cursor_compare_by_price_value'] = $price;
+				$query_args['graphql_cursor_compare'] = '>';
 			} else {
-				if ( is_array( $price ) ) {
-					$price = end( $price );
-				}
 				$query_args['graphql_cursor_compare_by_price_key']   = 'wc_product_meta_lookup.max_price';
 				$query_args['graphql_cursor_compare_by_price_value'] = $price;
+				$query_args['graphql_cursor_compare'] = '<';
 			}
 		}
 
@@ -221,6 +217,17 @@ class Product_Connection_Resolver extends AbstractConnectionResolver {
 		 */
 		$query_args['fields'] = 'ids';
 
+		/**
+		 * If orderby contains 'title', we remove it and fallback to menu_order only.
+		 * This is because title ordering is not properly supported in the current implementation
+		 * and can cause unexpected results. Using menu_order as fallback maintains a consistent
+		 * product order in the catalog.
+		 */
+		if ( strpos( $query_args['orderby'], 'title' ) !== false ) {
+			$query_args['orderby'] = trim( str_replace( 'title', '', $query_args['orderby'] ) ) ?: 'menu_order';
+			$query_args['order'] = isset( $last ) ? 'ASC' : 'DESC';
+		}
+
 		/**
 		 * Filter the $query args to allow folks to customize queries programmatically
 		 *

We also need patch for WPGraphQL:

diff --git a/src/Data/Cursor/AbstractCursor.php b/src/Data/Cursor/AbstractCursor.php
index 11586356..67c510da 100644
--- a/src/Data/Cursor/AbstractCursor.php
+++ b/src/Data/Cursor/AbstractCursor.php
@@ -299,7 +299,9 @@ abstract class AbstractCursor {
 		// Get ID SQL Query alias.
 		$key = $this->get_cursor_id_key();
 
-		$this->builder->add_field( $key, $value, 'ID' );
+		$compare = $this->get_query_var( 'graphql_cursor_compare' ) === '>' ? 'ASC' : 'DESC';
+		
+		$this->builder->add_field( $key, $value, 'ID', $compare );
 	}
 
 	/**

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

No branches or pull requests

4 participants