skip to Main Content

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.

2

Answers


  1. // 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.

    Login or Signup to reply.
  2. 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 &ndash; %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>';
    }
    
    // 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search