[英]Get featured products in Woocommerce 3
I want to add Featured products for upsell (or Recent Products OR Best Selling Products) in New order Email template.我想在新订单电子邮件模板中添加用于追加销售的特色产品(或最近的产品或畅销产品)。 It works like Upsell with email marketing.
它的工作原理类似于 Upsell 与电子邮件营销。 How do I add it to woocommerce email template so that there is a section in the end of the email which shows my featured/Recent/best selling products.
如何将其添加到 woocommerce 电子邮件模板,以便电子邮件末尾有一个部分显示我的特色/最近/最畅销产品。 I tried using this code in my New order Email template but nothing works.
我尝试在我的新订单电子邮件模板中使用此代码,但没有任何效果。 I use all latest version of wordpress n 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
Since Woocommerce 3, the products following properties:从 Woocommerce 3 开始,产品具有以下特性:
are now stored like a post term under 'product_visibility'
taxonomy , for better performances.现在存储为
'product_visibility'
分类法下的帖子术语,以获得更好的性能。 So old postmeta data doesn't work anymore.所以旧的 postmeta 数据不再起作用了。
To make your code working you need instead to make a tax_query
this way:要使您的代码正常工作,您需要以这种方式进行
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
}
This code goes on function.php file of your active child theme (or theme).此代码位于活动子主题(或主题)的 function.php 文件中。
Tested and works.测试和工作。
Updated related to your comments…
与您的评论相关的更新…
Added the code in a function that is called via a hooked function in "New order" email notification.
在通过“新订单”电子邮件通知中的挂钩函数调用的函数中添加了代码。
To get the product image use:
$product->get_image( 'shop_thumbnail' );
要获取产品图片,请使用:
$product->get_image( 'shop_thumbnail' );
To get the product link use :$product->get_permalink();
要获取产品链接,请使用:
$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.