• Resolved jwpalfa

    (@jwpalfa)


    I have 2 product categories: membership and merchandise. The membership category will only ever have one product, i.e. membership. The merchandise category will have multiple products. The default shop page is fine but I want the link for the membership category to go to /product/membership instead of /product_category/membership. The link for merchandise is fine as /product_category/merchandise.

    How can I best alter the url for the link in the shop page?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Stef

    (@serafinnyc)

    You will have to write a hook for that. I was going to suggest you just change that in Permalinks but if your site is older you might ruin your seo.

    The easiest way is in permalinks but merchandise will have to change too.

    If you know how to add a hook let me know.

    Thread Starter jwpalfa

    (@jwpalfa)

    I’d be happy to use a hook if you can give me a couple of pointers.

    Stef

    (@serafinnyc)

    No I mean I was hoping you’d show me LOL I don’t know if it’s even possible. I mean anything’s possible. :o)

    Thread Starter jwpalfa

    (@jwpalfa)

    Seems like it should be pretty straightforward …

    Just remove the action that builds the link url and add it back in with some logic to build the link for $category == membership with /product and leave the other links alone and using /product_category.

    I just can’t figure out where the hook goes!

    Thread Starter jwpalfa

    (@jwpalfa)

    For the record, here’s what I have come up with in my search for a way to skip the product category page for a category with only 1 product:

    This Woo function builds the url for the product category page inside the shop page loop:

    function woocommerce_template_loop_category_link_open( $category ) {
        echo '<a href="' . esc_url( get_term_link( $category, 'product_cat' ) ) . '">';
    }

    It can be removed with the woocommerce_before_subcategory hook. As in:

    remove_action( 'woocommerce_before_subcategory', 
        'woocommerce_template_loop_category_link_open', 10 );

    Then I created my own function to build the url. I took a bit of a shortcut since I know the ‘Membership’ category has only one product named ‘Membership’. If I was wanting to generalize this behaviour I would check the number of products in each category and build the product page url for any category with 1 product instead of the product category page url. Put that in my rainy day job jar 🙂

    Here’s my brute force url builder:

    function arcc_template_loop_category_link_open( $category ) {
        $category_name = $category->name;
        if ( $category_name == 'Membership' ) {
            $product = get_page_by_title( 'Membership', OBJECT, 'product' );
            $product_id = $product->ID; 
            $arcclink = get_the_permalink( $product_id );
        } else {
            $arcclink = get_term_link( $category, 'product_cat' );
        }
        echo '<a href="' . esc_url( $arcclink ) . '">';
    }
    add_action( 'woocommerce_before_subcategory', 
        'arcc_template_loop_category_link_open', 10);

    I’m not sure what this does to the breadcrumbs but that’s for another day.

    • This reply was modified 7 years ago by jwpalfa.
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘variable link in shop page’ is closed to new replies.