<?php
/** layout builder output for woocommerce custom fields **/
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 0 === strpos( $value, 'woo/' ) ) {
$field_key = substr( $value, 4 );
if ( 'price' == $field_key ) {
$product = wc_get_product( get_the_ID() );
$price = $product->get_price_html();
$value = str_replace( $item['source'], $price, $value );
} elseif ( 'sale_price' == $field_key ) {
$product = wc_get_product( get_the_ID() );
$price = $product->get_sale_price();
if ( $price > 0 ) {
$value = str_replace( $item['source'], wc_price( $price ) . $product->get_price_suffix(), $value );
} else {
$value = str_replace( $item['source'], '', $value );
}
} elseif ( 'regular_price' == $field_key ) {
$product = wc_get_product( get_the_ID() );
$price = $product->get_regular_price();
if ( $price > 0 ) {
$value = str_replace( $item['source'], wc_price( $price ) . $product->get_price_suffix(), $value );
} else {
$value = str_replace( $item['source'], '', $value );
}
} elseif ( 'average_rating' == $field_key ) {
$product = wc_get_product( get_the_ID() );
$rating = $product->get_average_rating();
if ( $rating !== 0 ) {
$value = str_replace( $item['source'], wc_get_rating_html( $rating ), $value );
} else {
$value = str_replace( $item['source'], '', $value );
}
} elseif ( 'stock_status' == $field_key ) {
$product = wc_get_product( get_the_ID() );
$value = str_replace( $item['source'], wc_get_stock_html( $product ), $value ); }
}
/** TODO
** product_type, on_sale - not sure why or how you would use these
**/
return $value;
}, 10, 2 );