Skip to content

Instantly share code, notes, and snippets.

@djrmom
Created October 12, 2018 18:49
Show Gist options
  • Save djrmom/6e7e531128c912f99539925290da4549 to your computer and use it in GitHub Desktop.
Save djrmom/6e7e531128c912f99539925290da4549 to your computer and use it in GitHub Desktop.
facetwp woocommerce layout builder
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment