Skip to content
Advertisement

PHP return value formatting

Could you please help me out how i would be able to customize the output of the below code which was added to wordpress/woocommerce?

Currently it shows the sales price and the regular price, and i’d like to add some customization for each of them, like: coloring (sales price=red); text-decoration (sales price=line-through)..

function sr_change_variable_price($price, $product) {
    if ( $product->is_type( 'variable' ) && !is_product() ) 
    {
     return $product->get_variation_regular_price( 'min' ).' '.$product->get_variation_sale_price( 'min' ).' Ft'; // if variable product and non-product page
    } else if ( $product->is_type( 'variable' ) && is_product() )
    {
        return '';  // if variable product and product page
    } else
    {
        return $price;  // other cases
    }
}
add_filter( 'woocommerce_get_price_html', 'sr_change_variable_price', 10, 2 );

Thank you very much in advance for any help,

Advertisement

Answer

in your function you concating two strings in first condition, in second condition returned an empty string and in else statement returned that self price without change. so as your return type is string there is no problem for merging your prices with another strings such as html tags. if this process don’t make problem for another processes of your wordpress, you can use html tags by concating them in your returns:

function sr_change_variable_price($price, $product){
    $html_tag = '<span style={some styles}>%s</span>';
    if($product->is_type('variable') AND !is_product()){
        $min = $product->get_variation_regular_price('min') . ' ' . $product->get_variation_sale_price('min');
        $min = $min ' Ft';
        return(sprintf($html_tag, $min));
    }elseif($product->is_type('variable') AND is_product()){
        return('');
    }else{
        return(sprintf($html_tag, $price));
    }
}
add_filter('woocommerce_get_price_html', 'sr_change_variable_price', 10, 2);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement