在产品库下显示 woocommerce 相关产品

display woocommerce related products under product gallery

我从单个产品中删除了相关产品

remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );

现在我想在产品图片库下显示相关产品。这是产品库代码。

/*******Single Product ********/
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );

add_action( 'woocommerce_product_thumbnails', 'show_product_thumbnails', 20 );
add_action( 'woocommerce_before_single_product_summary', 'show_product_images', 10 );

function show_product_thumbnails(){
    global $post, $product, $woocommerce;

    $attachment_ids = $product->get_gallery_attachment_ids();
    $count_gallery  = count($attachment_ids);

    echo '<div class="thumbnails" id="gallery">';

    if ( $attachment_ids ) {
        echo '<div class="main-slider" style="width:85%; float:right;position: relative;">'
            echo '<div class="slider slider-for">';
                foreach ( $attachment_ids as $attachment_id ) {
                    $totem_single_image = wp_get_attachment_image_src( $attachment_id, 'shop_single', true );
                    $image_link         = $totem_single_image[0];
                    $main_image_link    = wp_get_attachment_url( $attachment_id );
                    $image_title        = esc_attr( get_the_title( $attachment_id ) );
                    $image              = wp_get_attachment_image( 
                        $attachment_id, 
                        apply_filters( 'single_product_small_thumbnail_size', 'shop_single' ),
                        0,
                        $attr = array(
                            'title' => $image_title,
                            'alt'   => $image_title
                        )
                    );

                    echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '<a href="%s" class="elevatezoom-gallery" title="%s"><img class="lily_zoom" src="%s" alt="%s" data-zoom-image="%s"></a>', $main_image_link, $image_caption, $image_link, $image_title, $main_image_link), $attachment_id, $post->ID );  
                }
            echo '</div>';
        echo'</div>';

        echo '<div class="pagination" style="width:13%; display:block;">';
            echo '<div class="slider slider-nav" role="toolbar">';
                foreach ( $attachment_ids as $attachment_id ) {
                    $totem_single_image = wp_get_attachment_image_src( $attachment_id, 'medium', true );
                    $image_link         = $totem_single_image[0];
                    $main_image_link    = wp_get_attachment_url( $attachment_id );
                    $image_title        = esc_attr( get_the_title( $attachment_id ) );
                    $image              = wp_get_attachment_image( 
                        $attachment_id, 
                        apply_filters( 'single_product_small_thumbnail_size', 'shop_single' ), 
                        0, 
                        $attr = array(
                            'title' => $image_title,
                            'alt'   => $image_title
                        )
                    );

                    echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '<img src="%s" alt="%s"></a>', $image_link,$image_title), $attachment_id, $post->ID );  
                }
            echo '</div>';
        echo '</div>';
    }
    echo '</div>';
}

function show_product_images(){
    global $post, $woocommerce, $product;

    echo '<div class="images lily-main-image">';

        if ( has_post_thumbnail() ) {
            $image_caption   = get_post( get_post_thumbnail_id() )->post_excerpt;
            $main_image_link = wp_get_attachment_url( get_post_thumbnail_id() );
            $image           = get_the_post_thumbnail( 
                $post->ID, 
                apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), 
                array(
                    'title'           => get_the_title( get_post_thumbnail_id() ),
                    'data-zoom-image' => $main_image_link, 
                    'id'              => 'totemImgZoom'
                )
            );

            $attachment_count = count( $product->get_gallery_attachment_ids() );
            // echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img width="320px"src="%s" id="totemImgZoom" data-zoom-image="%s" title="%s" alt="">', $main_image_link, $main_image_link, $image_caption), $post->ID);   
        }
        else {
        //  echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img class="zoomImage" src="%s" alt="%s"/>', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
        }

        do_action( 'woocommerce_product_thumbnails' );
        do_action( 'woocommerce_output_related_products');

    echo '</div>'; 
}

好的,这是解决方案。

remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_output_related_products', 25);
function woocommerce_output_related_products(){
 $args = array( 
        'posts_per_page' => 4,  
        'columns' => 4,  
        'orderby' => 'rand' 
 ); 
    woocommerce_related_products( apply_filters( 'woocommerce_output_related_products_args', $args ) ); 
}