WordPress Functions

WooCommerce: wc_add_notice Function

23 October 2023

Managing and displaying notices on your website is crucial for providing important information to your users. That’s where the wc_add_notice function comes into play. This powerful function allows you to easily add and store notices, such as error messages, success messages, or general notices.

With wc_add_notice, you have the flexibility to add custom validation for notice types and even include optional notice data. You can choose from three types of notices – error, success, or notice – depending on the specific message you want to display. Whether you’re creating an account page or handling backend operations, this versatile function ensures that your notices are displayed accurately and effectively.

Syntax

wc_add_notice( $message, $notice_type = 'success', $data = array() );

Parameters

  • $message (string): The text to display in the notice.
  • $notice_type (string, optional): The name of the notice type – either error, success, or notice. Default value is ‘success’.
  • $data (array, optional): Optional notice data.

Return Value

This function does not have a return value.

How it works

/**
* Add and store a notice.
*
* @since 2.1
* @version 3.9.0
* @param string $message The text to display in the notice.
* @param string $notice_type Optional. The name of the notice type - either error, success or notice.
* @param array $data Optional notice data.
*/
function wc_add_notice( $message, $notice_type = 'success', $data = array() ) {
if ( ! did_action( 'woocommerce_init' ) ) {
wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
return;
}
$notices = WC()->session->get( 'wc_notices', array() );
// Backward compatibility.
if ( 'success' === $notice_type ) {
$message = apply_filters( 'woocommerce_add_message', $message );
}
$message = apply_filters( 'woocommerce_add_' . $notice_type, $message );
if ( ! empty( $message ) ) {
$notices[ $notice_type ][] = array(
'notice' => $message,
'data' => $data,
);
}
WC()->session->set( 'wc_notices', $notices );
}
  1. First, it checks if the action woocommerce_init has been fired using the did_action() function. If it hasn’t been fired yet, a warning message is triggered using the wc_doing_it_wrong() function and the function execution is stopped.
  2. The existing notices are retrieved from WooCommerce session using WC()->session->get(). The notices are stored in an array called $notices.
  3. If the notice type is ‘success’, backward compatibility is maintained by applying the filter ‘woocommerce_add_message’ to modify the message.
  4. The filter ‘woocommerce_add_’ . $notice_type is applied to modify the message based on the notice type.
  5. If the message is not empty after filtering, a new notice entry is added to $notices. The entry includes the message and optional data provided.
  6. Finally, the updated $notices array is stored back into WooCommerce session using WC()->session->set().

Overall, this function allows developers to add custom notices with different types (error, success, or notice) to be displayed on the website. These notices can provide important information, feedback, or notifications to the users.

Note: To learn more about using WooCommerce notices and how to display them on different pages, refer to the WooCommerce documentation.

Usage

Inside a Plugin

Let’s say you are developing a custom WooCommerce plugin and you want to add a notice to the account page of your users when they perform certain actions. You can use the wc_add_notice function to achieve this.

Example: Adding a notice after successful account creation

// Hook into the 'woocommerce_created_customer' action
add_action( 'woocommerce_created_customer', 'my_custom_notice_function' );
/**
* Add a custom notice after successful account creation.
*
* @param int $customer_id The ID of the newly created customer.
*/
function my_custom_notice_function( $customer_id ) {
// Retrieve the customer's email address
$customer_email = get_the_title( $customer_id ); // Replace with your actual code
// Prepare the notice message
$message = sprintf( __( 'Account created successfully for %s!', 'your-plugin-textdomain' ), $customer_email );
// Add the notice using wc_add_notice
wc_add_notice( $message, 'success' );
}
Explanation:

In this example:

  • We hook into the woocommerce_created_customer action which is triggered after a new customer account is successfully created.
  • Inside our custom function my_custom_notice_function, we retrieve the customer’s email address using any necessary code.
  • We then prepare the notice message using sprintf and include the customer’s email as part of the message.
  • Finally, we call wc_add_notice to add a success notice with our prepared message.

After running this code, when a new user creates an account on your WooCommerce site, they will see a success notice with their email displayed on the account page.

Inside a Theme

Now let’s consider that you are developing a custom WooCommerce theme and you want to display an error message in case there is an issue with product stock during checkout. You can utilize the wc_add_notice function to accomplish this.

Example: Display an error notice for out-of-stock products during checkout

// Hook into the 'woocommerce_check_cart_items' action
add_action( 'woocommerce_check_cart_items', 'my_custom_checkout_notice' );
/**
* Add a notice if any product in the cart is out of stock during checkout.
*/
function my_custom_checkout_notice() {
// Check if there are any out-of-stock products in the cart
$out_of_stock_products = wc_get_product_ids_on_sale(); // Replace with your actual code
if ( ! empty( $out_of_stock_products ) ) {
// Prepare the notice message
$message = __( 'Some products in your cart are currently out of stock. Please remove them before proceeding with the checkout.', 'your-theme-textdomain' );
// Add the notice using wc_add_notice
wc_add_notice( $message, 'error' );
}
}
Explanation:

In this example:

  • We hook into the woocommerce_check_cart_items action which is triggered when WooCommerce checks for valid cart items during checkout.
  • Inside our custom function my_custom_checkout_notice, we check if there are any out-of-stock products in the cart using any necessary code.
  • If there are out-of-stock products, we prepare an error message to inform the user and guide them on how to resolve it.
  • Finally, we call wc_add_notice to add an error notice with our prepared message.

After implementing this code, when a customer proceeds to checkout and has one or more out-of-stock products in their cart, they will see an error notice notifying them about it and instructing them to remove those products before proceeding.

Note: Make sure to replace ‘your-plugin-textdomain’ and ‘your-theme-textdomain’ with your actual text domain used in your plugin and theme.

Leave a Reply

Your email address will not be published. Required fields are marked *

Array

Some of Our Clients

Join clients who enjoy 96% satisfaction

Schedule a Free Strategy Call with a WordPress Expert