Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce Jetpack Infinite Scroll example Part 2
<?php
/**
* Workaround to prevent is_shop() from failing due to WordPress core issue
*
* @link https://core.trac.wordpress.org/ticket/21790
* @param array $args infinite scroll args.
* @return array infinite scroll args.
*/
function _s_woocommerce_is_shop_page() {
global $wp_query;
$front_page_id = get_option( 'page_on_front' );
$current_page_id = $wp_query->get( 'page_id' );
$shop_page_id = apply_filters( 'woocommerce_get_shop_page_id', get_option( 'woocommerce_shop_page_id' ) );
$is_static_front_page = 'page' === get_option( 'show_on_front' );
if ( $is_static_front_page && $front_page_id === $current_page_id ) {
$is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
} else {
$is_shop_page = is_shop();
}
return $is_shop_page;
}
/**
* Jetpack infinite scroll duplicates posts where orderby is anything other than modified or date
* This filter offsets the products returned by however many are displayed per page
*
* @link https://github.com/Automattic/jetpack/issues/1135
* @param array $args infinite scroll args.
* @return array infinite scroll args.
*/
function _s_woocommerce_jetpack_duplicate_products( $args ) {
if ( ( isset( $args['post_type'] ) && 'product' === $args['post_type'] ) || ( isset( $args['taxonomy'] ) && 'product_cat' === $args['taxonomy'] ) ) {
$args['offset'] = $args['posts_per_page'] * $args['paged'];
}
return $args;
}
add_filter( 'infinite_scroll_query_args', '_s_woocommerce_jetpack_duplicate_products', 100 );
/**
* Override number of products per page in Jetpack infinite scroll.
*
* @param array $args infinite scroll args.
* @return array infinite scroll args.
*/
function _s_woocommerce_jetpack_products_per_page( $args ) {
if ( is_array( $args ) && ( _s_woocommerce_is_shop_page() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
$args['posts_per_page'] = _s_woocommerce_products_per_page();
}
return $args;
}
add_filter( 'infinite_scroll_settings', '_s_woocommerce_jetpack_products_per_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.