Just use the $product
parameter to get the custom field value, and determine what to return:
function custom_price_html( $price, $product ) {
if ( ( int ) get_post_meta( $product->id, 'price_per_person', true ) )
$price .= ' per person';
elseif ( ( int ) get_post_meta( $product->id, 'price_per_group', true ) )
$price .= ' per group';
$price .= '[filtered]'; // For debugging - if you don't see this next to your prices, the filter isn't even running, hence why it's not working!
return $price;
}
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 500, 2 );
This is assuming that you save each “checked” state under its own meta key, and that if a state is not checked, the field does not exist or is “empty”.
It might actually make more sense to use one meta field price_type
, and then save a value of per_person
or per_group
depending on the checked state (since a price can’t actually be both types, can it?).
Update: To debug the situation, place this in your functions.php
, view the product in your browser, then update your question with the output:
function wpse_183901_debug_product() {
if ( is_singular( 'product' ) ) {
echo '<pre>';
echo esc_html( print_r( get_post_meta( get_queried_object_id() ), true ) );
echo '</pre>';
exit;
}
}
add_action( 'template_redirect', 'wpse_183901_debug_product' );