首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将woocommerce产品类别缩略图替换为相应类别中最新产品的图像

将woocommerce产品类别缩略图替换为相应类别中最新产品的图像
EN

Stack Overflow用户
提问于 2019-12-04 11:13:41
回答 2查看 818关注 0票数 0

我试图更新woocommerce中的类别列表/循环,以将类别缩略图替换为相应类别中最新可用产品的缩略图。

虽然我可以列出每个类别的最新产品,并在图像下面显示类别名称,但我无法让它链接到类别页面,但我认为我在测试页面上实现了这一目标的一半。这是运行在一个测试页,虽然我希望它工作在商店登陆页。

另外,由于某些原因,我无法将它显示为普通的woocommerce产品网格。尽管我复制了woocommerce类,但所有的产品/类别都只是堆栈。

这是密码。我是不是走了很长一段路才能达到这个目标呢?

代码语言:javascript
复制
<?php
    if (is_page( 2295 )){ ?>
    //open 
    <div class='woocommerce'>

        <?php

        //parameters for the function
        $args = array(
    'number'     => $number,
    'orderby'    => 'title',
    'order'      => 'ASC',
    'hide_empty' => $hide_empty,
    'include'    => $ids
);

//fetch the product categories
$product_categories = get_terms( 'product_cat', $args );

//how many categories
$count = count($product_categories);

// Check categories exist
if ( $count > 0 ){

    //loop through and minipulate
    foreach ( $product_categories as $product_category ) {
        //parameters for the query
        $args = array(
            'posts_per_page' => 1,
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',                    
                    'terms' => $product_category->slug
                )
            ),
            'post_type' => 'product',
            'orderby' => 'title,'
        );

        //run the query with the above arguments
        $products = new WP_Query( $args );
        //open the list with woocommerce class
        echo "<ul class='products columns-4'>";
        //list latest products of each category 
        while ( $products->have_posts() ) {
            $products->the_post();
            ?>
                <li class="product-category product">
                    <a href="<?php get_site_url() ?>/product-category/<?php echo $product_category->slug; ?>">
                        <?php the_post_thumbnail('shop_catalog'); ?>
                        <?php echo"<h2 class'woocommerce-loop-category__title'>" . $product_category->name . "</h2>"; ?>
                    </a>
                </li>
            <?php
        }
        echo "</ul>";

    }
}?>

</div>

   <?php }
    ?>

这方面的任何帮助都会很麻烦的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-05 13:09:08

在活动主题的functions.php中添加以下代码片段以实现上述目标-

代码语言:javascript
复制
function modify_woocommerce_before_shop_loop() {
    remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10 );
    add_action( 'woocommerce_before_subcategory_title', 'change_woocommerce_subcategory_thumbnail', 10 );
}
add_action( 'woocommerce_before_shop_loop', 'modify_woocommerce_before_shop_loop' );

function change_woocommerce_subcategory_thumbnail( $category ) {
    global $wpdb;
    $small_thumbnail_size = apply_filters( 'subcategory_archive_thumbnail_size', 'woocommerce_thumbnail' );
    $dimensions           = wc_get_image_size( $small_thumbnail_size );
    // Get the latest product from the category
    $product = $wpdb->get_row("SELECT 
        ID FROM $wpdb->posts p
        JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
        JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
        JOIN $wpdb->terms t ON (tt.term_id = t.term_id)
        WHERE p.post_type='product'
        AND p.post_status = 'publish'
        AND tt.taxonomy = 'product_cat'
        AND t.term_id = $category->term_id
        ORDER BY post_date DESC LIMIT 1");
    if( $product ) {
        $thumbnail_id         = get_post_meta( $product->ID, '_thumbnail_id', true );
    }else {
        $thumbnail_id         = get_term_meta( $category->term_id, 'thumbnail_id', true );
    }

    if ( $thumbnail_id ) {
        $image        = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
        $image        = $image[0];
        $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
        $image_sizes  = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
    } else {
        $image        = wc_placeholder_img_src();
        $image_srcset = false;
        $image_sizes  = false;
    }

    if ( $image ) {
        // Prevent esc_url from breaking spaces in urls for image embeds.
        // Ref: https://core.trac.wordpress.org/ticket/23605.
        $image = str_replace( ' ', '%20', $image );

        // Add responsive image markup if available.
        if ( $image_srcset && $image_sizes ) {
            echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
        } else {
            echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2019-12-07 04:09:46

如果我还想检查该产品是否有库存,我已经尝试过将查询升级到类似的东西,尽管看起来像是弄坏了什么:

代码语言:javascript
复制
$product = $wpdb->get_row("SELECT 
        ID FROM $wpdb->posts p
        JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
        JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
        JOIN $wpdb->terms t ON (tt.term_id = t.term_id)
        JOIN $wpdb->wp_postmeta pm ON (p.ID = pm.post_id)
        WHERE p.post_type='product'
        AND p.post_status = 'publish'
        AND tt.taxonomy = 'product_cat'
        AND t.term_id = $category->term_id
        AND pm.meta_value = 'instock'
        ORDER BY post_date DESC LIMIT 1");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59174703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档