WooCommerce在单一产品页面上展示交叉销售

时间:2017-06-22 作者:Alex Gogl

我试图在单个产品页面而不是购物车中显示交叉销售:

到目前为止,我已经尝试了以下代码:

<?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 -->
然而,这只会在upsells下面显示upsells,所以它只是两次相同的内容。我不确定用什么动作来代替

do_action( \'woocommerce_after_single_product_summary_upsell_display\' ); ?> 

2 个回复
SO网友:itzmekhokan
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();
}
SO网友:Alpesh Rathod

找到此代码并将其删除

1: 使用“\\u crosssell\\u ids”元键获取交叉销售产品的ID。

<?php

/* crossells */

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

?>
通过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;
}

结束

相关推荐