0

I found a post for adding units of measure after a price in woocomerce sites. It works wonderfully for me. Adding unit amount after WooCommerce price.

I want to add a default value since 95% of my products are all sq ft. Im hoping this will make it so all products show sq ft, but the few products I change to new values will have the new value displayed.

Edit Here is what I ended up using to accomplish this.

//add default of sq sf
add_filter('woocommerce_get_price_html','wpse103469_add_price_per_unit_meta_default');
function wpse103469_add_price_per_unit_meta_default( $price ) {
 $default_units =  __( 'sq. ft.', 'myplugin_textdomain' );
 $units = get_post_meta( get_the_ID(), 'wc_price_per_unit_key', true );
 return ( $units ) ? $price .= ' ' . $units : $price .= ' ' . $default_units;
}
2

Here's a tweaked version of the referenced wpse103469_add_price_per_unit_meta_to_price() function. If no units have been specified, the default units are appended to the price, otherwise the user-specified units are used.

add_filter('woocommerce_get_price_html', 'wpse103469_add_price_per_unit_meta_to_price');
function wpse103469_add_price_per_unit_meta_to_price( $price ) {
     $default_units =  __( 'sq. ft.', 'myplugin_textdomain' );
     $units = get_post_meta( get_the_ID(), 'wc_price_per_unit_key', true );

     return ( $units ) ? $price .= ' ' . $units : $price .= ' ' . $default_units;
}
  • Thank you very much. After a slight edit, I was able to make that work. – Joshua Baer Sep 7 '16 at 3:22
  • Glad that worked, and welcome to WPSE! Can you please click the accept button if you feel that this solution solved your issue? – Dave Romsey Sep 7 '16 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.