最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Woocommerce show cross sells on singe product page

matteradmin10PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 7 years ago.

Improve this question

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' ); ?> 
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 7 years ago.

Improve this question

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' ); ?> 
Share Improve this question asked Jun 22, 2017 at 11:32 Alex GoglAlex Gogl 1711 gold badge1 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12
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();
}

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;
}
Post a comment

comment list (0)

  1. No comments so far