Reputation: 1773
Currently I have a WooCommerce installation where product URLs are produced as such:
domain.com/shop/product-category/product-name/
Due to SEO and current site structure, I would like to change it to:
domain.com/product-category/product-name/
I know how to remove shop from the breadcrumb using the woocommerce_get_breadcrumb filter, but I am unsure how to proceed for the URLs themselves.
All I've found on Stack Overflow are people recommending to install a plugin called Premmerce. This plugin has a premium version and constantly hassles you to upgrade: Removing /product-category/ and /shop/ from URL in WooCommerce
I would like to do this programmatically from my own plugin or just functions.php
Upvotes: 2
Views: 1090
Reputation: 1773
WooCommerce forces /product/ on you if you try only inputting /%product_cat%/ in the permalinks panel through class-wc-admin-permalink-settings.php. The wc permalinks is an option called "woocommerce_permalinks". Since I only want to have /%product_cat%/, I can just force it even though it's not the most elegant solution:
add_action( "update_option_woocommerce_permalinks", "apply_product_cat", 10, 3 );
function apply_product_cat( $old_val, $new_val, $option_name ) {
if ($option_name == "woocommerce_permalinks") {
$new_val['product_base'] = "/%product_cat%/";
update_option( "woocommerce_permalinks", $new_val );
}
}
What Premmerce does is effectively offer you another admin panel, where they just save to this option on their own.
Upvotes: 1