【问题标题】:how to get woocommerce best selling product include product-image and etc... with SQL query?如何使用 SQL 查询获得 woocommerce 最畅销的产品,包括产品图像等...?
【发布时间】:2019-12-24 09:37:54
【问题描述】:

我需要在这里从 woocommerce 获得畅销产品

SELECT * FROM wp_posts  
INNER JOIN wp_postmeta 
ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 
AND ( wp_postmeta.meta_key = 'total_sales' ) 
AND wp_posts.post_type = 'product' 
AND (wp_posts.post_status = 'publish') 
GROUP BY wp_posts.ID 
ORDER BY wp_postmeta.meta_value+0 
DESC, wp_posts.post_date ASC LIMIT 0, 16

我尝试使用“SELECT TO”,但 mysql 不支持它也不支持“IN”!

【问题讨论】:

标签: mysql wordpress woocommerce


【解决方案1】:

这是我的解决方案:

 function best_selling_products(){
        global $wpdb;
// first of all I get IDs of the bestselling product with this query
        $query="SELECT wp_posts.id FROM wp_post

s INNER JOIN wp_postmeta 
ON ( wp_posts.ID = wp_postmeta.post_id ) 
WHERE 1=1 AND ( wp_postmeta.meta_key = 'total_sales' ) 
AND wp_posts.post_type = 'product' 
AND (wp_posts.post_status = 'publish') 
GROUP BY wp_posts.ID 
ORDER BY wp_postmeta.meta_value+0 DESC, wp_posts.post_date ASC LIMIT 0, 16";

//then i used that get whatever that I wand from wp_get_product with product id
    $prdtc=$wpdb->get_results($query);
    $array=array();
    $i=0;
foreach ($prdtc as $id)
 {
        $res = wc_get_product( $id->id);
                $array[$i]=array(
    $id->id,
    $res->get_name(),
    $res->get_price(),
    get_the_post_thumbnail_url($id->id),
    get_permalink($id->id)
);
            $i++;
        }
            echo json_encode($array,JSON_FORCE_OBJECT);
        }

这就是我希望它对某人有用的全部内容。

【讨论】:

    【解决方案2】:

    您可以使用此代码在循环中检索最畅销的产品。

    <?php
        $args = array(
            'post_type' => 'product',
            'meta_key' => 'total_sales',
            'orderby' => 'meta_value_num',
            'posts_per_page' => -1,
        );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); 
        global $product; 
    ?>
    <div>
    
    <a href="<?php the_permalink(); ?>" id="id-<?php the_id(); ?>" title="<?php the_title(); ?>">
    
    <?php 
    if (has_post_thumbnail( $loop->post->ID )) 
    echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); 
    else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="65px" height="115px" />'; 
    ?>
    <h3><?php the_title(); ?></h3>
    </a>
    </div>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    

    【讨论】:

    • 实际上我需要这个来制作一个 REST API,这段代码让我有点困惑!
    • 仅使用下面的代码并在 $loop 中获取一个对象并根据您的需要使用。 $args = array('post_type' => 'product', 'meta_key' => 'total_sales', 'orderby' => 'meta_value_num', 'posts_per_page' => -1, ); $loop = new WP_Query($args)
    • 是的,它让我最畅销,但我需要更多数据,例如 url 和产品图片
    • 如果您借助 id 获取数据中的 Id,您可以轻松获取产品图片和 url,例如: ID); ?>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多