在商店页面上显示“缺货”字符串,但不显示库存数量。

时间:2016-07-14 18:47:05

标签: php wordpress woocommerce product inventory

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

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

此代码在商店页面上显示“缺货”通知,其中所有产品均显示如下:

enter image description here

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

enter image description here

我不想要这个。

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

提前致谢!

1 个答案:

答案 0 :(得分:2)

关于您的代码的说明:

  1. 在第一部分(如果)显示库存数量,只要产品有库存(即使using the working tricks of this answer甚至是商店页面)
  2. 第二部分(其他)正在显示'缺货'字符串并删除“添加到购物车”按钮(因为产品缺货)。
  3. 使用下面的代码,现在我们有:

    • !is_shop() 的附加(if)条件,只要产品是库存且不在商店页面,就会显示库存数量。
    • 商店页面的其他(其他),退出该功能而不显示库存数量。
    • 和你原封不动的最终条件(就像之前一样)。

    因此,根据需要,这是一个功能齐全的解决方案:

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

    如果您只想在单个商品页面上显示库存数量,并且只在商店页面中显示“缺货”,那么这将是您的代码:

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

    现在,如果您只是不想显示任何库存数量,那么您的代码将是这样的:

    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 - Remove the available product inventory number from the shop page

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

    代码会显示您的活动子主题或主题的 function.php 文件...

相关问题