I need to override Schema ‘availability’ option to Preorder for Woocommerce (3.9.2) products on backorder. Currently WC generates ‘InStock’ for them. Which filters and how do I apply? My current code (below) ruins WordPress, seems like filter is wrong:
function tt_WC_change_schema_availability( $availability ) {
if ( is_product() && ! is_admin() ) {
$stock_status = $product->is_in_stock();
$output = 'OutOfStock'; // default, out of stock
if ($stock_status){
$output = 'InStock';
$qty = $product->get_stock_quantity();
if ( ! ($qty > 0) ) {
$output = 'PreOrder';
}
}
return 'http://schema.org/' . $output;
}
}
add_filter( 'woocommerce_structured_data_product_offer', 'tt_WC_change_schema_availability' );
2
Answers
Many thanks to @Howard E! I've found out that $stock_status returns a string, also added extra check if product stock's managed. Here's working function:
It looks like you are close, but the $product object also has to be included in your function. It’s the second parameter that can be passed to this filter. Also, you want to return part of the array that gets sent to the
woocommerce_structured_data_product_offer
filter. This would be$availability['availability']
since you’re passing the variable as$availability