[英]Replace woocommerce product category thumbnail with image of latest product in respective category
I'm trying to update the category list/loop in woocommerce to replace the category thumbnail with a thumbnail of the newest available product in the respective category.我正在尝试更新 woocommerce 中的类别列表/循环,以将类别缩略图替换为相应类别中最新可用产品的缩略图。
I think I'm halfway to achieve this on a test page though I can list the latest product of each category and display the category name beneath the image though I can't get it to link to the category page.我想我已经在测试页面上实现了这一目标,尽管我可以列出每个类别的最新产品并在图像下方显示类别名称,但我无法将其链接到类别页面。 This is running on a test page though I would like it to work on the shop landing page.这是在测试页面上运行的,但我希望它在商店登录页面上运行。
Also for some reason I can't get this to display as the regular woocommerce product grid.同样出于某种原因,我无法将其显示为常规的 woocommerce 产品网格。 All the products/categories just stack even though I have replicated the woocommerce classes.即使我复制了 woocommerce 类,所有产品/类别也只是堆叠。
Here is the code.这是代码。 Am I going the long way around is there an easier way to achieve this?我会走很长的路吗?有没有更简单的方法来实现这一目标?
<?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 }
?>
Any help with this this would be awasome对此的任何帮助都会很棒
Add the follows code snippet in your active theme's functions.php to achieve the above -在您的活动主题的functions.php 中添加以下代码片段以实现上述目的 -
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'] ) . '" />';
}
}
}
If I also want to check if the product is in stock or not I have tried upating the query to something like this though looks to have broken something:如果我还想检查产品是否有库存,我已经尝试将查询升级为类似的内容,尽管看起来已经损坏了一些东西:
$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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.