在 Woocommerce 存档页面中显示所有产品类型的库存可用性

2021-12-22 00:00:00 product php wordpress woocommerce stock

我在显示产品库存时使用此代码:

 add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 10 );函数 display_variable_product_stock_quantity(){wc_get_variable_product_stock_quantity('echo_html');}函数 show_stock() {全球$产品;if ( $product->stock ) {//如果管理库存已启用如果 ( !$product->managing_stock() && !$product->is_in_stock() )回声'';}if ( number_format($product->stock,0,'','') > 0 ) {//如果库存不足echo '<div class="remainingpc" style="text-align:center;"><font color="red">' .number_format($product->stock,0,'','') .' 左 Pcs

';}别的 {echo '<div class="remaining" style="text-align:center;"><font color="red">缺货</font></div>';}}add_action('woocommerce_after_shop_loop_item','show_stock', 10);

如果产品是变量,我会使用此答案代码来显示库存可用性:
获取Woocommerce 中可变产品的所有变体的总库存

如何将这些代码合并到一个条件函数中?

例如.如果产品是简单产品,则不会显示可变产品的其他代码.

解决方案

以下将处理 woocommerce 存档产品页面中所有产品类型的库存可用性显示为商店.

要处理除变量之外的其他产品类型的库存可用性显示,您可以使用专用函数 wc_get_stock_html() 代替,这将简化代码.

add_action( 'woocommerce_after_shop_loop_item', 'wc_loop_get_product_stock_availability_text', 10 );函数 wc_loop_get_product_stock_availability_text() {全球 $wpdb, $product;//对于可变产品if( $product->is_type('变量') ) {//获取所有产品变体(子项)的库存数量总和$stock_quantity = $wpdb->get_var("SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}posts as pJOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_idWHERE p.post_type = 'product_variation'AND p.post_status = '发布' AND p.post_parent = '".get_the_id()."'AND pm.meta_key = '_stock' AND pm.meta_value 不为空");如果( $stock_quantity > 0 ){echo '<p class="stock in-stock">'.sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';} 别的 {如果 ( is_numeric($stock_quantity) )echo '<p class="库存缺货">'.__("缺货", "woocommerce") .'</p>';别的返回;}}//其他产品类型别的 {回声 wc_get_stock_html( $product );}}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

I am using this code in showing the stocks of products:

    add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 10 );
function display_variable_product_stock_quantity(){
    wc_get_variable_product_stock_quantity( 'echo_html' );
} 

function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled 
if ( ! $product->managing_stock() && ! $product->is_in_stock() )
        echo '';
}
if ( number_format($product->stock,0,'','') > 0 ) { // if stock is low
echo '<div class="remainingpc" style="text-align:center;"><font color="red"> ' . number_format($product->stock,0,'','') . ' Pcs Left</font></div>';
} 
else {
echo '<div class="remaining" style="text-align:center;"><font color="red">Out of Stock</font></div>'; 
}
}

add_action('woocommerce_after_shop_loop_item','show_stock', 10);

And if the product is a variable I use this answer code to display the stock availability:
Get the total stock of all variations from a variable product In Woocommerce

How can I merge this codes in a single conditional function?

For example. if the products is a simple product, the other code for variable product will not display.

解决方案

The following will handle the display of the stock availability for all product types in woocommerce archive product pages as shop.

To handle the stock availability display for other product types than variable, you can use the dedicated function wc_get_stock_html() instead, which will simplify the code.

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;

    // 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 ) {
            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 );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

相关文章