Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active March 27, 2023 23:12
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save bekarice/ce7b04fe8158dc736e0c to your computer and use it in GitHub Desktop.
Save bekarice/ce7b04fe8158dc736e0c to your computer and use it in GitHub Desktop.
Add content and notices to the WooCommerce checkout - sample code
/**
* Each of these samples can be used - note that you should pick one rather than add them all.
*
* How to use WC notices: https://github.com/woothemes/woocommerce/blob/master/includes/wc-notice-functions.php#L96
* Tutorial: http://www.skyverge.com/blog/edit-woocommerce-templates/
**/
/**
* Add a content block after all notices, such as the login and coupon notices.
*
* Reference: https://github.com/woothemes/woocommerce/blob/master/templates/checkout/form-checkout.php
*/
add_action( 'woocommerce_before_checkout_form', 'skyverge_add_checkout_content', 12 );
function skyverge_add_checkout_content() {
echo 'This content that you can use to tell customers stuff. You could make it a div class="checkout-message" and style it if you wanted.';
}
/**
* Add a content in a notice instead. Let's add it before other notices with a priority = 9
*
* Reference: https://github.com/woothemes/woocommerce/blob/master/templates/checkout/form-checkout.php
*/
add_action( 'woocommerce_before_checkout_form', 'skyverge_add_checkout_success', 9 );
function skyverge_add_checkout_success() {
wc_print_notice( __( 'A success message with high priority.', 'woocommerce' ), 'success' );
}
/**
* Add an info notice instead. Let's add it after other notices with priority = 11
*
* Reference: https://github.com/woothemes/woocommerce/blob/master/templates/checkout/form-checkout.php
*/
add_action( 'woocommerce_before_checkout_form', 'skyverge_add_checkout_notice', 11 );
function skyverge_add_checkout_notice() {
wc_print_notice( __( 'A notice message instead.', 'woocommerce' ), 'notice' );
}
/**
* Add add a notice before the payment form - let's use an eror notice. Could also use content, etc.
*
* Reference: https://github.com/woothemes/woocommerce/blob/master/templates/checkout/review-order.php
*/
add_action( 'woocommerce_review_order_before_payment', 'skyverge_before_paying_notice' );
function skyverge_before_paying_notice() {
wc_print_notice( __( 'An error message.', 'woocommerce' ), 'error' );
}
@warunanc
Copy link

Thanks for these snippets. With the help from following link - anything is possible in Checkout page
WooCommerce Visual Hook Guide: Checkout Page
https://businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/

@indefinitelee
Copy link

thanks for this. I want to change the success/failure notice for applying a coupon on my cart page.

would this code in my child theme overwrite the default notice text?

@dogrescuer
Copy link

Thank you for this code. Does the woocommerce_review_order_before_payment example mean to imply that one will not see error messages unless placed as described?

@gietmxh
Copy link

gietmxh commented Mar 22, 2020

tks you

@yurith-rubio
Copy link

Thank you, I am not good with PHP, but this worked for me. I just have an issue: The message is shown at the top or the page.

  • How can I locate the message in a specific are on the checkout?

Thanks

@Kaji01
Copy link

Kaji01 commented Feb 17, 2023

Just a quick note to say that this is still working flawlessly in 2023!

For anyone wondering how to control where your message will appear, it all depends on the action hook:

  • woocommerce_before_checkout_form will place it at the top of the page
  • woocommerce_review_order_before_payment will place it in the middle of the page near where the order total is displayed

If you just drop the code as shown above into your main functions.php file it will always display on the checkout page no matter what, so make sure you wrap your message inside of the appropriate conditionals if you only want it to display under specific conditions.

The color of the message is controlled by the keyword at the end of the wc_print_notice() call:

  • NULL leaving it blank isn't showing up for me in the current version, though it doesn't seem to trigger an error, either
  • success shows the message inside of a GREEN box
  • notice shows the message inside of a BLUE box
  • error shows the message inside of a RED box

Hope that helps people finding this to get the most out of it. Thanks again to the author for posting a great reference for it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment