【问题标题】:Get featured products in Woocommerce 3在 Woocommerce 3 中获取特色产品
【发布时间】:2020-04-08 03:04:07
【问题描述】:

我想在新订单电子邮件模板中添加特色产品以进行追加销售(或近期产品或畅销产品)。它与电子邮件营销的追加销售一样。如何将其添加到 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

【问题讨论】:

    标签: php wordpress woocommerce product


    【解决方案1】:

    自 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 文件中。

    经过测试并且有效。

    与您的 cmets 相关的更新...

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

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

    【讨论】:

    • 非常感谢,我可以将这段代码添加到 theme-child/woocommerce/emails/admin-new-order.php 中吗
    • 谢谢你,我想要它在新订单电子邮件模板的末尾,像这样 - ibb.co/mKBk5n 它就像一个追加销售策略
    • 谢谢你,在我得到解决方案后,我肯定会接受 n 点赞。我试图在模板中添加代码但没有运气,请检查这个请ibb.co/bLZYrS
    • 非常感谢,它已经在管理员电子邮件模板中很好地添加了产品,我想将其添加到客户新订单模板中,以便只有客户才能获得它。顺便说一句,代码正在运行。
    【解决方案2】:
    <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-->
    

    【讨论】:

      猜你喜欢
      • 2018-02-25
      • 2015-12-26
      • 2019-01-26
      • 2013-02-22
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多