Add multiple suffixes to WooCommerce displayed product prices

I have been trying to find a way to add a different suffix to both my retail prices and sales prices. I am going to be selling stone and I want my product page to display the following…..

$12.99 /SQFT

$9.99 /SQFT with 315 sqft (pallet) minimum purchase

When I add PHP code to my functions.php file, it displays

$0.00 /SQFT

$0.00 /SQFT with 315 sqft (pallet) minimum purchase

I have tried numerous different plugins but have not found one that will allow me to display this the way I want. The PHP code that I tried to add to my functions.php page is the following….

add_filter( 'woocommerce_get_price_html', 'custom_price_suffixes', 100, 2 );
function custom_price_suffixes( $price, $product ) {
    if ( $product->is_on_sale() ) {
        $sale_price = wc_price( $product->get_sale_price() ) . ' /SQFT with 315 sqft (pallet) minimum purchase';
        $regular_price = wc_price( $product->get_regular_price() ) . ' /SQFT';
        $price = '<del>' . $regular_price . '</del> <ins>' . $sale_price . '</ins>';
    } else {
        $price = wc_price( $product->get_price() ) . ' /sqft';
    }
    return $price;
}

That results in the $0.00. I have found through my testing that when the get_sale_price and get_regular_price are called, they display $0.00. I have tried just the get_price hook and it pulls the price. For some reason it is not pulling my pricing. My products have variants, nots sure if that is the problem? I would think there would be a way to add different suffix’s to each of the prices though.

New contributor

Stephen Williamson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

0

There are some mistakes and missing cases with your code. The following will handle your custom price suffixes for all product types and cases:

// Handle variable product formatted price range 
add_filter( 'woocommerce_format_price_range', 'custom_format_price_range_suffixes', 20, 3 );
function custom_format_price_range_suffixes( $price, $from, $to ) {
    $suffix  = ' ' . esc_html__('/SQFT');
    $suffix2 = $suffix . ' ' . esc_html__('with 315 sqft (pallet) minimum purchase');
    $from    = ( is_numeric( $from ) ? wc_price( $from ) : $from ) . $suffix;
    $to      = ( is_numeric( $to ) ? wc_price( $to ) : $to ) . $suffix2;
    
    return sprintf( _x( '%1$s – %2$s', 'Price range: from-to', 'woocommerce' ), $from, $to );
}

// Handle products with a sale price 
add_filter( 'woocommerce_format_sale_price', 'custom_format_sale_price_suffixes', 20, 3 );
function custom_format_sale_price_suffixes( $price, $regular_price, $sale_price ) {
    $formatted_regular_price = is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price;
    $formatted_sale_price    = is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price;
    $suffix  = ' ' . esc_html__('/SQFT');
    $suffix2 = $suffix . ' ' . esc_html__('with 315 sqft (pallet) minimum purchase');

    // Strikethrough pricing.
    $price = '<del aria-hidden="true">' . $formatted_regular_price . $suffix . '</del> ';

    // For accessibility (a11y) we'll also display that information to screen readers.
    $price .= '<span class="screen-reader-text">';
    // translators: %s is a product's regular price.
    $price .= esc_html( sprintf( __( 'Original price was: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_regular_price ) ) );
    $price .= $suffix . '</span>';

    // Add the sale price.
    $price .= '<ins aria-hidden="true">' . $formatted_sale_price . $suffix2 . '</ins>';

    // For accessibility (a11y) we'll also display that information to screen readers.
    $price .= '<span class="screen-reader-text">';
    // translators: %s is a product's current (sale) price.
    $price .= esc_html( sprintf( __( 'Current price is: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_sale_price ) ) );
    $price .= $suffix2 . '</span>';
    return $price;
}

// Handle price suffix for other cases
add_filter( 'woocommerce_get_price_suffix', 'custom_price_suffix', 20, 4 );
function custom_price_suffix( $suffix, $product, $price, $qty ) {
    if ( $product->is_on_sale() ) {
        return ''; // Return no suffix as prices are already suffixed
    } elseif ( $product->is_type('variable') ) {
        $prices    = $product->get_variation_prices( true );
        $min_price = current( $prices['price'] );
        $max_price = end( $prices['price'] );

        if ( ! empty($prices['price']) && $min_price !== $max_price ) {
            return ''; // Return no suffix as prices are already suffixed
        }
    }
    return ' ' . esc_html__('/SQFT') . ' ' . esc_html__('with 315 sqft (pallet) minimum purchase');
}

Code goes in functions.php file of your child theme (or in a plugin). It should better work.

0

// Add a filter to modify the price HTML in WooCommerce
add_filter( 'woocommerce_get_price_html', 'custom_price_suffixes', 100, 2 );

/**
 * Custom function to add suffixes to prices in WooCommerce
 *
 * @param string $price The original price HTML
 * @param object $product The product object
 * @return string The modified price HTML with suffixes
 */
function custom_price_suffixes( $price, $product ) {
    // Define the suffix to add to prices (e.g. "/SQFT")
    $suffix = 'SQFT';
    
    // Define the minimum purchase suffix to add to sale prices (e.g. " with 315 sqft (pallet) minimum purchase")
    $min_purchase_suffix = 'ith 315 sqft (pallet) minimum purchase';
    
    // Check if the product is on sale
    if ( $product->is_on_sale() ) {
        // Format the regular price with the suffix
        $regular_price = wc_price( $product->get_regular_price() ). $suffix;
        
        // Format the sale price with the suffix and minimum purchase suffix
        $sale_price = wc_price( $product->get_sale_price() ). $suffix. $min_purchase_suffix;
        
        // Wrap the prices in HTML del and ins tags to indicate the sale
        $price = '<del>'. $regular_price. '</del> <ins>'. $sale_price. '</ins>';
    } else {
        // If not on sale, simply add the suffix to the price
        $price = wc_price( $product->get_price() ). strtolower($suffix);
    }
    
    // Return the modified price HTML
    return $price;
}

I made a few minor corrections to the code, including adding spaces around the concatenation operators (.) for better readability.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật