[英]Get featured products in Woocommerce 3
我想在新订单电子邮件模板中添加用于追加销售的特色产品(或最近的产品或畅销产品)。 它的工作原理类似于 Upsell 与电子邮件营销。 如何将其添加到 woocommerce 电子邮件模板,以便电子邮件末尾有一个部分显示我的特色/最近/最畅销产品。 我尝试在我的新订单电子邮件模板中使用此代码,但没有任何效果。 我使用所有最新版本的 wordpress n woocommerce。
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => 1
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) :
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
// Output product information here
endwhile;
endif;
wp_reset_query(); // Remember to reset
从 Woocommerce 3 开始,产品具有以下特性:
现在存储为'product_visibility'
分类法下的帖子术语,以获得更好的性能。 所以旧的 postmeta 数据不再起作用了。
要使您的代码正常工作,您需要以这种方式进行tax_query
:
function custom_featured_products(){
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1 ,
'tax_query' => array( array(
'taxonomy' => 'product_visibility',
'field' => 'term_id',
'terms' => 'featured',
'operator' => 'IN',
) )
) );
$featured_product_names = array();
if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post();
$product = wc_get_product( $query->post->ID );
$featured_product_names[] = $product->get_title();
endwhile; wp_reset_query();endif;
// Testing output
echo '<p>Featured products: ' . implode(', ', $featured_product_names) . '</p>';
}
// Displaying the featured products names in new order email notification
add_action ('woocommerce_email_customer_details', 'new_order_featured_products_display', 30, 4 );
function new_order_featured_products_display( $order, $sent_to_admin, $plain_text, $email ){
// Only for "New Order" email notification
if( 'new_order' != $email->id ) return;
custom_featured_products(); // calling the featured products function output
}
此代码位于活动子主题(或主题)的 function.php 文件中。
测试和工作。
与您的评论相关的更新…
在通过“新订单”电子邮件通知中的挂钩函数调用的函数中添加了代码。
要获取产品图片,请使用:
$product->get_image( 'shop_thumbnail' );
要获取产品链接,请使用:$product->get_permalink();
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>'.get_the_title().'</p>';
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.