0

I came across a weird issue, not sure whether it's a bug or something is wrong with my code.

Woocommerce allows to show cross-sale products only in the cart by default, so I had to make a custom loop to display them on product pages.

So I made a custom product tab for the product page that shows cross-sale products (up-sales or related products will not fit the requirements for that). it's all working fine except the Reviews tab. If I place it after the tab with cross-sales it shows wrong reviews (which seem like random). The reviews counter (the one that is next to the tab's name) is correct, but the list of reviews contains reviews of other products and not the current one. If the tab of reviews is before my cross-sales tab then it's fine. So I guess something in my code is breaking it, but I can't figure out what...

Here's the code that I run in my cross-sales tab:

$crosssell_ids = get_post_meta( get_the_ID(), '_crosssell_ids' ); 
$crosssell_ids = $crosssell_ids[0];

if(count($crosssell_ids) > 0) {
    $crosssell_args = array( 'post_type' => 'product', 'posts_per_page' => 8, 'post__in' => $crosssell_ids );
    $crosssell_loop = new WP_Query( $crosssell_args );
    echo '<div class="products"><div class="tabs-title">Consumables for <br>&laquo;' . get_the_title($product->id) . '&raquo;</div>';
    while ( $crosssell_loop->have_posts() ) {
        $crosssell_loop->the_post();
        wc_get_template_part( 'content', 'product' );
    };
    echo '</div>';
}

I gave my loop a custom name to avoid any conflicts, so it should overwrite any variables that are used by reviews... I also tried to buffer the template inside the while loop and output buffered variable, but it didn't solve the problem.

The problem is that I'm required to put reviews after this tab by design, so I can't just leave it with reviews before (it's also because the first tab with product description should also contain a couple of these cross-sale products).

And here is my function for adding custom tabs and setting their priority:

// Custom tabs for product page
add_filter( 'woocommerce_product_tabs', 'my_product_tabs' );
function my_product_tabs( $tabs ) {
    // Add new product tabs for product page
    $detailed_info = get_post_meta( get_the_ID(), 'detail_info', true );
    if( !empty($detail_info) ) {
        $tabs['detail_info'] = array(
            'title'     => __( 'Detail info', 'woocommerce' ),
            'callback'  => 'my_tabs_detail_info_html'
        );
    }
    $crosssell_ids = get_post_meta( get_the_ID(), '_crosssell_ids' ); 
    $crosssell_ids = $crosssell_ids[0];
    if( !empty($crosssell_ids) ) {
        $tabs['consumables'] = array(
            'title'     => __( 'Consumables', 'woocommerce' ),
            'callback'  => 'my_tabs_consumables_html'
        );
    }
    $tabs['delivery_methods'] = array(
        'title'     => __( 'Delivery Methods', 'woocommerce' ),
        'callback'  => 'my_tabs_delivery_methods_html'
    );

    // Edit default product tabs for product page
    unset( $tabs['additional_information'] );

    // Tabs order
    if ( isset($tabs['description']) ) $tabs['description']['priority'] = 10;
    if ( isset($tabs['detail_info']) ) $tabs['detail_info']['priority'] = 15;
    if ( isset($tabs['consumables']) ) $tabs['consumables']['priority'] = 20;
    $tabs['reviews']['priority'] = 25;
    if ( isset($tabs['delivery_methods']) ) $tabs['delivery_methods']['priority'] = 30;
    return $tabs;
}
  • Looks like it's $crosssell_loop->the_post(); line that's causing the issue... – Dmitriy Demir Sep 7 '16 at 11:39
0

I found the solution which is probably quite basic for experienced WP developers. I'm gonna post it here instead of deleting the question though, I hope this might help someone someday...

The issue was that the post data wasn't reset, so all I needed is to add a function call wp_reset_postdata(); after my while loop. Like so:

$crosssell_ids = get_post_meta( get_the_ID(), '_crosssell_ids' ); 
$crosssell_ids = $crosssell_ids[0];

if(count($crosssell_ids) > 0) {
    $crosssell_args = array( 'post_type' => 'product', 'posts_per_page' => 8, 'post__in' => $crosssell_ids );
    $crosssell_loop = new WP_Query( $crosssell_args );
    echo '<div class="products"><div class="tabs-title">Consumables for <br>&laquo;' . get_the_title($product->id) . '&raquo;</div>';
    while ( $crosssell_loop->have_posts() ) {
        $crosssell_loop->the_post();
        wc_get_template_part( 'content', 'product' );
    };
    wp_reset_postdata();
    echo '</div>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.