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

WooCommerce integration #1

Closed
saleebm opened this issue Feb 15, 2020 · 4 comments
Closed

WooCommerce integration #1

saleebm opened this issue Feb 15, 2020 · 4 comments

Comments

@saleebm
Copy link

saleebm commented Feb 15, 2020

Why I want it

  • simple page numbers for product catalogs with complex filters and sorting options.

Is it worth it?

  • Not for typical SEO, probably would prefer using cursors as specific pointers to each edge for consistency.
  • But for clients, I see a few benefits with implementing this after reading your tutorial. Especially when it comes to a reducing complexity in the wp_query.

how?

foreach ( \WP_GraphQL_WooCommerce::get_post_types() as $post_type ) {
	$post_type_object = get_post_type_object( $post_type );
	self::add_post_type_fields( $post_type_object );
}

add_filter( 'graphql_product_connection_query_args',  function($query_args, $source, $args, $context, $info) {
                if ( isset( $args['where']['offsetPagination']['offset'] ) ) {
			$query_args['offset'] = $args['where']['offsetPagination']['offset'];
		}
                return $query_args;
}), 10, 5);

I'd like to see what you would think, seeing as you created this and I'm fairly new to all this, all I am trying to do is get some input if this makes any sense or not. I personally forked it and implemented the WooCommerce feature here.

Thank you for this awesome work!

@mehidi258
Copy link

I am facing similar issue. offsetPagination working for all other post types but for products it's not working!
image
Is there any way to add support for products as well or i am missing something?

@esamattis
Copy link
Member

Here's something Mehidi Hassan did on the WPGraphQL slack:

/**
 * Add offset pagination to products.
 */
function theme_prefix_add_offset_pagination_to_products() {
	register_graphql_field(
		'RootQueryToProductConnectionWhereArgs',
		'offsetPagination',
		array(
			'type'        => 'OffsetPagination',
			'description' => 'Paginate content nodes with offsets',
		)
	);
}
add_action( 'graphql_register_types', 'theme_prefix_add_offset_pagination_to_products' );
/**
 * Add offset pagination to products.
 *
 * @param array $query_args query args.
 * @param array $where_args where query args.
 */
function theme_prefix_filter_map_offset_to_wp_query_args(
	array $query_args,
	array $where_args
) {
	if ( isset( $where_args['offsetPagination']['offset'] ) ) {
		$query_args['offset'] = $where_args['offsetPagination']['offset'];
	}
	if ( isset( $where_args['offsetPagination']['size'] ) ) {
		$query_args['posts_per_page'] =
			intval( $where_args['offsetPagination']['size'] ) + 1;
	}
	return $query_args;
}
add_filter( 'graphql_map_input_fields_to_product_query', 'theme_prefix_filter_map_offset_to_wp_query_args', 10, 2 );

The thread:
https://wp-graphql.slack.com/archives/C3NM1M291/p1616563369156900

@creative-andrew
Copy link

From the solution mentioned in the comment above

Only the following part is needed:

/**
 * Add offset pagination to products.
 */
function theme_prefix_add_offset_pagination_to_products() {
	register_graphql_field(
		'RootQueryToProductConnectionWhereArgs',
		'offsetPagination',
		array(
			'type'        => 'OffsetPagination',
			'description' => 'Paginate content nodes with offsets',
		)
	);
}
add_action( 'graphql_register_types', 'theme_prefix_add_offset_pagination_to_products' );

The filter graphql_map_input_fields_to_product_query has been deprecated and its recommended to use instead

graphql_map_input_fields_to_wp_query which is already implemented by the plugin on

function op_filter_map_offset_to_wp_query_args(

@ZeroPie
Copy link

ZeroPie commented May 25, 2024

As of today on:
wp-graphql-offset-pagination: 0.2.0
WooGraphQL Version: 0.2.0
WPGraphQL Version: 1.26
WordPress Version: 6.5.3
WooCommerce Version: 8.8.3

This worked:
https://gist.github.com/ZeroPie/9870b323c14262e6df19d44bf72d8445

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

5 participants