Show full category hierarchy in permalinks.

Closes #6158
This commit is contained in:
Mike Jolley 2015-03-27 11:47:44 +00:00
parent 33a3940501
commit 7bbd0261d7
2 changed files with 20 additions and 9 deletions

View File

@ -198,23 +198,35 @@ function wc_get_featured_product_ids() {
*/
function wc_product_post_type_link( $permalink, $post ) {
// Abort if post is not a product
if ( $post->post_type !== 'product' )
if ( $post->post_type !== 'product' ) {
return $permalink;
}
// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, '%' ) )
if ( false === strpos( $permalink, '%' ) ) {
return $permalink;
}
// Get the custom taxonomy terms in use by this post
$terms = get_the_terms( $post->ID, 'product_cat' );
if ( empty( $terms ) ) {
if ( ! empty( $terms ) ) {
usort( $terms, '_usort_terms_by_ID' ); // order by ID
$category_object = apply_filters( 'wc_product_post_type_link_product_cat', $terms[0], $terms, $post );
$category_object = get_term( $category_object, 'product_cat' );
$product_cat = $category_object->slug;
if ( $parent = $category_object->parent ) {
$ancestors = get_ancestors( $category_object->term_id, 'product_cat' );
foreach ( $ancestors as $ancestor ) {
$ancestor_object = get_term( $ancestor, 'product_cat' );
$product_cat = $ancestor_object->slug . '/' . $product_cat;
}
}
} else {
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
$product_cat = _x( 'uncategorized', 'slug', 'woocommerce' );
} else {
// Replace the placeholder rewrite tag with the first term's slug
$first_term = array_shift( $terms );
$product_cat = $first_term->slug;
}
$find = array(
@ -241,8 +253,6 @@ function wc_product_post_type_link( $permalink, $post ) {
$product_cat
);
$replace = array_map( 'sanitize_title', $replace );
$permalink = str_replace( $find, $replace, $permalink );
return $permalink;

View File

@ -138,6 +138,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
== Changelog ==
* Feature - Show full category hierarchy in permalinks.
* Fix - Ensure coupon taxes are reset when calculating totals.
* Tweak - Base discounts on the undiscounted price. #5874