简体   繁体   中英

WooCommerce custom slug for specific product category

I try to create a custom permalink for products in a specific product category but it works really weird.

I try to achieve URLs like:

  1. domain.com/category_name/product-name - for products in a specific category,
  2. domain.com/default_slug/product-name - for the rest of products

I tried to add the %type% rule to product permalinks by using a two-step approach to (1) generate proper URLs and (2) show respective product page:

function custom_permalink($permalink, $post_id, $leavename) {
  if (strpos($permalink, '%type%') === FALSE) return $permalink;
         
  // Get post
  $post = get_post($post_id);
  if (!$post) return $permalink;
     
  // Get taxonomy terms
  $terms = wp_get_object_terms($post->ID, 'product_cat');
  if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
    if ($terms[0]->slug == 'category_name') {
      $taxonomy_slug = $terms[0]->slug;
    } else {
      $taxonomy_slug = 'default_slug';
    }
  } 
  else {
    $taxonomy_slug = 'default_slug';
  }
     
  return str_replace('%type%', $taxonomy_slug, $permalink);
}
add_filter('post_link', 'custom_permalink', 10, 3);
add_filter('post_type_link', 'custom_permalink', 10, 3);

function rewrite_product_base_tag() {
  add_rewrite_tag( '%type%', '([^&]+)', 'in-type=' );
}
add_action( 'init', 'rewrite_product_base_tag', 10, 0 );

It works fine except it shows products even if I use any random slug. The following URLs will show the product based on its name (every single URL shows the "product-name" product):

  • domain.com/default_slug/product-name
  • domain.com/category_name/product-name
  • domain.com/ANY_RANDOM_STRING/product-name

I also tried to use different regex to limit available slugs. It only shows the second option (category_name) properly. The first one (default_slug) shows the Archive page. It works exactly the same even if those are changed with each other: the first one shows the Archive page, the second one works fine.

Adding a rewrite_rule function doesn't work at all.


function rewrite_product_base_tag() {
  add_rewrite_tag( '%type%', '(default_slug|category_name)', 'in-type=' );
}
add_action( 'init', 'rewrite_product_base_tag', 10, 0 );

add_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
function add_custom_attributes_to_permalink( $post_id, $post, $update ) {

    //make sure we are only working with Products
    if ($post->post_type != 'product' || $post->post_status == 'auto-draft') {
        return;
    }

    //get the product
    $_product = wc_get_product($post_id);

    //get the "clean" permalink based on the post title
    $clean_permalink = sanitize_title( $post->post_title, $post_id );

    //next we get all of the attribute slugs, and separate them with a "-"
    $attribute_slugs = array(); //we will be added all the attribute slugs to this array
    foreach ($_product->get_attributes(); as $attribute_slug => $attribute_value) {
        $attribute_slugs[] = $attribute_value;
    }
    $attribute_suffix = implode('-', $attribute_slugs);

    //then add the attributes to the clean permalink
    $full_permalink = $clean_permalink.$attribute_suffix;

    // unhook the save post action to avoid a broken loop
    remove_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );

    // update the post_name (which becomes the permalink)
    wp_update_post( array(
        'ID' => $post_id,
        'post_name' => $full_permalink
    ));

    // re-hook the save_post action
    add_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM