Amount saved as a shortcode
The Advanced Dynamic Pricing for WooCommerce plugin allows you to display detailed discount information anywhere on your site using shortcodes. One such shortcode is [adp_amount_saved], which shows the total amount the customer saves based on the active pricing rules. This can be a powerful tool for increasing conversion rates by highlighting the value customers receive from your dynamic pricing rules.
This guide provides the complete code snippet for registering the [adp_amount_saved] shortcode, explains how it works, and shows you how to customise it for your store.
The Complete Code Snippet
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * Register the [adp_amount_saved] shortcode. * * Displays the total amount saved based on active pricing rules. * The amount is formatted according to your WooCommerce currency settings. */ add_shortcode( 'adp_amount_saved', 'adp_amount_saved_shortcode_callback' ); function adp_amount_saved_shortcode_callback() { // Include tax in the saved amount calculation $includeTax = true; // Access the necessary plugin classes $customizer = ADPFactory::get( "CustomizerExtensions_CustomizerExtensions" ); $discountMessage = ADPFactory::get( "Advertising_DiscountMessage", $customizer ); // Get the formatted amount saved $amountSaved = $discountMessage->getAmountSaved( $includeTax ); return $amountSaved; } |
What the code does:
| Line / Element | Explanation |
|---|---|
add_shortcode( 'adp_amount_saved', ... ) | Registers a new shortcode [adp_amount_saved]with WordPress, so you can use it in posts, pages, widgets, or theme templates. |
$includeTax = true; | Determines whether the saved amount includes tax. Set to true to display the total saved (including tax) or false for the subtotal saved (excluding tax). |
ADPFactory::get( "CustomizerExtensions_CustomizerExtensions" ); | Retrieves the plugin’s customizer extension class, which holds display settings for discount messages. |
ADPFactory::get( "Advertising_DiscountMessage", $customizer ); | Loads the discount message handler. The $customizer parameter passes any customiser settings to the message formatter. |
$discountMessage->getAmountSaved( $includeTax ); | Calculates the total discount amount saved by the customer, formatted with the store’s currency. |
return $amountSaved; | Returns the formatted string, which WordPress then renders in place of the shortcode. |
How to Use the Shortcode
Once you have added the code to your site (see Section 4), you can use the shortcode in several places.
- In a post or page – Simply type
[adp_amount_saved]into the WordPress editor. - In a widget – Add a Shortcode widget to any sidebar or footer area and enter
[adp_amount_saved]. - In a theme template – Use
<?php echo do_shortcode( '[adp_amount_saved]' ); ?>inside your theme’s PHP files.
Example output:
- When the cart qualifies for a 15.00discount,theshortcodedisplays:‘15.00
- When the cart qualifies for a 10% discount on a 200.00subtotal,theshortcodedisplays:‘20.00
Note: The shortcode returns the saved amount only when a pricing rule is active and applicable to the current cart. If no discount applies, it returns an empty string or
0, depending on your plugin configuration.
Customisation Options
The shortcode callback can be modified to suit your specific display requirements.
Excluding Tax from the Saved Amount
If you prefer to show the saved amount excluding tax, change the $includeTax variable to false:
|
1 |
$includeTax = false; |
Adding a Prefix or Suffix
You can wrap the returned amount with custom text. For example, to show “You saved: $15.00”:
|
1 |
return 'You saved: ' . $amountSaved; |
To show “Total savings: $15.00 🎉”:
|
1 |
return 'Total savings: ' . $amountSaved . ' 🎉'; |
Displaying Only When a Discount Applies
If you want the shortcode to output nothing when no discount is active, add a conditional check:
|
1 2 3 4 |
if ( empty( $amountSaved ) || $amountSaved == 0 ) { return ''; } return $amountSaved; |
Changing the Number of Decimal Places
The saved amount is formatted automatically using WooCommerce’s currency settings. To change the number of decimal places, use PHP’s number_format function:
|
1 2 |
$formattedAmount = number_format( (float) $amountSaved, 0, '.', ',' ); return $formattedAmount; |
This example would display $15 instead of $15.00.
Step‑by‑Step Implementation
Follow these steps to add the custom code to your WordPress site.
Step 1: Choose where to add the code
You have two safe options for adding custom PHP code:
- Option A (Recommended): Use the free Code Snippets plugin. This is the safest method. Install it from Plugins → Add New by searching for “Code Snippets”. It allows you to add, activate, and deactivate code snippets without ever editing your theme files.
- Option B: Add to your child theme’s
functions.phpfile. If you are comfortable editing theme files, add the code to your active child theme’sfunctions.phpfile. Never add custom code directly to a parent theme, because it will be lost when the theme is updated.
Step 2: Insert the snippet
- If using Code Snippets:
- If editing
functions.php:
Step 3: Test the shortcode
- Add a product to your cart that triggers a pricing rule (e.g., bulk discount, BOGO, role‑based price).
- Create a new test page or edit an existing one.
- Enter
[adp_amount_saved]into the content editor. - Publish or preview the page. The shortcode should display the total discount amount saved.
If the shortcode displays the correct amount, the implementation is successful.