Woocommerce show cross sells on singe product page

admin2025-06-06  0

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;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749209371a317277.html

最新回复(0)