在 Woocommerce 3 中获取特色产品 [英] Get featured products in Woocommerce 3

查看:45
本文介绍了在 Woocommerce 3 中获取特色产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在新订单电子邮件模板中添加用于追加销售的特色产品(或近期产品或畅销产品).它的工作原理类似于 Upsell 与电子邮件营销.如何将其添加到 woocommerce 电子邮件模板,以便电子邮件末尾有一个部分显示我的特色/最近/最畅销产品.我尝试在我的新订单电子邮件模板中使用此代码,但没有任何效果.我使用所有最新版本的 wordpress n woocommerce.

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. 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. I tried using this code in my New order Email template but nothing works. I use all latest version of 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 以来,产品具有以下特性:

Since Woocommerce 3, the products following properties:

  • 特色",
  • 库存状态",
  • 目录可见性"
  • 评级系统"

现在存储为 'product_visibility' 分类 下的一个帖子,以获得更好的性能.所以旧的 postmeta 数据不再起作用了.

are now stored like a post term under 'product_visibility' taxonomy, for better performances. So old postmeta data doesn't work anymore.

要使您的代码正常工作,您需要以这种方式创建 tax_query:

To make your code working you need instead to make a tax_query this way:

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 文件中.

This code goes on function.php file of your active child theme (or theme).

经过测试并有效.

已根据您的评论更新...

Updated related to your comments…

在新订单"中通过挂钩函数调用的函数中添加了代码;电子邮件通知.

Added the code in a function that is called via a hooked function in "New order" email notification.

要获取产品图片,请使用:$product->get_image('shop_thumbnail');
要获取产品链接,请使用:$product->get_permalink();

To get the product image use: $product->get_image( 'shop_thumbnail' );
To get the product link use : $product->get_permalink();

这篇关于在 Woocommerce 3 中获取特色产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆