7

I am trying to show cross sells on a single product page rather than in the cart:

So far I've tried the following code:

<?php do_action( 'woocommerce_after_single_product_summary_data_tabs' ); ?>

                                    <?php if ( $product->get_upsell_ids() ) : ?>
                                        <div class="single_product_summary_upsell">
                                            <?php do_action( 'woocommerce_after_single_product_summary_upsell_display' ); ?>
                                        </div><!-- .single_product_summary_upsells -->
                                    <?php endif; ?>

                  <?php if ( $product->get_cross_sell_ids() ) : ?>
                                        <div class="single_product_summary_upsell">
                                            <?php do_action( 'woocommerce_after_single_product_summary_upsell_display' ); ?>
                                        </div><!-- .single_product_summary_upsells -->
                                    <?php endif; ?>

                  <div class="single_product_summary_related">
                                        <?php do_action( 'woocommerce_after_single_product_summary_related_products' ); ?>
                                    </div><!-- .single_product_summary_related -->


                            </div><!-- .columns -->

However this will only show upsells below upsells, so it's just the same content twice. I am not sure on what action to use instead of

do_action( 'woocommerce_after_single_product_summary_upsell_display' ); ?> 

2 Answers 2

11
add_action('woocommerce_after_single_product_summary', 'show_cross_sell_in_single_product', 30);
function show_cross_sell_in_single_product(){
    $crosssells = get_post_meta( get_the_ID(), '_crosssell_ids',true);

    if(empty($crosssells)){
        return;
    }

    $args = array( 
        'post_type' => 'product', 
        'posts_per_page' => -1, 
        'post__in' => $crosssells 
        );
    $products = new WP_Query( $args );
    if( $products->have_posts() ) : 
        echo '<div class="cross-sells"><h2>Cross-Sells Products</h2>';
        woocommerce_product_loop_start();
        while ( $products->have_posts() ) : $products->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile; // end of the loop.
        woocommerce_product_loop_end();
        echo '</div>';
    endif;
    wp_reset_postdata();
}
2
  • That code is onto something, I like it far better than customizing a theme. Just need to make some modifications on the styles and it should work. Is there any way without absolute declarations of classes? Like without the <div class="..."></div> part?
    – Alex Gogl
    Jun 24, 2017 at 10:07
  • 1
    This only returns one cross sell item, since get_post_meta() is set for $single = true
    – Nathan
    Sep 13, 2017 at 20:06
1

find this code and remove it

1: get the ids of the cross sell products using the ‘_crosssell_ids’ meta key.

<?php

/* crossells */

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

?>
  1. Loop through the products by id

if(count($crosssell_ids)>0){
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'post__in' => $crosssell_ids );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?><a href='<?php the_permalink(); ?>'><?php
the_post_thumbnail( 'thumbnail' );
the_title();
?></a><?php
endwhile;
}
1
  • Just tried it out, the problem with this code is that it doesn't really utilize the themes design
    – Alex Gogl
    Jun 22, 2017 at 11:56

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