Add Custom Message on WooCommerce My Account Page

[September 2023]

A Tutorial with Full Code

Though there are a number of plugins available to add messages to your customer’s my account pages on WooCommerce. However, it is possible to add a message to the page directly in functions.php Or by embedding content from another page onto the my accounts page. Please note, of course you could always edit the page that includes the woocommerce_my_account shortcode, as well.

In this example, I go through the steps to add the code to your WordPress functions.php and how to embed content from a page rather than formatting it in functions.php. Lastly, we add a tab within the my accounts page to show this content rather than directly on the main account page.

Step 1:

To add a notification message to all customer accounts on the My Account page in WooCommerce, you can utilize the woocommerce_account_content filter hook. This hook allows you to add custom content to the My Account page without modifying any template files.

Here’s the PHP code you can add to your child theme’s functions.php file to display a notification message on the My Account page:

// Add notification message to My Account page
function add_account_notification() {
    echo '<div class="woocommerce-message">This is a notification message for all customers.</div>';
}
add_action('woocommerce_account_content', 'add_account_notification');

The code above will display a notification message with the class woocommerce-message on the My Account page for all customers. You can style this message using CSS to make it more visually appealing.

If you want to customize the message content based on specific conditions, you can do so within the add_account_notification function. For example, you could check if the user is logged in, their user role, or any other criteria, and display different messages accordingly.

Keep in mind that this code will add the notification to all customer accounts. If you need more advanced customization, such as showing different messages to different customer groups, you might need to create a custom page template for the My Account page using a child theme and use conditional statements to display the messages accordingly. However, for a simple notification message for all customers, the provided code should suffice.

Step 2:

The second step is if you would like to embed content from a page into your customer’s accounts. This may be helpful for a non-developer to add in sales related promotions or other important store notifications. We don’t recommend making this too complex, but you can use links, bullet points, embed images, etc.

To pull in the content from a specific post (e.g., post ID 252) and display it as a notification message on the My Account page, you can use the get_post function to retrieve the content of the desired post.

In this modified code, we use get_post to fetch the post data of the specified post ID (252 in this example). Then, we use wpautop to automatically format the content with paragraphs.

Please make sure that the post with the specified ID (252 in this example) exists and has the content you want to display as the notification message. Adjust the $post_id variable to match the actual ID of the post you want to pull content from.

With this adjustment, the notification message on the My Account page will display the content from the specified post.

// Add notification message to My Account page
function add_account_notification() {
    $post_id = 252; // Replace 252 with the desired post ID

    $post = get_post($post_id);

    if ($post) {
        echo '<div class="woocommerce-message">' . wpautop($post->post_content) . '</div>';
    }
}
add_action('woocommerce_account_content', 'add_account_notification');