/woocommerce_templates.php

https://github.com/PayJunctionDev/woocommerce · PHP · 222 lines · 118 code · 64 blank · 40 comment · 32 complexity · 47ca76a81cd06c4485ac3bf4f99a5e9f MD5 · raw file

  1. <?php
  2. /**
  3. * WooCommerce Templates
  4. *
  5. * Handles template usage so that we can use our own templates instead of the theme's.
  6. *
  7. * Templates are in the 'templates' folder. woocommerce looks for theme
  8. * overides in /theme/woocommerce/ by default but this can be overwritten with WOOCOMMERCE_TEMPLATE_URL
  9. *
  10. * @package WooCommerce
  11. * @category Core
  12. * @author WooThemes
  13. */
  14. function woocommerce_template_loader( $template ) {
  15. global $woocommerce;
  16. if ( is_single() && get_post_type() == 'product' ) {
  17. $template = locate_template( array( 'single-product.php', WOOCOMMERCE_TEMPLATE_URL . 'single-product.php' ) );
  18. if ( ! $template ) $template = $woocommerce->plugin_path() . '/templates/single-product.php';
  19. }
  20. elseif ( is_tax('product_cat') ) {
  21. $template = locate_template( array( 'taxonomy-product_cat.php', WOOCOMMERCE_TEMPLATE_URL . 'taxonomy-product_cat.php' ) );
  22. if ( ! $template ) $template = $woocommerce->plugin_path() . '/templates/taxonomy-product_cat.php';
  23. }
  24. elseif ( is_tax('product_tag') ) {
  25. $template = locate_template( array( 'taxonomy-product_tag.php', WOOCOMMERCE_TEMPLATE_URL . 'taxonomy-product_tag.php' ) );
  26. if ( ! $template ) $template = $woocommerce->plugin_path() . '/templates/taxonomy-product_tag.php';
  27. }
  28. elseif ( is_post_type_archive('product') || is_page( get_option('woocommerce_shop_page_id') )) {
  29. $template = locate_template( array( 'archive-product.php', WOOCOMMERCE_TEMPLATE_URL . 'archive-product.php' ) );
  30. if ( ! $template ) $template = $woocommerce->plugin_path() . '/templates/archive-product.php';
  31. }
  32. return $template;
  33. }
  34. add_filter( 'template_include', 'woocommerce_template_loader' );
  35. /**
  36. * Get template part (for templates like loop)
  37. */
  38. function woocommerce_get_template_part( $slug, $name = '' ) {
  39. global $woocommerce;
  40. if ($name=='shop') :
  41. if (!locate_template(array( 'loop-shop.php', WOOCOMMERCE_TEMPLATE_URL . 'loop-shop.php' ))) :
  42. load_template( $woocommerce->plugin_path() . '/templates/loop-shop.php',false );
  43. return;
  44. endif;
  45. endif;
  46. get_template_part( WOOCOMMERCE_TEMPLATE_URL . $slug, $name );
  47. }
  48. /**
  49. * Get the reviews template (comments)
  50. */
  51. function woocommerce_comments_template($template) {
  52. global $woocommerce;
  53. if(get_post_type() !== 'product') return $template;
  54. if (file_exists( STYLESHEETPATH . '/' . WOOCOMMERCE_TEMPLATE_URL . 'single-product-reviews.php' ))
  55. return STYLESHEETPATH . '/' . WOOCOMMERCE_TEMPLATE_URL . 'single-product-reviews.php';
  56. else
  57. return $woocommerce->plugin_path() . '/templates/single-product-reviews.php';
  58. }
  59. add_filter('comments_template', 'woocommerce_comments_template' );
  60. /**
  61. * Get other templates (e.g. product attributes)
  62. */
  63. function woocommerce_get_template($template_name, $require_once = true) {
  64. global $woocommerce;
  65. if (file_exists( STYLESHEETPATH . '/' . WOOCOMMERCE_TEMPLATE_URL . $template_name )) load_template( STYLESHEETPATH . '/' . WOOCOMMERCE_TEMPLATE_URL . $template_name, $require_once );
  66. elseif (file_exists( STYLESHEETPATH . '/' . $template_name )) load_template( STYLESHEETPATH . '/' . $template_name , $require_once);
  67. else load_template( $woocommerce->plugin_path() . '/templates/' . $template_name , $require_once);
  68. }
  69. /**
  70. * Front page archive/shop template applied to main loop
  71. */
  72. if (!function_exists('woocommerce_front_page_archive')) {
  73. function woocommerce_front_page_archive( $query ) {
  74. global $paged, $woocommerce, $wp_the_query, $wp_query;
  75. if ( defined('SHOP_IS_ON_FRONT') ) :
  76. wp_reset_query();
  77. // Only apply to front_page
  78. if ( $query === $wp_the_query ) :
  79. if (get_query_var('paged')) :
  80. $paged = get_query_var('paged');
  81. else :
  82. $paged = (get_query_var('page')) ? get_query_var('page') : 1;
  83. endif;
  84. // Filter the query
  85. add_filter( 'parse_query', array( &$woocommerce->query, 'parse_query') );
  86. // Query the products
  87. $wp_query->query( array( 'page_id' => '', 'p' => '', 'post_type' => 'product', 'paged' => $paged ) );
  88. // get products in view (for use by widgets)
  89. $woocommerce->query->get_products_in_view();
  90. // Remove the query manipulation
  91. remove_filter( 'parse_query', array( &$woocommerce->query, 'parse_query') );
  92. remove_action('loop_start', 'woocommerce_front_page_archive', 1);
  93. endif;
  94. endif;
  95. }
  96. }
  97. add_action('loop_start', 'woocommerce_front_page_archive', 1);
  98. /**
  99. * Detect frontpage shop and fix pagination on static front page
  100. **/
  101. function woocommerce_front_page_archive_paging_fix() {
  102. if ( is_front_page() && is_page( get_option('woocommerce_shop_page_id') )) :
  103. if (get_query_var('paged')) :
  104. $paged = get_query_var('paged');
  105. else :
  106. $paged = (get_query_var('page')) ? get_query_var('page') : 1;
  107. endif;
  108. query_posts( array( 'page_id' => get_option('woocommerce_shop_page_id'), 'is_paged' => true, 'paged' => $paged ) );
  109. define('SHOP_IS_ON_FRONT', true);
  110. endif;
  111. }
  112. add_action('wp', 'woocommerce_front_page_archive_paging_fix', 1);
  113. /**
  114. * Add Body classes based on page/template
  115. **/
  116. global $woocommerce_body_classes;
  117. function woocommerce_page_body_classes() {
  118. global $woocommerce_body_classes;
  119. $woocommerce_body_classes = (array) $woocommerce_body_classes;
  120. $woocommerce_body_classes[] = 'theme-' . strtolower( get_current_theme() );
  121. if (is_woocommerce()) $woocommerce_body_classes[] = 'woocommerce';
  122. if (is_checkout()) $woocommerce_body_classes[] = 'woocommerce-checkout';
  123. if (is_cart()) $woocommerce_body_classes[] = 'woocommerce-cart';
  124. if (is_account_page()) $woocommerce_body_classes[] = 'woocommerce-account';
  125. }
  126. add_action('wp_head', 'woocommerce_page_body_classes');
  127. function woocommerce_body_class($classes) {
  128. global $woocommerce_body_classes;
  129. $woocommerce_body_classes = (array) $woocommerce_body_classes;
  130. $classes = array_merge($classes, $woocommerce_body_classes);
  131. return $classes;
  132. }
  133. add_filter('body_class','woocommerce_body_class');
  134. /**
  135. * Fix active class in nav for shop page
  136. **/
  137. function woocommerce_nav_menu_item_classes( $menu_items, $args ) {
  138. if (!is_woocommerce()) return $menu_items;
  139. $shop_page = (int) get_option('woocommerce_shop_page_id');
  140. $page_for_posts = (int) get_option( 'page_for_posts' );
  141. foreach ( (array) $menu_items as $key => $menu_item ) :
  142. $classes = (array) $menu_item->classes;
  143. // Unset active class for blog page
  144. if ( $page_for_posts == $menu_item->object_id ) :
  145. $menu_items[$key]->current = false;
  146. unset( $classes[ array_search('current_page_parent', $classes) ] );
  147. unset( $classes[ array_search('current-menu-item', $classes) ] );
  148. // Set active state if this is the shop page link
  149. elseif ( is_shop() && $shop_page == $menu_item->object_id ) :
  150. $menu_items[$key]->current = true;
  151. $classes[] = 'current-menu-item';
  152. $classes[] = 'current_page_item';
  153. endif;
  154. $menu_items[$key]->classes = array_unique( $classes );
  155. endforeach;
  156. return $menu_items;
  157. }
  158. add_filter( 'wp_nav_menu_objects', 'woocommerce_nav_menu_item_classes', 2, 20 );