Woocommerce - 仅显示库存产品 [英] Woocommerce - Showing only products in stock

查看:27
本文介绍了Woocommerce - 仅显示库存产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户想显示他们店里的产品总数,我使用了这个代码

<前>$numposts = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish'");

效果很好,但是它显示了已发布产品的总数,我希望它只显示库存为 1 或更高的位置,因此基本上它只显示实际有库存的产品总数

解决方案

尝试使用 _stock_status 元键加入 post_meta 表,其中元值是 'instock'.建议缓存数据,因为您不想在每个请求上都运行它,但需要平衡适当的时间来缓存数据(因为缓存期内的销售额不会反映在库存商品总数中).仅当您使用缓存时才有效(由于查询的数量,强烈建议在 WooCommerce 中使用缓存).

全局$wpdb;//缓存键$key = 'in_stock_products';//如果缓存中没有任何内容,我们将返回 'false',否则为 0+$in_stock_products = wp_cache_get( $key );//如果我们没有从缓存中获取它,则运行查询if ( false === $in_stock_products ){//创建 SQL 查询(HEREDOC 格式)$sql_query = <<<SQLSELECT COUNT(p.ID) AS in_stock_productsFROM {$wpdb->posts} p加入 {$wpdb->postmeta} 下午ON p.ID = pm.post_idAND pm.meta_key = '_stock_status'AND pm.meta_value = 'instock'WHERE p.post_type = 'product' AND p.post_status = 'publish'SQL;//查询数据库$in_stock_products = (int) $wpdb->get_var( $sql_query );//缓存结果,选择合适的时间缓存结果$cache_ttl = 0;//0 是尽可能长",否则缓存时间以秒为单位wp_cache_add( $key, $in_stock_products, $cache_ttl );//缓存尽可能长}//$in_stock_products 现在具有来自缓存或数据库查询的值.echo "库存中有 $in_stock_products";

a client want to show a total of the amount of products they have in their shop, I have used this code


$numposts = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish'");

Which works fine, however it shows the total number of published products and I want it to only show where stock is at 1 or greater, so basically it only shows the total number of products that are in actually in stock

解决方案

Try joining to the post_meta table using the _stock_status meta_key where the meta_value is 'instock'. Caching the data is recommended since you don't want to run this on every request but will need to balance an appropriate amount of time to cache the data (since sales within the cache period won't be reflected in the total number of instock items). Only works if you are using a cache (which is highly recommended with WooCommerce due to the number of queries).

global $wpdb;
// cache key
$key = 'in_stock_products';
// we get back 'false' if there is nothing in the cache, otherwise 0+
$in_stock_products = wp_cache_get( $key );
// run the query if we didn't get it from the cache
if ( false === $in_stock_products ){
    // create the SQL query (HEREDOC format)
    $sql_query = <<<SQL
    SELECT COUNT(p.ID) AS in_stock_products
    FROM {$wpdb->posts} p
    JOIN {$wpdb->postmeta} pm 
        ON p.ID = pm.post_id 
        AND pm.meta_key = '_stock_status' 
        AND pm.meta_value = 'instock'
    WHERE p.post_type = 'product' AND p.post_status = 'publish'
SQL;
    // query the database
    $in_stock_products = (int) $wpdb->get_var( $sql_query );
    // cache the results, choosing an appropriate amount of time to cache results
    $cache_ttl = 0; // 0 is "as long as possible", otherwise cache time in seconds
    wp_cache_add( $key, $in_stock_products, $cache_ttl ); // cache as long as possible
}
// $in_stock_products now has the value either from the cache or the database query.
echo "There are $in_stock_products in stock";

这篇关于Woocommerce - 仅显示库存产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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