Created
October 12, 2018 18:49
-
-
Save djrmom/6e7e531128c912f99539925290da4549 to your computer and use it in GitHub Desktop.
facetwp woocommerce layout builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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