MrBoBo69

code to add previous and next in product page

Mar 7th, 2025
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.86 KB | Software | 0 0
  1. <?php
  2. // get next and prev products
  3.  
  4. function ShowLinkToProduct($post_id, $categories_as_array, $label) {
  5.     // get post according post id
  6.     $query_args = array( 'post__in' => array($post_id), 'posts_per_page' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
  7.         array(
  8.             'taxonomy' => 'product_cat',
  9.             'field' => 'id',
  10.             'terms' => $categories_as_array
  11.         )));
  12.     $r_single = new WP_Query($query_args);
  13.     if ($r_single->have_posts()) {
  14.         $r_single->the_post();
  15.         global $product;
  16.     ?>
  17.     <li style="display: inline-block !important; margin-right: 300px;">
  18.         <a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
  19.             <?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
  20.             <?php echo $label; ?>
  21.             <?php if ( get_the_title() ) the_title(); else the_ID(); ?>
  22.             <?php echo $product->get_price_html(); ?>
  23.         </a>
  24.     </li>
  25.     <?php
  26.         wp_reset_query();
  27.     }
  28. }
  29.  
  30. if ( is_singular('product') ) {
  31.     global $post;
  32.  
  33.     // get categories
  34.     $terms = wp_get_post_terms( $post->ID, 'product_cat' );
  35.     foreach ( $terms as $term ) $cats_array[] = $term->term_id;
  36.  
  37.     // get all posts in current categories
  38.     $query_args = array('posts_per_page' => -1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
  39.         array(
  40.             'taxonomy' => 'product_cat',
  41.             'field' => 'id',
  42.             'terms' => $cats_array
  43.         )));
  44.     $r = new WP_Query($query_args);
  45.  
  46.     // show next and prev only if we have 3 or more
  47.     if ($r->post_count > 2) {
  48.  
  49.         $prev_product_id = -1;
  50.         $next_product_id = -1;
  51.  
  52.         $found_product = false;
  53.         $i = 0;
  54.  
  55.         $current_product_index = $i;
  56.         $current_product_id = get_the_ID();
  57.  
  58.         if ($r->have_posts()) {
  59.             while ($r->have_posts()) {
  60.                 $r->the_post();
  61.                 $current_id = get_the_ID();
  62.  
  63.                 if ($current_id == $current_product_id) {
  64.                     $found_product = true;
  65.                     $current_product_index = $i;
  66.                 }
  67.  
  68.                 $is_first = ($current_product_index == $first_product_index);
  69.  
  70.                 if ($is_first) {
  71.                     $prev_product_id = get_the_ID(); // if product is first then 'prev' = last product
  72.                 } else {
  73.                     if (!$found_product && $current_id != $current_product_id) {
  74.                         $prev_product_id = get_the_ID();
  75.                     }
  76.                 }
  77.  
  78.                 if ($i == 0) { // if product is last then 'next' = first product
  79.                     $next_product_id = get_the_ID();
  80.                 }
  81.  
  82.                 if ($found_product && $i == $current_product_index + 1) {
  83.                     $next_product_id = get_the_ID();
  84.                 }
  85.  
  86.                 $i++;
  87.             }
  88.  
  89.             echo '<ul class="product_list_widget">';
  90.             if ($next_product_id != -1) { ShowLinkToProduct($next_product_id, $cats_array, "Prev:<br> "); }
  91.            
  92.             if ($prev_product_id != -1) { ShowLinkToProduct($prev_product_id, $cats_array, "Next:<br> "); }
  93.            
  94.             echo '</ul><hr>';
  95.         }
  96.  
  97.         wp_reset_query();
  98.     }
  99. }
  100. ?>
  101.  
  102.  
  103.  
  104. <?php
  105. /**
  106.  * The template for displaying product content in the single-product.php template
  107.  *
  108.  * This template can be overridden by copying it to yourtheme/woocommerce/content-single-product.php.
  109.  *
  110.  * HOWEVER, on occasion WooCommerce will need to update template files and you
  111.  * (the theme developer) will need to copy the new files to your theme to
  112.  * maintain compatibility. We try to do this as little as possible, but it does
  113.  * happen. When this occurs the version of the template file will be bumped and
  114.  * the readme will list any important changes.
  115.  *
  116.  * @see     https://woo.com/document/template-structure/
  117.  * @package WooCommerce\Templates
  118.  * @version 3.6.0
  119.  */
  120.  
  121. defined( 'ABSPATH' ) || exit;
  122.  
  123. global $product;
  124.  
  125. /**
  126.  * Hook: woocommerce_before_single_product.
  127.  *
  128.  * @hooked woocommerce_output_all_notices - 10
  129.  */
  130. do_action( 'woocommerce_before_single_product' );
  131.  
  132. if ( post_password_required() ) {
  133.     echo get_the_password_form(); // WPCS: XSS ok.
  134.     return;
  135. }
  136. ?>
  137. <div id="product-<?php the_ID(); ?>" <?php wc_product_class( '', $product ); ?>>
  138.  
  139.     <?php
  140.     /**
  141.      * Hook: woocommerce_before_single_product_summary.
  142.      *
  143.      * @hooked woocommerce_show_product_sale_flash - 10
  144.      * @hooked woocommerce_show_product_images - 20
  145.      */
  146.     do_action( 'woocommerce_before_single_product_summary' );
  147.     ?>
  148.  
  149.     <div class="summary entry-summary">
  150.         <?php
  151.         /**
  152.          * Hook: woocommerce_single_product_summary.
  153.          *
  154.          * @hooked woocommerce_template_single_title - 5
  155.          * @hooked woocommerce_template_single_rating - 10
  156.          * @hooked woocommerce_template_single_price - 10
  157.          * @hooked woocommerce_template_single_excerpt - 20
  158.          * @hooked woocommerce_template_single_add_to_cart - 30
  159.          * @hooked woocommerce_template_single_meta - 40
  160.          * @hooked woocommerce_template_single_sharing - 50
  161.          * @hooked WC_Structured_Data::generate_product_data() - 60
  162.          */
  163.         do_action( 'woocommerce_single_product_summary' );
  164.         ?>
  165.     </div>
  166.  
  167.     <?php
  168.     /**
  169.      * Hook: woocommerce_after_single_product_summary.
  170.      *
  171.      * @hooked woocommerce_output_product_data_tabs - 10
  172.      * @hooked woocommerce_upsell_display - 15
  173.      * @hooked woocommerce_output_related_products - 20
  174.      */
  175.     do_action( 'woocommerce_after_single_product_summary' );
  176.     ?>
  177. </div>
  178.  
  179. <?php do_action( 'woocommerce_after_single_product' ); ?>
Tags: php
Advertisement
Add Comment
Please, Sign In to add comment