Okay
  Public Ticket #2565296
Single Product Navigation Buttons to loop within the select category
Closed

Comments

  • jfung started the conversation

    Hi,

    The single product navigation buttons doesn't loop within products in the selected category.

    For example, if I were to select the Diamond Rings sub-category, and then click into a single product, it would be more user-friendly to have the single product navigation buttons to loop products within the Diamond Rings sub-category. 

    Currently, the single product navigation buttons can redirect to products that are outside of the Diamond Rings category and it may be confusing/inconvenient for users since they selected a specific category to browse products on.

    Is there a way to change the way the single product navigation button links are generated to stay within the selected category?

    Please note that I know this is how WooCommerce works by default, so it's not a bug from the theme, but I wasn't able to find information on this. So if you can provide any help or pointers on this, that would be terrific.

    Thanks!


  •  895
    TheFox replied

    Hi,

    We will answer both of your tickets here.

    So our team add the navigation using the following code :

    add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_navigation_link', 10 );
    function woocommerce_output_navigation_link(){
        
        
        ob_start();?>
    <div class="single_product_navigation"><!--?php $next = get_permalink(get_adjacent_post(false,'',true)); if ($next != get_permalink()) { ?--><a href="<?php echo esc_url($next); ?>" class="previous_product"><!--?php echo __('Previous', 'thefoxwp'); ?--></a><!--?php } ?--><!--?php $prev = get_permalink(get_adjacent_post(false,'',false)); if ($prev != get_permalink()) { ?--><a href="<?php echo esc_url($prev); ?>" class="next_product"><!--?php echo __('Next', 'thefoxwp'); ?--></a><!--?php } ?--></div>
    <!--?php
    $output = ob_get_clean();
    echo !empty( 6;output ) ? $output : '';
            
    }
    

    so what you would need to do is remove this action

    remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_navigation_link', 20 );

    and re-use the same code we sent 

    add_action( 'woocommerce_before_single_product', 'new_woocommerce_output_navigation_link', 1 );
    function new_woocommerce_output_navigation_link(){
        
        
        ob_start();?>
    <div class="single_product_navigation"><!--?php $next = get_permalink(get_adjacent_post(true,'',true)); if ($next != get_permalink()) { ?--><a href="<?php echo esc_url($next); ?>" class="previous_product"><!--?php echo __('Previous', 'thefoxwp'); ?--></a><!--?php } ?--><!--?php $prev = get_permalink(get_adjacent_post(true,'',false)); if ($prev != get_permalink()) { ?--><a href="<?php echo esc_url($prev); ?>" class="next_product"><!--?php echo __('Next', 'thefoxwp'); ?--></a><!--?php } ?--></div>
    <!--?php
    $output = ob_get_clean();
    echo !empty( 6;output ) ? $output : '';
            
    }
    


    This should put the navigation on the top and make it in the same category only.

    Usually our team doesn't provide code snippet like this but it seems that our devs had some free time.

    If you need more modification, please contact https://wpbuilders.co/request-service/

    Have a nice weekend!

    Best regards

  • jfung replied

    Hi,

    Thank you for the help!

    Just wondering, do I just copy and paste the codes to add the navigation buttons?

    I've done that and am getting "unexpected end of file" error (screenshot-1).

    I then tried to remove the comment tags in the functions (screenshot-2) but was getting the unexpected ";" error.


    Please advise!

    Thanks again for the code snippet though!

    Best Regards



  •  895
    TheFox replied

    Hello,

    I am sorry but I don't know, I think you are supposed to use the code they give us in your child theme functions.php

    If you need customization please contact https://wpbuilders.co/request-service/

    Sorry for not being able to help you this time.

    Best regards

  • jfung replied

    Hi,

    I have been putting that code in the child theme's functions.php, but was getting the errors that I provided in the screenshots in the previous response.

    Okay so it seems there isn't going to be anymore help, aside from a maybe-working code example.

    I guess I will have to figure it out myself.

    Thanks.

  •  895
    TheFox replied

    Hello Jfung,

    Try to see if there isn't an error in the code when you copy / paste it.

    We had some conversion errors when pasting code in the forum threads in the past, that might be the source of the error.

    Sorry to not being able to help you more than thatfrown.png

    Do not hesitate to contact us if you have more questions.

    Have a nice day!

    Best Regards.

  • jfung replied

    I did check and made sure I didn't miss anything when copying and pasting the codes.

    It's okay, I ended up using some other approach that I found on the internet and used css to hide the product navigation buttons at the bottom.

    It make the website loading extra things and extra css to hide things but at least it works, and the performance shouldn't be affected, so I am happy with that.

    As for the "same category" part of the code, I dug into the code and it seems that the "same category" refers to the most generic category of links: posts, products, etc. With get_adjacent_post, it doesn't go as specific as to loop within a certain product category.

    It's a bummer, but it's how WooCommerce works.

    Thanks for the help.