php WooCommerce特色产品

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WooCommerce特色产品相关的知识,希望对你有一定的参考价值。

<?php

add_shortcode( 'woo_featured', 'wb_woo_featured' );
/*
 *
 * Featured Product Loop
 */
function wb_woo_featured() {

$args = array(  
	'post_type'      => 'product',  
	'posts_per_page' => 3,
	'post_status'    => 'publish',
	'tax_query'      => array(
		array(
			'taxonomy' => 'product_visibility',
			'field'    => 'name',
			'terms'    => 'featured',
			'operator' => 'IN'
		),  
	),
);  
$featured_product = new WP_Query( $args );  

if ( $featured_product->have_posts() ) :  

ob_start();

echo '<div class="woocommerce columns-3"><ul class="products">';
while ( $featured_product->have_posts() ) : $featured_product->the_post();

$product               = wc_get_product( $featured_product->post->ID );  
$post_thumbnail_id     = get_post_thumbnail_id();
$product_thumbnail     = wp_get_attachment_image_src($post_thumbnail_id, $size = 'shop-feature');
$product_thumbnail_alt = get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true );

// Featured Post Loop Output 
//	wc_get_template_part( 'content', 'product' ); 
?>
	
	<li class="product">
		<a href="<?php the_permalink();?>">
			<img src="<?php echo $product_thumbnail[0];?>" alt="<?php echo $product_thumbnail_alt;?>">
			<h3 class="woocommerce-loop-product__title"><?php the_title();?></h3>
			<button class="yellow-but">VIEW PRODUCT</button>
		</a>    
	</li>

<?php

endwhile;  
echo '</ul></div>';
endif;  
wp_reset_query();

return ob_get_clean();
}

<?php
$args = array(
	'posts_per_page' => -1,
	'post_type'      => 'product',
	'post_status'    => 'publish',
	'tax_query'      => array(
		array(
			'taxonomy' => 'product_visibility',
			'field'    => 'name',
			'terms'    => 'featured',
			'operator' => 'IN',
			),
		)  
);
$featured_product = new WP_Query( $args );

if ( $featured_product->have_posts() ) : 

echo '<div class="woocommerce columns-3"><ul class="products">';

while ( $featured_product->have_posts() ) : $featured_product->the_post();

$post_thumbnail_id     = get_post_thumbnail_id();
$product_thumbnail     = wp_get_attachment_image_src($post_thumbnail_id, $size = 'shop-feature');
$product_thumbnail_alt = get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true );
?>

	<li class="product">
		<a href="<?php the_permalink();?>">
			<img src="<?php echo $product_thumbnail[0];?>" alt="<?php echo $product_thumbnail_alt;?>">
			<h3 class="woocommerce-loop-product__title"><?php the_title();?></h3>
			<button class="yellow-but">VIEW PRODUCT</button>
		</a>    
	</li>
<?php 
endwhile; 

echo '</ul></div>';

endif; 
		
 wp_reset_query();
 ?>          
<!-- Featured products loop -->  

以上是关于php WooCommerce特色产品的主要内容,如果未能解决你的问题,请参考以下文章

将自定义产品分类更改为 WooCommerce 产品分类

从 Woocommerce 相关产品中排除特定产品类别

获取 WooCommerce 中的所有产品类别

Woocommerce 产品标签(显示其他产品)

在单个产品页面上增加相关产品(woocommerce)

将 WooCommerce 自定义产品属性转换为定义的产品属性