how to display custom fields
-
I need to display both the unit price and the regular price (this would be the price per case) with the product. I created unit_price and units_per_case fields using WC Field Factory. This works fine, but I want to display the unit_price AND the regular_price like this:
$.99 per unit
$495 per case
500 units/caseI know I need to modify the woocommerce_get_price_html by creating a function and adding a filter and I’ve copied code to do this and put it in my child theme functions.php file. but it doesn’t work. It doesn’t seem as if the code is running at all. I have spent hours looking through forums and tried countless ways to fix it, but no luck. i have to be doing something very wrong. any ideas?
here is my code:
function cw_case_unit_price_display( $price, $product ) {
$unit_price = get_post_meta( $product->id, ‘unit_price’, true );
if ( ! empty( $unit_price ) ) {
$price = ‘<span class=”amount”>’ . wc_price( $unit_price ) . ‘ per unit<br>’ . $price . ‘ per case</span>’;
}
else
$price = ‘1’; // added to see if code works at all…..doesn’t ;(return apply_filters(‘woocommerce_get_price’,$price);
}add_filter(‘woocommerce_get_price_html’, ‘cw_case_unit_price_display’, 10, 2);
- The topic ‘how to display custom fields’ is closed to new replies.