add_action( 'woocommerce_after_shop_loop_item', 'wc_loop_get_product_stock_availability_text', 10 );
function wc_loop_get_product_stock_availability_text() {
global $wpdb, $product;
$max_stock_qty = 50; // Maximum Number of Available Stock qty
// For variable products
if( $product->is_type('variable') ) {
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish' AND p.post_parent = '".get_the_id()."'
AND pm.meta_key = '_stock' AND pm.meta_value IS NOT NULL
");
if ( $stock_quantity > 0 ) {
// Here we limit the sock quantity display to 50 when it's up to 50
$stock_quantity = $stock_quantity >= $max_stock_qty ? $max_stock_qty : $stock_quantity;
echo '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
echo '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
return;
}
}
// Other products types
else {
echo wc_get_stock_html( $product );
}
}
1条答案
按热度按时间mv1qrgav1#
对于可变产品,当袜子数量超过**
50
时,以下将限制袜子数量显示为50
**:字符串
代码放在你的活动子主题(或活动主题)的functions.php文件中。它应该可以工作。
相关:在Woocommerce存档页面中显示所有产品类型的库存可用性