简体   繁体   English

显示 WooCommerce 档案中产品的缺货状态,特定元数据除外

[英]Display Out of Stock status on products in WooCommerce archives except for specific metadata

I'm using the following to display stock status on WooCommerce archives pages:我正在使用以下内容在 WooCommerce 档案页面上显示库存状态:

<?php
//add action give it the name of our function to run
add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
//create our function
function wcs_stock_text_shop_page() {
    //returns an array with 2 items availability and class for CSS
    global $product;
    $availability = $product->get_availability();
    //check if availability in the array = string 'Out of Stock'
    //if so display on page.//if you want to display the 'in stock' messages as well just leave out this, == 'Out of stock'
    if ( $availability['availability'] == 'Out of stock') {
        echo apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</p>', $availability['availability'] );
    }
 
}

Does anyone know how I could edit the above code to exclude products with the meta _ywpo_preorder , please?有谁知道我如何编辑上面的代码以排除具有 meta _ywpo_preorder的产品吗?

You can check about that metadata in the postmeta table with the product id and if it exists you exit function.您可以使用产品 ID 在 postmeta 表中检查该元数据,如果它存在,则退出 function。

<?php
    //add action give it the name of our function to run
    add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
    //create our function
    function wcs_stock_text_shop_page() {
        //returns an array with 2 items availability and class for CSS
        global $product;

        $meta = get_post_meta($product->get_ID(), '_ywpo_preorder', true);
        if($meta)
        {
            return;
        }

        $availability = $product->get_availability();
        //check if availability in the array = string 'Out of Stock'
        //if so display on page.//if you want to display the 'in stock' messages as well just leave out this, == 'Out of stock'
        if ( $availability['availability'] == 'Out of stock') {
            echo apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</p>', $availability['availability'] );
        }
     
    }

暂无
暂无

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

相关问题 显示特定类别的缺货产品 - Woocommerce - Display out of stock products on specific category - Woocommerce 根据Woocommerce中的订单数将所有产品状态设置为“无货” - Set all products status “out of stock” based on orders count in Woocommerce 隐藏 WooCommerce 目录中具有特定库存状态的所有产品 - Hide all products with a specific stock status from WooCommerce catalog 最后显示缺货产品(即使在排序后) - Woocommerce - Display out of stock products last (even after sort) - Woocommerce 在 Woocommerce 档案中为可变产品设置最大显示可用库存数量 - Set a max displayed available stock quantity on Woocommerce archives for variable products Woocommerce /如何按库存状态分类相关产品? - Woocommerce / how to sort related products by stock status? 如何在woocommerce中分离库存产品和缺货产品 - How to separate stock products and out of stock products in woocommerce 在 WOOCOMMERCE 中更新价格、状态库存、发布状态表单 CSV(更多 20k 产品)(内存耗尽 3000 产品) - Update price, status stock, post status form CSV (more 20k products) in WOOCOMMERCE (Memory runs out on 3000 products) 如果 Woocommerce 商店和档案缺货,请删除产品按钮 - Remove product button if out of stock from Woocommerce shop and archives 当 WooCommerce 档案中的产品缺货时删除自定义数量字段 - Remove custom quantity field when product out of stock on WooCommerce archives
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM