jeudi 20 août 2020

Woocommerce Remove Price If Price is Not Set

Based on the answer to How to display woocommerce sale price or regular price if there is no sale price, I have a product loop that displays the product price with a "$" sign before the price. The problem is that products without prices still show "$".

Based on the answers to Using AND/OR in if else PHP statement and wordpress/woocommerce: Remove price if zero, I tried to add an additional if condition using && but can't get it to work:

In functions.php:

function get_regular_or_sale_price() {
    global $product;
    if ( $product->price && $product->is_on_sale() ) {
        return '$'.$product->get_sale_price();
    }
    return '$'.$product->get_regular_price();
}

function get_regular_price_if_sale() {
    global $product;
    if ( $product->price && $product->is_on_sale() ) {        return '$'.$product->get_regular_price();
    }
    return '$'.$product->get_regular_price();
}

Aucun commentaire:

Enregistrer un commentaire