I’m working on a website for a client who would like to have product navigation on each single product page. The navigation will go through each product within the category and when it reaches the last product it will return to a product overview page. I’ve tried a number of different approaches, but the only approach that has kind of worked thus far is using the wordpress post navigation:
<div class="prev-product"><?php previous_post_link('%link','prev'); ?> </div>
<div class="next-product"><?php next_post_link( '%link','next'); ?>
but this cycles through all products, not just category specific. I’ve also tried setting the prev/next post link like this, trying to remain within the category:
<div class="prev-product"><?php previous_post_link('%link','prev', TRUE); ?> </div>
<div class="next-product"><?php next_post_link( '%link','next', TRUE); ?></div>
but as it turns out, the post category is not being set because Woocommerce handles the category as a product category and not a post category. To try to work around this, I came up with this function:
function save_product_cat(){
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
wp_set_post_categories( $post->ID, $terms );
wp_set_object_terms( $post->ID, $terms->slug, 'category');
}
add_action('save_post', 'save_product_cat');
I’m trying to hook into when a post is saved so that I can grab the product category and set it as the category. When I test to see what the product category/ies are I’m still getting an empty array. Any ideas as to why my category isn’t getting saved?