Created
May 23, 2017 17:19
-
-
Save DrewAPicture/a5d1db6938052bb354cf6749e69a1939 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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