0

I'm trying to show after the price of my product, whether the price is 'per person' or 'per group'.

I've created a custom field for both, so for example, if custom field 'price_per_person' is ticked, price would display as $100 per person or if custom field 'price_per_person' is ticked, price would display as $100 per group

I've started with the filter below, but all the queries I have done to identify whether the custom field is selected have not worked.

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
    $price = $price . ' per person';
    return apply_filters( 'woocommerce_get_price', $price );
}

Thanks!

Update with Output

Array
(
    [_edit_last] => Array
        (
            [0] => 1
        )

    [_edit_lock] => Array
        (
            [0] => 1428754721:1
        )

    [_visibility] => Array
        (
            [0] => visible
        )

    [_stock_status] => Array
        (
            [0] => instock
        )

    [total_sales] => Array
        (
            [0] => 0
        )

    [_downloadable] => Array
        (
            [0] => no
        )

    [_virtual] => Array
        (
            [0] => no
        )

    [_regular_price] => Array
        (
            [0] => 79
        )

    [_sale_price] => Array
        (
            [0] => 
        )

    [_purchase_note] => Array
        (
            [0] => 
        )

    [_featured] => Array
        (
            [0] => no
        )

    [_weight] => Array
        (
            [0] => 
        )

    [_length] => Array
        (
            [0] => 
        )

    [_width] => Array
        (
            [0] => 
        )

    [_height] => Array
        (
            [0] => 
        )

    [_sku] => Array
        (
            [0] => 
        )

    [_product_attributes] => Array
        (
            [0] => a:0:{}
        )

    [_sale_price_dates_from] => Array
        (
            [0] => 
        )

    [_sale_price_dates_to] => Array
        (
            [0] => 
        )

    [_price] => Array
        (
            [0] => 79
        )

    [_sold_individually] => Array
        (
            [0] => 
        )

    [_manage_stock] => Array
        (
            [0] => no
        )

    [_backorders] => Array
        (
            [0] => no
        )

    [_stock] => Array
        (
            [0] => 
        )

    [_upsell_ids] => Array
        (
            [0] => a:0:{}
        )

    [_crosssell_ids] => Array
        (
            [0] => a:0:{}
        )

    [_product_image_gallery] => Array
        (
            [0] => 
        )

    [slide_template] => Array
        (
            [0] => 
        )

    [_thumbnail_id] => Array
        (
            [0] => 63
        )

    [_wpb_vc_js_status] => Array
        (
            [0] => false
        )

    [_wpb_vc_js_interface_version] => Array
        (
            [0] => 0
        )

    [price_per_person] => Array
        (
            [0] => 1
        )

    [_price_per_person] => Array
        (
            [0] => field_5528e5f80bad0
        )

    [price_per_group] => Array
        (
            [0] => 0
        )

    [_price_per_group] => Array
        (
            [0] => field_5528e6580bad1
        )

)
0

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' );
  • I have checked under one of the two states, but it doesn't display any result. Yes, in theory there only needs to be one meta field... how would I structure the query? Thanks – emailsforben Apr 11 '15 at 10:42
  • You need to first confirm that your checked states are being correctly saved in the database, and under which keys are they? – TheDeadMedic Apr 11 '15 at 10:56
  • The keys are both checkboxes 1. is price_per_person 2. is price_per_group Field group is 'Pricing Type' if that helps? – emailsforben Apr 11 '15 at 11:11
  • So you're using ACF? – TheDeadMedic Apr 11 '15 at 11:17
  • I need to know how exactly you've configured it. Are you using "True / False", or "Checkbox" (and if the latter, what option values). – TheDeadMedic Apr 11 '15 at 11:20

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.