HomeBlogErrors / FixesWooCommerce кастомная категориальная навигация с использованием «каталог
Errors / FixesSeptember 5, 20262 min

WooCommerce кастомная категориальная навигация с использованием «каталог

WooCommerce кастомная категориальная навигация с использованием «каталог
WooCommerce кастомная категориальная навигация с использованием «каталог - image 2

WooCommerce Custom Category Pagination Using "Catalog"

Introduction

On a website that was built almost 10 years ago, a custom rewrite rule was implemented, allowing the word "catalog" to be used as the base for the store and categories. It was possible to set "catalog" as the base for product categories in the permalinks settings. This setup worked for short category archive pages but stopped working with pagination at some point. This fact makes one wonder about the specificity of using the term "catalog," which may be reserved for WooCommerce setups that do not handle transactions.

Problem and Its Causes

The problem lies in the fact that when using the term "catalog" for a product category, pages with pagination return a 404 error. This might be related to the fact that WooCommerce uses the term "catalog" to represent a product catalog without transactions, whereas "shop" is used for active sales.

Solution

To solve this issue, you can use rewrite rule override filters and add your own rules for product category pagination. Here's an example code snippet:

add_filter( 'rewrite_rules_array', function( $rules ) {
    $new_rules = array();
    $terms = get_terms( array(
        'taxonomy' => 'product_cat',
        'post_type' => 'product',
        'hide_empty' => false,
    ));
    
    if ( $terms && ! is_wp_error( $terms ) ) {
        $siteurl = esc_url( home_url( '/' ) );
        foreach ( $terms as $term ) {
            $term_slug = $term->slug;
            $baseterm = str_replace( $siteurl, '', get_term_link( $term->term_id, 'product_cat' ) );
            
            // rules for specific category
            $new_rules[$baseterm .'?$'] = 'index.php?product_cat=' . $term_slug;
            
            // rules for category pagination
            $new_rules[$baseterm . '/page/([0-9]{1,})/?$'] = 'index.php?product_cat=' . $term_slug . '&paged=$matches[1]';
        }
        
        return $new_rules + $rules;
    }
});

Practical Tips

  1. Testing: After making changes, thoroughly test the site for 404 errors and correct pagination display.
  2. Manage Rewrite Rules: Ensure that your custom rewrite rule does not conflict with other rewrite rules.
  3. Save Rewrite Rules: Don't forget to save the rewrite rules after each change in the permalinks settings.

Conclusion

Using the word "catalog" as the base for product categories can cause issues with pagination in WooCommerce. To address this problem, you can use rewrite rule override filters and add your own rules for product category pagination.

SEO