Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1406733
    ankitdesigns
    Participant

    My understanding is that when I have “Related Products” enabled in the Customizer > WooCommerce that on my product page I will see other products too, but I thought it was based on the product’s category.

    Here’s a function I have, using WooCommerce’s related.php file as the backbone:

    function woocommerce_chla_long_description() {
    
    	GLOBAL $product, $woocommerce_loop;
    
    	$related = $product->get_related( $posts_per_page );
    
    	if ( sizeof( $related ) == 0 ) return;
    
    	$args = apply_filters( 'woocommerce_related_products_args', array(
    	  'post_type'           => 'product',
    	  'ignore_sticky_posts' => 1,
    	  'no_found_rows'       => 1,
    	  // 'posts_per_page'      => 3,
    	  'showposts'   		=>  3,
    	  'orderby'     		=>  'rand',
    	  'order'       		=>  'DESC',
    	  'post__in'            => $related,
    	  'post__not_in'        => array( $product->id )
    	) );
    
        $products = new WP_Query( $args );
        
        $html_out = '<div class="course-meta-description">';
    		
    		$html_out .= '<div class="x-column x-sm x-2-3" style="padding: 0px;">';
    			$html_out .= '<p>' . get_the_content() . '</p>';
    		$html_out .= '</div>';
    	
    		$html_out .= '<div class="x-column x-sm x-1-3" style="padding: 0px;">';
    			$term_list = wp_get_post_terms($products->post->ID, 'product_cat', array("fields" => "all"));
                $first_term = $term_list[0];
                $first_term_name = $first_term->name;
    
    			$html_out .= '<h4>' . 'More ' . $first_term_name . ' Courses' . '</h4>';
    
    		$enable  = x_get_option( 'x_woocommerce_product_related_enable' );
    
    	    if ( $products->have_posts() && $enable == '1' ) :
    
    			$html_out .= '<ul class="related-course-list">';
    
          			while ( $products->have_posts() ) : $products->the_post();
    
          				$course_title = get_the_title();
          				$url = get_permalink();
          				$course_level = get_field( "course_level" );
    			        $course_id = get_field( "course_id" );
    
          				$html_out .= '<li>';
          					$html_out .= '<a href="' . $url . '">' . $course_title . '</a>';
          					$html_out .= $course_level . ' - ' . $course_id;
    	      			$html_out .= '</li>';
    
          			endwhile;
    
    		    $html_out .= '</ul>'; //end related course list
    
    	    else : // No results
    		    $html_out = "No Courses Found.";
    		endif; 
    
    		$html_out .= '</div>';
        $html_out .= '</div>'; //end course-meta-description
    
        wp_reset_postdata();
    
        echo $html_out;
    }

    It’s outputting products outside of it’s category. Here’s a link, it’s ordered by ‘rand’ so you may need to refresh a few times to see the issue, a Grade 11 course should not appear on a Grade 12 product page, and vice versa.

    http://chlacademy.ca/courses/grade-11/physics-sph3u/

    #1407069
    Nabeel A
    Moderator

    Hi there,

    Thanks for writing in! Regretfully this isn’t a feature offered by X. It could be possible with custom development, but this would be outside the scope of support we can offer. You may wish to consult a developer to assist you with this. X is quite extensible with child themes, so there are plenty of possibilities.

    Thanks for understanding. Take care!

  • <script> jQuery(function($){ $("#no-reply-1406733 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>