客戶想顯示他們總共在他們的商店有產品量的,我已經使用這個代碼Woocommerce - 只顯示產品庫存
$numposts = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish'");
,工作正常,但它顯示的總人數我希望它只顯示庫存在1或更大的位置,因此基本上它只顯示實際庫存中的產品總數
客戶想顯示他們總共在他們的商店有產品量的,我已經使用這個代碼Woocommerce - 只顯示產品庫存
$numposts = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish'");
,工作正常,但它顯示的總人數我希望它只顯示庫存在1或更大的位置,因此基本上它只顯示實際庫存中的產品總數
嘗試使用_stock_status
meta_key加入post_meta表,其中meta_value是'instock'。建議緩存數據,因爲您不希望在每個請求上運行此操作,但需要平衡適當的時間量以緩存數據(因爲高速緩存期內的銷售額不會反映在庫內總數中)。僅在使用緩存時纔有效(由於查詢次數,WooCommerce強烈建議使用緩存)。
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";
太棒了!我建議使用'wp_cache_get()'和'wp_cache_add()'作爲這個值,所以你不要對每個請求運行SQL查詢。 – doublesharp
抱歉原諒我的無知,但你怎麼做到這一點? – user3593556
計算所有股票的產品,你必須加入其他表'wp_postmeta'與現場'_sale_price'這是greator比'0' –
對不起,你需要跟我說話像個孩子,我該怎麼辦去做? – user3593556
http://stackoverflow.com/questions/20990199/woocommerce-display-only-on-sale-products-in-shop –