1 <?php
2 3 4 5 6 7 8 9 10 11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 16 17 18 19 20 21
22 function wc_notice_count( $notice_type = '' ) {
23 $notice_count = 0;
24 $all_notices = WC()->session->get( 'wc_notices', array() );
25
26 if ( isset( $all_notices[$notice_type] ) ) {
27
28 $notice_count = absint( sizeof( $all_notices[$notice_type] ) );
29
30 } elseif ( empty( $notice_type ) ) {
31
32 foreach ( $all_notices as $notices ) {
33 $notice_count += absint( sizeof( $all_notices ) );
34 }
35
36 }
37
38 return $notice_count;
39 }
40
41 42 43 44 45 46 47
48 function wc_has_notice( $message, $notice_type = 'success' ) {
49 $notices = WC()->session->get( 'wc_notices', array() );
50 $notices = isset( $notices[ $notice_type ] ) ? $notices[ $notice_type ] : array();
51 return array_search( $message, $notices ) !== false;
52 }
53
54 55 56 57 58 59
60 function wc_add_notice( $message, $notice_type = 'success' ) {
61
62 $notices = WC()->session->get( 'wc_notices', array() );
63
64
65 if ( 'success' === $notice_type )
66 $message = apply_filters( 'woocommerce_add_message', $message );
67
68 $notices[$notice_type][] = apply_filters( 'woocommerce_add_' . $notice_type, $message );
69
70 WC()->session->set( 'wc_notices', $notices );
71 }
72
73 74 75 76 77
78 function wc_clear_notices() {
79 WC()->session->set( 'wc_notices', null );
80 }
81
82 83 84 85 86
87 function wc_print_notices() {
88
89 $all_notices = WC()->session->get( 'wc_notices', array() );
90 $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
91
92 foreach ( $notice_types as $notice_type ) {
93 if ( wc_notice_count( $notice_type ) > 0 ) {
94 wc_get_template( "notices/{$notice_type}.php", array(
95 'messages' => $all_notices[$notice_type]
96 ) );
97 }
98 }
99
100 wc_clear_notices();
101 }
102 add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
103 add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
104
105 106 107 108 109 110
111 function wc_print_notice( $message, $notice_type = 'success' ) {
112
113 if ( 'success' === $notice_type )
114 $message = apply_filters( 'woocommerce_add_message', $message );
115
116 wc_get_template( "notices/{$notice_type}.php", array(
117 'messages' => array( apply_filters( 'woocommerce_add_' . $notice_type, $message ) )
118 ) );
119 }
120
121 122 123 124 125
126 function wc_get_notices( $notice_type = '' ) {
127
128 $all_notices = WC()->session->get( 'wc_notices', array() );
129
130 if ( empty ( $notice_type ) ) {
131 $notices = $all_notices;
132 } elseif ( isset( $all_notices[$notice_type] ) ) {
133 $notices = $all_notices[$notice_type];
134 } else {
135 $notices = array();
136 }
137
138 return $notices;
139 }
140