简体   繁体   English

在 Woocommerce 档案中为可变产品设置最大显示可用库存数量

[英]Set a max displayed available stock quantity on Woocommerce archives for variable products

I want to set the maximum number of stock available items on Woocommerce shop pages.我想在 Woocommerce 商店页面上设置可用商品的最大数量。

I am using Display the stock availability for all product types in Woocommerce archive pages answer code, which does a great job, showing the available stock for variations in a Woocommerce shop.我正在使用显示 Woocommerce 存档页面答案代码中所有产品类型的库存可用性,它做得很好,显示了 Woocommerce 商店中变体的可用库存。

But I want to show a maximum number, say 50, if the stock is actually in excess of 50. If there are 721 of an item in stock, I just want the number to show 50 to the customer.但我想显示最大数量,比如 50,如果库存实际上超过 50。如果库存中有 721 件商品,我只想向客户显示数量 50。

I tried to add another if statement to the existing code but it simply added another line to the display, showing the actual total stock as well as my maximum number of 50, if the total was above 50.我试图在现有代码中添加另一个 if 语句,但它只是在显示中添加了另一行,显示实际总库存以及我的最大数量 50,如果总数超过 50。

The following that will limit the sock quantity display to 50 when it's over 50 , for variable products:对于可变产品,以下内容将在超过50时将袜子数量显示限制为50

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

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 It should work works.它应该工作。

Related: Display the stock availability for all product types in Woocommerce archive pages相关: 在 Woocommerce 存档页面中显示所有产品类型的库存可用性

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

相关问题 获取WooCommerce变量产品中每个活动变体的库存数量 - Get the stock quantity of each active variation in WooCommerce variable products 限制Woocommerce产品类别档案中显示的产品 - Limit displayed products on Woocommerce product category archives woocommerce-仅显示少数产品的库存数量 - woocommerce - Show stock quantity only on few products 在 WooCommerce 中显示具有正库存数量的产品 - Show products with a positive stock quantity in WooCommerce WooCommerce产品增加按库存数量排序 - Add sorting by stock quantity in WooCommerce products sort by 当 WooCommerce 档案中的产品缺货时删除自定义数量字段 - Remove custom quantity field when product out of stock on WooCommerce archives 以编程方式为 WooCommerce 中的特定变量产品设置最小、最大和步数 - Set minimum, maximum and step quantity programmatically for specific variable products in WooCommerce 显示 WooCommerce 档案中产品的缺货状态,特定元数据除外 - Display Out of Stock status on products in WooCommerce archives except for specific metadata 将 woocommerce 中的所有产品设为“缺货”(删除所有产品数量) - Make all products in woocommerce as "Out Of Stock" ( Remove all products quantity ) WooCommerce 产品变化:自动启用管理库存和设置库存数量 - WooCommerce product variations: Auto enable Manage Stock and set stock quantity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM