Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce php code get_price_html()

I am new to WordPress and WooCommerce, I believe I have identified the line of code that is producing the output I want changed.

I am using free artificer theme from WooCommerce and the index.php has a line:

<h3>
    <?php the_title(); ?>
    <span class="price">
        <?php echo $_product->get_price_html(); ?>
    </span>
</h3>

This produces something like "Black Stone - $43" (i.e. product title - price)

I want something like "Black Stone
$43"
(i.e. product title <br/> price)

It looks like there are some filters for the ``get_price_html()` function, but the documentation is not very good or I just don't understand how to navigate through it.

Any direction would be appreciated.
Thanks.

like image 668
user2612580 Avatar asked Oct 13 '13 14:10

user2612580


2 Answers

all the $product->get_price_html(); produces something like this:

<del><span class="amount">£8.00</span>–<span class="amount">£9.00</span></del>
<ins><span class="amount">£7.00</span>–<span class="amount">£8.00</span></ins>

to manipulate this data, you must extract it from this string

If you use WP filters - you will change get_price_html() output everywhere and if you need to change get_price_html() output just in one place, you should do next:

global $product;

$price_html = $product->get_price_html();

$price_html_array = price_array($price_html);

function price_array($price){
    $del = array('<span class="amount">', '</span>','<del>','<ins>');
    $price = str_replace($del, '', $price);
    $price = str_replace('</del>', '|', $price);
    $price = str_replace('</ins>', '|', $price);
    $price_arr = explode('|', $price);
    $price_arr = array_filter($price_arr);
    return $price_arr;
}

now you have same data in array

Array ( [0] => £8.00–£9.00 [1] => £7.00–£8.00 )

and you can do with it everything you want

to apply global filter, you must add

add_filter( 'woocommerce_get_price_html', 'price_array', 100, 2 );
like image 192
Alexander Ivashchenko Avatar answered Sep 19 '22 12:09

Alexander Ivashchenko


This is probably the filter you're looking for:

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
    $price = '';
    $price .= woocommerce_price($product->min_variation_price);
    return $price;
}

This just changes it so that the min price is displayed (and nothing else) as it's not clear how you want to format/style it. You can access various other details via the $product object to customise the output. Use it within your functions.php file.

like image 26
Alex Avatar answered Sep 19 '22 12:09

Alex