shshanker
12/9/2015 - 4:24 AM

// make the categories non hierarchical for their permalinks //added rewrite rules to make "product-category-base" and "product-permalink-ba

// make the categories non hierarchical for their permalinks //added rewrite rules to make "product-category-base" and "product-permalink-base" // woocommerce get category url slug and then "rewrite" the single product permalink

<?php 


// make the categories non hierarchical for their permalinks
function my_woocommerce_taxonomy_args_product_cat( $args ) {
	$args['rewrite']['hierarchical'] = false;
	//print_r($args['rewrite']); die();
	return $args;
}
add_filter( 'woocommerce_taxonomy_args_product_cat', 'my_woocommerce_taxonomy_args_product_cat' );


//added rewrite rules to make "product-category-base" and "product-permalink-base"
add_filter( 'rewrite_rules_array', function( $rules )
{
    $new_rules = array(
        'products-page/([^/]*?)/page/([0-9]{1,})/?$' => 'index.php?product_cat=$matches[1]&paged=$matches[2]',
        'products-page/([^/]*?)/?$' => 'index.php?product_cat=$matches[1]',
    );
    return $new_rules + $rules;
} );

// woocommerce get category url slug and then "rewrite" the single product permalink
add_action('woocommerce_register_post_type_product', 'my_woocommerce_register_post_type_product' );
function my_woocommerce_register_post_type_product($args){
	
 	$full_path = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 	$parts = Explode('/', $full_path);
	$current_cat = $parts[count($parts) - 1];
	$current_cat_base = $parts[count($parts) - 2];
	if($current_cat_base != 'products-page'){
		return $args;	
	}

	$args['rewrite']['slug'] = '/products-page/'.$current_cat;
	return $args;	
	

	
}


?>