Reputation: 101
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.
Upvotes: 9
Views: 77608
Reputation: 1
I made this function based in source code.
function extractPrices($product, $currency = "BRL")
{
if($product->is_type( 'simple' )) {
return (object) ["regular_price" => $product->regular_price, "sale_price" => $product->sale_price];
}
$prices = $product->get_variation_prices( true );
if (!empty( $prices['price'])) {
$min_price = current( $prices['price'] );
$max_price = end( $prices['price'] );
$min_reg_price = current( $prices['regular_price'] );
$max_reg_price = end( $prices['regular_price'] );
if ( $min_price !== $max_price ) {
return (object) ["regular_price" => $max_price, "sale_price" => $min_price];
} elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
return (object) ["regular_price" => $max_reg_price, "sale_price" => $min_price];
} else {
return (object) ["regular_price" => $max_price, "sale_price" => $min_price];
}
}
return (object) ["regular_price" => null, "sale_price" => null];
}
And another function to get off discount
function calculeOff($prices): ?int
{
if(!empty($prices->sale_price) && !empty($prices->regular_price)) {
return round(100 - ($prices->sale_price / $prices->regular_price * 100));
}
return null;
}
Upvotes: 0
Reputation: 10809
You can achieve this by modifying the said line and adding a small css code in theme custom.css
file.
Replace the given code by this:
<h3>
<?php the_title(); ?>
<br/>
<span class="price">
<?php echo $_product->get_price_html(); ?>
</span>
</h3>
And add the following css code either in custom.css
(recommended) file or in last line of theme style.css
file.
ul.featured-products li h3 .price::before{
content : '' !important;
}
Please Note: The above code is tested and working fine with Artificer version 1.3.16 (which was released on 05 May, 2016.)
Hope this helps!
Upvotes: 2
Reputation: 868
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 );
Upvotes: 14
Reputation: 1609
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.
Upvotes: 5