Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1005568

    Hi Enfold team,

    I want to use the product slider as a ‘related products’ module, in a product page. I want to show all the other products from the subcategory, except of course the current one.

    I don’t want to use the “related products” module because it has not as many options as the product slider element, furthermore, I will need to add related products for each product, which I don’t want because I will waste time doing that instead of simply highlight the other products from the current subcategory.

    I have found this code on an enfold topic, regarding the same issue but with the portfolio element.

    
    function ava_exclude_portfolio($query) {
    	if (is_singular('portfolio')) {
        $exclude = avia_get_the_ID();
    		$query->set( 'post__not_in', array($exclude) );
    	}
    }
    
    add_action('pre_get_posts', 'ava_exclude_portfolio');

    What will be the code needed to do this trick with Woocommerce products instead of Portfolio items?
    Best regards,

    #1005580

    Hey dimitrilpc,

    I have not tested the code but you can try it with:

    
    function ava_exclude_portfolio($query) {
    	if (is_singular('product')) {
        $exclude = avia_get_the_ID();
    		$query->set( 'post__not_in', array($exclude) );
    	}
    }
    
    add_action('pre_get_posts', 'ava_exclude_portfolio');
    

    Best regards,
    Dude

    #1006174

    Hi dude,

    I didn’t saw your quick reply, thank you for it. I had tested this code, I should have stated it in my original post, sorry. I think the reason why this code doesn’t work is because WooCommerce have its own methods to call products. I see for example the avia_get_the_ID() method, and I wonder if this is the proper way to call a WC product ID.

    Best regards,

    #1006516

    Hi,

    You could use the global product variable to fetch the product id. You can use the variable like:

    
    global $product;
    $id = $product->get_id();
    

    (or before Woocommerce 2.6)

    
    global $product;
    $id = $product->id;
    

    You can also try to replace the pre_get_posts action with woocommerce_product_query ( http://hookr.io/actions/woocommerce_product_query/ – however this would require a rewrite of the function)

    Best regards,
    Peter

    #1006541

    Hi dude,

    Unfortunately, these two code snippets doesn’t work: I have a blank page with both of them.

    I looked up the code in productslider.php (in the folder productslider) and found these lines in the file (at line 679):

    if( ! empty( $terms ) )
    			{
    				$tax_query[] =  array( 	
    									'taxonomy' 	=>	$params['taxonomy'],
    									'field' 	=>	'id',
    									'terms' 	=>	$terms,
    									'operator' 	=>	'IN'
    							);
    			}			
    			
    			$query = array(
    							'post_type'				=>	$params['post_type'],
    							'post_status'			=>	'publish',
    							'ignore_sticky_posts'	=>	1,
    							'paged'					=>	$page,
    							'offset'            	=>	$params['offset'],
    							'post__not_in'			=>	( !empty( $no_duplicates ) ) ? $avia_config['posts_on_current_page'] : array(),
    							'posts_per_page'		=>	$params['items'],
    							'orderby'				=>	$ordering_args['orderby'],
    							'order'					=>	$ordering_args['order'],
    							'meta_query'			=>	$meta_query,
    							'tax_query'				=>	$tax_query
    												);
    			
    			
    			if ( isset( $ordering_args['meta_key'] ) ) 
    			{
    	 			$query['meta_key'] = $ordering_args['meta_key'];
    	 		}
    
    			$query = apply_filters( 'avia_product_slide_query', $query, $params );
    			query_posts( $query );
    			
    		    // store the queried post ids in
                if( have_posts() )
                {
                    while( have_posts() )
                    {
                        the_post();
                        $avia_config['posts_on_current_page'][] = get_the_ID();
                    }
                }

    How can I pass the ID of the current product in the post__not_in of the $query array? Maybe this could work,

    Best regards,

    #1006869

    Hi,

    Thanks for the update.

    Try to use the “avia_product_slide_query” filter.

    add_filter('avia_product_slide_query', 'avia_product_slide_query_mod', 10, 2);
    function avia_product_slide_query_mod($params, $grid, $test) {
            global $product;
            $id = $product->get_id();
            if( is_singular('product') ) {
    	     $params['post__not_in'] = array($id);
            }
    	return $params;
    }

    Best regards,
    Ismael

    #1007021

    Hi Ismael,

    This doesn’t work either. This time, I have a grey screen. I looked in the console, but nothing. You can see my page with this screenshoot. I also tried to deactivate all my plugins except WooCommerce to see if there is a conflict but there is none.

    Best regards,

    • This reply was modified 5 years, 10 months ago by dimitrilpc.
    #1007724

    Hi,

    Thanks for the update. Please provide a link to the actual product and the login details in the private field. We would like to check it.

    Best regards,
    Ismael

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.