Home Forums Basel support forum Related Products Tag Fiters

Related Products Tag Fiters

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #14055

    dimitriadis
    Participant

    Hi,
    I would like to make a customization that will fetch the related products of a product DYNAMICALLY, based on the product’ s tags.
    Let me give you an example: I have a pair of sunglasses with the following tags – female, round, metal I need my related products to have ALL these 3 tags; hence, bring me as related products only the products with tags – female AND round AND metal.

    I searched and found some solutions on this matter but, since you have already customized many WooCommerce files, it is extremely difficult to follow your pattern.

    Any tips and hints, please?

    Thank you in advance.

    #14096

    Artem Temos
    Keymaster

    Hello,

    Thank you for choosing our theme and contacting us.

    Sorry, but additional code customizations are out of theme support scope and we can’t do this for you. But if you need, we can provide you with a path to the file you looking for. Actually, we didn’t change WooCommerce template files locations. What files are you trying to find?

    Kind Regards
    XTemos Studio

    #14212

    dimitriadis
    Participant

    Hi,
    Thank you for your immediate response.

    I need to modify the related.php file.
    I am creating a new function get_related function (found in the /classes/abstracts/abstract-wc-product.php file of the WooCommerce plugin directory) with the following code:

    //New "Related Products" function for WooCommerce
    function get_related_custom( $id, $limit = 5 ) {
        global $woocommerce;
        // Related products are found from category and tag
        $tags_array = array(0);
        $cats_array = array(0);
        // Get tags
        $terms = wp_get_post_terms($id, 'product_tag');
        foreach ( $terms as $term ) $tags_array[] = $term->term_id;
        // Get categories (removed by NerdyMind)
    /*
        $terms = wp_get_post_terms($id, 'product_cat');
        foreach ( $terms as $term ) $cats_array[] = $term->term_id;
    */
        // Don't bother if none are set
        if ( sizeof($cats_array)==1 && sizeof($tags_array)==1 ) return array();
        // Meta query
        $meta_query = array();
        $meta_query[] = $woocommerce->query->visibility_meta_query();
        $meta_query[] = $woocommerce->query->stock_status_meta_query();
        // Get the posts
        $related_posts = get_posts( apply_filters('woocommerce_product_related_posts', array(
            'orderby'        => 'rand',
            'posts_per_page' => $limit,
            'post_type'      => 'product',
            'fields'         => 'ids',
            'meta_query'     => $meta_query,
            'tax_query'      => array(
                'relation'      => 'OR',
                array(
                    'taxonomy'     => 'product_cat',
                    'field'        => 'id',
                    'terms'        => $cats_array
                ),
                array(
                    'taxonomy'     => 'product_tag',
                    'field'        => 'id',
                    'terms'        => $tags_array
                )
            )
        ) ) );
        $related_posts = array_diff( $related_posts, array( $id ));
        return $related_posts;
    }
    add_action('init','get_related_custom');

    Then I need to go to related.php file (/themes/basel/woocommerce/single-product/related.php)
    and add the following line of code:
    $related = get_related_custom($product->id);

    The code in your related.php file is the following:

    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    
    if ( $related_products ) : ?>
    
    	<section class="related products">
    		<?php 
    
    			$slider_args = array(
    				'slides_per_view' => apply_filters( 'basel_related_products_per_view', 4 ),
    				'title' => esc_html__( 'Related products', 'woocommerce' ),
    				'img_size' => 'shop_catalog'
    			);
    
    			echo basel_generate_posts_slider( $slider_args, false, $related_products );
    			
    		?>
    
    	</section>
    
    <?php endif;
    
    wp_reset_postdata();

    When I add the line of code mentioned above, the website crashes.

    Any advices?

    Thank you in advance.

    #14223

    Artem Temos
    Keymaster

    You need to enable debug mode or check error logs on the server. If the website crashes then there is some PHP error and you need to check it.

Viewing 4 posts - 1 through 4 (of 4 total)