Tagged: Product Grid, Product page, product slider, related products
-
AuthorPosts
-
September 4, 2018 at 2:19 pm #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,September 4, 2018 at 2:33 pm #1005580Hey 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,
DudeSeptember 5, 2018 at 3:22 pm #1006174Hi 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,
September 6, 2018 at 9:28 am #1006516Hi,
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,
PeterSeptember 6, 2018 at 11:15 am #1006541Hi 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,
September 7, 2018 at 3:02 am #1006869Hi,
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,
IsmaelSeptember 7, 2018 at 10:22 am #1007021Hi 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 6 years, 3 months ago by dimitrilpc.
September 10, 2018 at 2:02 am #1007724 -
AuthorPosts
- You must be logged in to reply to this topic.