Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrewAPicture/a5d1db6938052bb354cf6749e69a1939 to your computer and use it in GitHub Desktop.
Save DrewAPicture/a5d1db6938052bb354cf6749e69a1939 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: AffiliateWP - WooCommerce Product Categories Fix
* Plugin URI: https://affiliatewp.com
* Description: Fixes product category 404s when used in conjunction with pretty affiliate URLs.
* Author: AffiliateWP, LLC
* Author URI: https://affiliatewp.com
* Version: 1.0
*
* AffiliateWP Debug is distributed under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* AffiliateWP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AffiliateWP. If not, see <http://www.gnu.org/licenses/>.
*
* @package AffiliateWP
* @category Add-on
* @author Drew Jaynes
* @version 1.0
*/
add_filter( 'rewrite_rules_array', function( $rules ) {
if ( ! function_exists( 'WC' ) || ! function_exists( 'affiliate_wp' ) ) {
return $rules;
}
if ( true === version_compare( WC()->version, '3.0.0', '>=' ) ) {
$permalinks = wc_get_permalink_structure();
} else {
$permalinks = get_option( 'woocommerce_permalinks' );
}
$category_base = empty( $permalinks['category_base'] ) ? _x( 'product-category', 'slug', 'woocommerce' ) : $permalinks['category_base'];
$ref = affiliate_wp()->tracking->get_referral_var();
$category_rules = array(
$category_base . '/(.+?)/' . $ref . '(/(.*))?/?$' => 'index.php?product_cat=$matches[1]&' . $ref . '=$matches[3]'
);
$rules = array_merge( $category_rules, $rules );
return $rules;
}, 11 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment