1 <?php
  2 /**
  3  * WooCommerce Conditional Functions
  4  *
  5  * Functions for determining the current query/page.
  6  *
  7  * @author      WooThemes
  8  * @category    Core
  9  * @package     WooCommerce/Functions
 10  * @version     2.1.0
 11  */
 12 
 13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 14 
 15 /**
 16  * is_woocommerce - Returns true if on a page which uses WooCommerce templates (cart and checkout are standard pages with shortcodes and thus are not included)
 17  *
 18  * @access public
 19  * @return bool
 20  */
 21 function is_woocommerce() {
 22     return apply_filters( 'is_woocommerce', ( is_shop() || is_product_taxonomy() || is_product() ) ? true : false );
 23 }
 24 
 25 if ( ! function_exists( 'is_shop' ) ) {
 26 
 27     /**
 28      * is_shop - Returns true when viewing the product type archive (shop).
 29      *
 30      * @access public
 31      * @return bool
 32      */
 33     function is_shop() {
 34         return ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) ? true : false;
 35     }
 36 }
 37 
 38 if ( ! function_exists( 'is_product_taxonomy' ) ) {
 39 
 40     /**
 41      * is_product_taxonomy - Returns true when viewing a product taxonomy archive.
 42      *
 43      * @access public
 44      * @return bool
 45      */
 46     function is_product_taxonomy() {
 47         return is_tax( get_object_taxonomies( 'product' ) );
 48     }
 49 }
 50 
 51 if ( ! function_exists( 'is_product_category' ) ) {
 52 
 53     /**
 54      * is_product_category - Returns true when viewing a product category.
 55      *
 56      * @access public
 57      * @param string $term (default: '') The term slug your checking for. Leave blank to return true on any.
 58      * @return bool
 59      */
 60     function is_product_category( $term = '' ) {
 61         return is_tax( 'product_cat', $term );
 62     }
 63 }
 64 
 65 if ( ! function_exists( 'is_product_tag' ) ) {
 66 
 67     /**
 68      * is_product_tag - Returns true when viewing a product tag.
 69      *
 70      * @access public
 71      * @param string $term (default: '') The term slug your checking for. Leave blank to return true on any.
 72      * @return bool
 73      */
 74     function is_product_tag( $term = '' ) {
 75         return is_tax( 'product_tag', $term );
 76     }
 77 }
 78 
 79 if ( ! function_exists( 'is_product' ) ) {
 80 
 81     /**
 82      * is_product - Returns true when viewing a single product.
 83      *
 84      * @access public
 85      * @return bool
 86      */
 87     function is_product() {
 88         return is_singular( array( 'product' ) );
 89     }
 90 }
 91 
 92 if ( ! function_exists( 'is_cart' ) ) {
 93 
 94     /**
 95      * is_cart - Returns true when viewing the cart page.
 96      *
 97      * @access public
 98      * @return bool
 99      */
100     function is_cart() {
101         return is_page( wc_get_page_id( 'cart' ) );
102     }
103 }
104 
105 if ( ! function_exists( 'is_checkout' ) ) {
106 
107     /**
108      * is_checkout - Returns true when viewing the checkout page.
109      *
110      * @access public
111      * @return bool
112      */
113     function is_checkout() {
114         return is_page( wc_get_page_id( 'checkout' ) ) ? true : false;
115     }
116 }
117 
118 if ( ! function_exists( 'is_checkout_pay_page' ) ) {
119 
120     /**
121      * is_checkout_pay - Returns true when viewing the checkout's pay page.
122      *
123      * @access public
124      * @return bool
125      */
126     function is_checkout_pay_page() {
127         global $wp;
128 
129         return is_checkout() && ! empty( $wp->query_vars['order-pay'] ) ? true : false;
130     }
131 }
132 
133 if ( ! function_exists( 'is_wc_endpoint_url' ) ) {
134 
135     /**
136      * is_wc_endpoint_url - Check if an endpoint is showing
137      *
138      * @access public
139      * @param  string $endpoint
140      * @return bool
141      */
142     function is_wc_endpoint_url( $endpoint ) {
143         global $wp;
144 
145         $wc_endpoints = WC()->query->get_query_vars();
146 
147         if ( ! isset( $wc_endpoints[ $endpoint ] ) ) {
148             return false;
149         } else {
150             $endpoint_var = $wc_endpoints[ $endpoint ];
151         }
152 
153         return isset( $wp->query_vars[ $endpoint_var ] ) ? true : false;
154     }
155 }
156 
157 if ( ! function_exists( 'is_account_page' ) ) {
158 
159     /**
160      * is_account_page - Returns true when viewing an account page.
161      *
162      * @access public
163      * @return bool
164      */
165     function is_account_page() {
166         return is_page( wc_get_page_id( 'myaccount' ) ) || apply_filters( 'woocommerce_is_account_page', false ) ? true : false;
167     }
168 }
169 
170 if ( ! function_exists( 'is_order_received_page' ) ) {
171 
172     /**
173     * is_order_received_page - Returns true when viewing the order received page.
174     *
175     * @access public
176     * @return bool
177     */
178     function is_order_received_page() {
179         global $wp;
180 
181         return ( is_page( wc_get_page_id( 'checkout' ) ) && isset( $wp->query_vars['order-received'] ) ) ? true : false;
182     }
183 }
184 
185 if ( ! function_exists( 'is_add_payment_method_page' ) ) {
186 
187     /**
188     * is_add_payment_method_page - Returns true when viewing the add payment method page.
189     *
190     * @access public
191     * @return bool
192     */
193     function is_add_payment_method_page() {
194         global $wp;
195 
196         return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['add-payment-method'] ) ) ? true : false;
197     }
198 }
199 
200 if ( ! function_exists( 'is_ajax' ) ) {
201 
202     /**
203      * is_ajax - Returns true when the page is loaded via ajax.
204      *
205      * @access public
206      * @return bool
207      */
208     function is_ajax() {
209         return defined( 'DOING_AJAX' );
210     }
211 }
212 
213 if ( ! function_exists( 'is_store_notice_showing' ) ) {
214 
215     /**
216      * is_store_notice_showing - Returns true when store notice is active.
217      *
218      * @access public
219      * @return bool
220      */
221     function is_store_notice_showing() {
222         return get_option( 'woocommerce_demo_store' ) !== 'no' ? true : false;
223     }
224 }
225 
226 if ( ! function_exists( 'is_filtered' ) ) {
227 
228     /**
229      * is_filtered - Returns true when filtering products using layered nav or price sliders.
230      *
231      * @access public
232      * @return bool
233      */
234     function is_filtered() {
235         global $_chosen_attributes;
236 
237         return apply_filters( 'woocommerce_is_filtered', ( sizeof( $_chosen_attributes ) > 0 || ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) ) );
238     }
239 }
240 
241 if ( ! function_exists( 'taxonomy_is_product_attribute' ) ) {
242 
243     /**
244      * Returns true when the passed taxonomy name is a product attribute.
245      *
246      * @uses  $wc_product_attributes global which stores taxonomy names upon registration
247      * @param string $name of the attribute
248      * @return bool
249      */
250     function taxonomy_is_product_attribute( $name ) {
251         global $wc_product_attributes;
252 
253         return taxonomy_exists( $name ) && array_key_exists( $name, (array) $wc_product_attributes );
254     }
255 }
256 
257 if ( ! function_exists( 'meta_is_product_attribute' ) ) {
258 
259     /**
260      * Returns true when the passed meta name is a product attribute.
261      *
262      * @param string $name of the attribute
263      * @param mixed $value
264      * @param int $product_id
265      * @return bool
266      */
267     function meta_is_product_attribute( $name, $value, $product_id ) {
268         $product    = get_product( $product_id );
269 
270         if ( $product->product_type != 'variation' ) {
271             return false;
272         }
273 
274         $attributes = $product->get_variation_attributes();
275 
276         return ( in_array( $name, array_keys( $attributes ) ) && in_array( $value, $attributes[ $name ] ) );
277     }
278 }
279 
WooCommerce API documentation generated by ApiGen 2.8.0