Advertisement
Guest User

related.php

a guest
Oct 25th, 2016
3,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.22 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Related Products
  4.  *
  5.  * This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.php.
  6.  *
  7.  * HOWEVER, on occasion WooCommerce will need to update template files and you
  8.  * (the theme developer) will need to copy the new files to your theme to
  9.  * maintain compatibility. We try to do this as little as possible, but it does
  10.  * happen. When this occurs the version of the template file will be bumped and
  11.  * the readme will list any important changes.
  12.  *
  13.  * @see         https://docs.woocommerce.com/document/template-structure/
  14.  * @author      WooThemes
  15.  * @package     WooCommerce/Templates
  16.  * @version     1.6.4
  17.  */
  18.  
  19. if ( ! defined( 'ABSPATH' ) ) {
  20.     exit;
  21. }
  22.  
  23. global $product, $woocommerce_loop;
  24.  
  25. if ( empty( $product ) || ! $product->exists() ) {
  26.     return;
  27. }
  28.  
  29. if ( ! $related = $product->get_related( $posts_per_page ) ) {
  30.     return;
  31. }
  32.  
  33. $cats_array = array(0);
  34.  
  35. // get categories
  36. $terms = wp_get_post_terms( $product->id, 'product_cat' );
  37.  
  38. // select only the category which doesn't have any children
  39. foreach ( $terms as $term ) {
  40.     $children = get_term_children( $term->term_id, 'product_cat' );
  41.     if ( !sizeof( $children ) )
  42.     $cats_array[] = $term->term_id;
  43. }
  44.  
  45. var_dump($cats_array);
  46.  
  47. $args = apply_filters( 'woocommerce_related_products_args', array(
  48.     'post_type' => 'product',
  49.     'ignore_sticky_posts' => 1,
  50.     'no_found_rows' => 1,
  51.     'posts_per_page' => $posts_per_page,
  52.     'orderby' => $orderby,
  53.     'tax_query' => array(
  54.         array(
  55.             'taxonomy' => 'product_cat',
  56.             'field' => 'id',
  57.             'terms' => $cats_array
  58.         ),
  59.     )
  60. ));
  61.  
  62. $products                    = new WP_Query( $args );
  63. $woocommerce_loop['name']    = 'related';
  64. $woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
  65.  
  66. if ( $products->have_posts() ) : ?>
  67.  
  68.     <div class="related products">
  69.  
  70.         <h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
  71.  
  72.         <?php woocommerce_product_loop_start(); ?>
  73.  
  74.             <?php while ( $products->have_posts() ) : $products->the_post(); ?>
  75.  
  76.                 <?php wc_get_template_part( 'content', 'product' ); ?>
  77.  
  78.             <?php endwhile; // end of the loop. ?>
  79.  
  80.         <?php woocommerce_product_loop_end(); ?>
  81.  
  82.     </div>
  83.  
  84. <?php endif;
  85.  
  86. wp_reset_postdata();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement