woocommerce/includes/wc-notice-functions.php

76 lines
2.1 KiB
PHP
Raw Normal View History

2013-06-11 14:59:42 +00:00
<?php
/**
* WooCommerce Message Functions
*
* Functions for error/message handling and display.
*
* @author WooThemes
* @category Core
* @package WooCommerce/Functions
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Get the count of notices added, either for all notices or for one particular notice type.
*
* @param string $notice_type The internal name of the notice type - either wc_errors, wc_messages or wc_notices. [optional]
2013-06-11 14:59:42 +00:00
* @return int
*/
function wc_notice_count( $notice_type = '' ) {
2013-06-11 14:59:42 +00:00
$notice_count = 0;
if ( ! empty( $notice_type ) ) {
$notice_count += absint( sizeof( WC()->session->get( $notice_type, array() ) ) );
} else {
foreach ( array( 'wc_errors', 'wc_messages', 'wc_notices' ) as $notice_type ) {
$notice_count += absint( sizeof( WC()->session->get( $notice_type, array() ) ) );
}
}
return $notice_count;
2013-06-11 14:59:42 +00:00
}
/**
* Add and store a notice
2013-06-11 14:59:42 +00:00
*
* @param string $message The text to display in the notice.
* @param string $notice_type The singular name of the notice type - either error, message or notice. [optional]
2013-06-11 14:59:42 +00:00
*/
function wc_add_notice( $message, $notice_type = 'message' ) {
2013-06-11 14:59:42 +00:00
$notices = WC()->session->get( "wc_{$notice_type}s", array() );
$notices[] = apply_filters( 'woocommerce_add_' . $notice_type, $message );
2013-06-11 14:59:42 +00:00
WC()->session->set( "wc_{$notice_type}s", $notices );
2013-06-11 14:59:42 +00:00
}
/**
* Unset all notices
2013-06-11 14:59:42 +00:00
*/
function wc_clear_notices() {
foreach ( array( 'wc_errors', 'wc_messages', 'wc_notices' ) as $notice_type ) {
WC()->session->set( $notice_type, null );
}
2013-06-11 14:59:42 +00:00
}
2013-06-11 14:59:42 +00:00
/**
* Prints messages and errors which are stored in the session, then clears them.
*/
function wc_print_notices() {
2013-06-11 14:59:42 +00:00
foreach ( array( 'errors', 'messages', 'notices' ) as $notice_type ) {
if ( wc_notice_count( $notice_type ) > 0 ) {
woocommerce_get_template( "shop/{$notice_type}.php", array(
$notice_type => WC()->session->get( "wc_{$notice_type}", array() )
) );
}
}
2013-06-11 14:59:42 +00:00
wc_clear_notices();
2013-06-11 14:59:42 +00:00
}
add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );