[英]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.