Echo the Price from mp_product_price(); function

How do I extract the price from mp_product_price(); function? I'm trying to extract the price so to use it on a entirely customised HTML code, without any additional tags, I only need to extract the price as text. Is there anyway of doing that?

  • Patrik

    This is what I did. It's slightly clunky, but it works.

    <?php
    $fullstring = mp_product_price(false, $postid, '');
    function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string,$start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
    }

    $price = get_string_between($fullstring, '<del class="mp_old_price">', '</del>');
    $price_sale = get_string_between($fullstring, '<div itemprop="price" class="mp_current_price">', '</div>');
    ?>

    This only works for when the price also has a Sale-price activated. I'm currently working on a (somewhat big) solution to fetch the correct data in all circumstances. I can paste that code when I'm done. But I figured this might be what you're looking for.

    I'm super late to the party of course, but I'm a bit annoyed that I have to do it this way since there are no ready-made functions to merely pull tiny pieces of data such as the price (!!) and sale-price (!!) from a product. It's tremendously annoying, actually..

  • Patrik

    Ok so... I have solved it by thinking a little... :slight_smile:

    <?php
    $meta = get_post_custom($postid);
    $normal_price = maybe_unserialize($meta['mp_price'][0]);
    $sale_price = maybe_unserialize($meta['mp_sale_price'][0]);
    $for_sale = maybe_unserialize($meta['mp_is_sale'][0]);
    $variations = maybe_unserialize($meta['mp_var_name'][0]);

    echo 'normal price: ' . $normal_price[0] . '
    '; // do a foreach loop instead to get all.
    echo 'sale price: ' . $sale_price[0] . '
    '; // do a foreach loop instead to get all.
    echo 'for sale? ' . $for_sale . '
    '; // returns true/false.
    echo 'variations ' . $variations[0] . '
    '; // do a foreach loop instead to get all.
    ?>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.