繁体   English   中英

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

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

我正在使用以下内容在 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'] );
    }
 
}

有谁知道我如何编辑上面的代码以排除具有 meta _ywpo_preorder的产品吗?

您可以使用产品 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM