在商店页面上显示“缺货"字符串,但不显示库存数量. [英] Displaying the 'out of stock' string on shop page but not the stock quantity.

查看:80
本文介绍了在商店页面上显示“缺货"字符串,但不显示库存数量.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 WordPress/WooCommerce 网站的 functions.php 文件中使用了这段代码:

I'm using this piece of code in my functions.php file of my WordPress/WooCommerce site:

function envy_stock_catalog() {
    global $product;
    if ( $product->is_in_stock() ) {
        echo $product->get_stock_quantity() ;
    } else {
        echo '<div class="out-of-stock" >' . __( 'out of stock', 'envy' ) . '</div>';
                add_action('init','remove_loop_button');
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );

此代码在商店页面上显示缺货"通知,所有产品都显示如下:

This code displays the 'out of stock' notice on the shop page where all products are displayed like this:

问题在于它还会导致产品标题旁边的产品可用库存数量如下:

The issue is that it also causes of the available inventory number of the products next to product titles like this:

我不想要这个.

我的问题:
如何更改我的代码以仍然显示'缺货'通知(当产品缺货时)而不是库存产品可用性> 在商店页面上(在添加到购物车按钮的左侧)?

My question:
How I can change my code to still display the 'out of stock' notice (when product is out of stock) and not the inventory product availability on shop page (in left side of add-to-cart button)?

提前致谢!

推荐答案

关于你的代码的说明:

  1. 在第一部分(如果)显示库存数量,只要产品有库存(甚至 使用这个答案,甚至是商店页面)
  2. 第二部分(其他)显示'out of stock' 字符串并删除添加到购物车按钮(因为产品缺货).
  1. In first part (if) was displaying stock quantity as long as product was in stock (even using the working tricks of this answer and even for shop page)
  2. Part two (else) was displaying 'out of stock' string and removing add-to-cart button (as the product was out stock).

<小时>

有了下面的代码,现在我们有:


With the code below, now we have:

  • 带有 !is_shop() 的附加 (if) 条件,只要产品有库存且不在商店页面中,就会显示库存数量.
  • 商店页面的附加(其他)功能,在不显示库存数量的情况下退出该功能.
  • 以及您未触及的最终 else 条件(就像以前一样).

这里是功能齐全的解决方案,如您所愿:

So here is the fully functional solution, as desired:

add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );
function envy_stock_catalog() {
    global $product;
    if ( $product->is_in_stock() ) { // If product is in stock

        if ( !is_shop() ) { // If is NOT Shop page

            // Displays the stock quantity
            echo '<span class="qty">' . $product->get_stock_quantity() . '<span>';
        } else { // If is shop page (and product is in stock)
            return; // Don't display stock quantity (and removes nothing).
        }
    // If product is NOT in stock
    } else {
        // Display 'out of stock' string
        echo '<div class="out-of-stock" >' . __( 'out of stock', 'envy' ) . '</div>';

        // Removes "add to cart" button
        add_action('init','remove_loop_button');
    }
}

<小时>

如果您只想在单个产品页面上显示库存数量,而仅在商店页面显示缺货",这将是您的代码:


If you just want to display stock quantity on single product page, and 'out of stock' only in shop page, this will be your code:

add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );
function envy_stock_catalog() {
    global $product;
    // If product is in stock but is not shop page
    if ( $product->is_in_stock() && !is_shop() ) { 

        // Displays the stock quantity
        echo '<span class="qty">' . $product->get_stock_quantity() . '<span>';
    // If product is NOT in stock and is shop page
    } elseif ( !$product->is_in_stock() && is_shop() ) {
        // Display 'out of stock' string
        echo '<div class="out-of-stock" >' . __( 'out of stock', 'envy' ) . '</div>';

        // Removes "add to cart" button
        add_action('init','remove_loop_button');
    }
    // other cases goes out the function
    return;
}

<小时>

现在如果您不想显示任何库存数量,您的代码将如下所示:


Now if you just don't want to display any stock quantity, your code will be like this:

add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );
function envy_stock_catalog() {
    global $product;
    if ( $product->is_in_stock() ) { // If product is in stock
        return; // Don't display stock quantity (and removes nothing).

    // If product is NOT in stock
    } else {
        // Display 'out of stock' string
        echo '<div class="out-of-stock" >' . __( 'out of stock', 'envy' ) . '</div>';

        // Removes "add to cart" button
        add_action('init','remove_loop_button');
    }
}

在这种情况下,您可以使用此答案中的所有工作技巧:
Woocommerce - 删除可用产品来自商店页面的库存编号

And in this case you could use all the working tricks from this answer:
Woocommerce - Remove the available product inventory number from the shop page

所有代码都经过测试并且运行良好.

代码位于您的活动子主题或主题的 function.php 文件中......

这篇关于在商店页面上显示“缺货"字符串,但不显示库存数量.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆