简体   繁体   English

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

[英]Display the stock availability for all product types in Woocommerce archive pages

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 在 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.以下将处理 woocommerce 存档产品页面中所有产品类型的库存可用性显示为商店。

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.要处理除变量之外的其他产品类型的库存可用性显示,您可以改用专用函数wc_get_stock_html() ,这将简化代码。

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).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce存档页面中显示特定产品属性 - Display specific product attribute in Woocommerce archive pages 在Woocommerce 3中检查产品库存可用性 - Checking product stock availability in Woocommerce 3 在 Woocommerce 存档页面中的产品标题下显示特定的产品属性 - Display specific product attributes under product title in Woocommerce archive pages 在 Woocommerce 中更改产品库存可用性文本 - Change product stock availability texts in Woocommerce WooCommerce:在普通页面或帖子上显示给定产品 ID 的库存数量 - WooCommerce: Display stock quantity for a given product ID on normal Pages or Posts 在 WooCommerce 存档页面上显示视频而不是产品缩略图 - Display video instead of product thumbnail on WooCommerce archive pages 在 WooCommerce 存档页面中的产品标题下方显示 ACF 字段/图像 - Display ACF field/image below the product title in WooCommerce archive pages 在 WooCommerce 产品存档页面上显示自定义分类术语 - Display custom taxonomy terms on WooCommerce product archive pages 显示Woocommerce存档页面中变体的大小产品属性值 - Display size product attribute values for variations in Woocommerce archive pages 仅在 Woocommerce 存档页面上显示可变产品折扣百分比 - Display variable product discounted percentage only on Woocommerce archive pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM