woocommerce.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * WooCommerce Compatibility File
  4. *
  5. * @link https://woocommerce.com/
  6. *
  7. * @package radcliffe-2
  8. */
  9. /**
  10. * WooCommerce setup function.
  11. *
  12. * See: https://docs.woocommerce.com/document/third-party-custom-theme-compatibility/
  13. *
  14. * @return void
  15. */
  16. function radcliffe_2_woocommerce_setup() {
  17. add_theme_support( 'woocommerce' );
  18. add_theme_support( 'wc-product-gallery-zoom' );
  19. add_theme_support( 'wc-product-gallery-lightbox' );
  20. add_theme_support( 'wc-product-gallery-slider' );
  21. }
  22. add_action( 'after_setup_theme', 'radcliffe_2_woocommerce_setup' );
  23. /**
  24. * WooCommerce specific scripts & stylesheets
  25. *
  26. * @return void
  27. */
  28. function radcliffe_2_woocommerce_scripts() {
  29. wp_enqueue_style( 'radcliffe-2-woocommerce-style', get_template_directory_uri() . '/woocommerce.css' );
  30. }
  31. add_action( 'wp_enqueue_scripts', 'radcliffe_2_woocommerce_scripts' );
  32. /**
  33. * Disable the default WooCommerce stylesheet
  34. *
  35. * Removing the default WooCommerce stylesheet and enqueing your own will
  36. * protect you during WooCommerce core updates.
  37. *
  38. * See: https://docs.woocommerce.com/document/disable-the-default-stylesheet/
  39. */
  40. add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
  41. /**
  42. * Add 'woocommerce-active' class to the body tag
  43. *
  44. * @param array $classes css classes applied to the body tag.
  45. * @return array $classes modified to include 'woocommerce-active' class
  46. */
  47. function radcliffe_2_woocommerce_active_body_class( $classes ) {
  48. $classes[] = 'woocommerce-active';
  49. // If no header menu exists, add a body class to reposition the shop mini-cart
  50. if ( ! has_nav_menu( 'menu-1' ) ) {
  51. $classes[] = 'menu-1-empty';
  52. }
  53. return $classes;
  54. }
  55. add_filter( 'body_class', 'radcliffe_2_woocommerce_active_body_class' );
  56. /**
  57. * Products per page
  58. *
  59. * @return integer number of products
  60. */
  61. function radcliffe_2_woocommerce_products_per_page() {
  62. return absint( apply_filters( 'radcliffe_2_woocommerce_products_per_page', 12 ) );
  63. }
  64. add_filter( 'loop_shop_per_page', 'radcliffe_2_woocommerce_products_per_page' );
  65. /**
  66. * Product gallery thumnbail columns
  67. *
  68. * @return integer number of columns
  69. */
  70. function radcliffe_2_woocommerce_thumbnail_columns() {
  71. return absint( apply_filters( 'radcliffe_2_woocommerce_product_thumbnail_columns', 4 ) );
  72. }
  73. add_filter( 'woocommerce_product_thumbnails_columns', 'radcliffe_2_woocommerce_thumbnail_columns' );
  74. /**
  75. * Default loop columns on product archives
  76. *
  77. * @return integer products per row
  78. */
  79. function radcliffe_2_woocommerce_loop_columns() {
  80. return absint( apply_filters( 'radcliffe_2_woocommerce_loop_columns', 3 ) );
  81. }
  82. add_filter( 'loop_shop_columns', 'radcliffe_2_woocommerce_loop_columns' );
  83. /**
  84. * Related Products Args
  85. *
  86. * @param array $args related products args.
  87. * @return array $args related products args
  88. */
  89. function radcliffe_2_woocommerce_related_products_args( $args ) {
  90. $args = apply_filters( 'radcliffe_2_woocommerce_related_products_args', array(
  91. 'posts_per_page' => 3,
  92. 'columns' => 3,
  93. ) );
  94. return $args;
  95. }
  96. add_filter( 'woocommerce_output_related_products_args', 'radcliffe_2_woocommerce_related_products_args' );
  97. if ( ! function_exists( 'radcliffe_2_woocommerce_product_columns_wrapper' ) ) {
  98. /**
  99. * Product columns wrapper
  100. *
  101. * @return void
  102. */
  103. function radcliffe_2_woocommerce_product_columns_wrapper() {
  104. $columns = radcliffe_2_woocommerce_loop_columns();
  105. echo '<div class="columns-' . absint( $columns ) . '">';
  106. }
  107. }
  108. add_action( 'woocommerce_before_shop_loop', 'radcliffe_2_woocommerce_product_columns_wrapper', 40 );
  109. if ( ! function_exists( 'radcliffe_2_woocommerce_product_columns_wrapper_close' ) ) {
  110. /**
  111. * Product columns wrapper close
  112. *
  113. * @return void
  114. */
  115. function radcliffe_2_woocommerce_product_columns_wrapper_close() {
  116. echo '</div>';
  117. }
  118. }
  119. add_action( 'woocommerce_before_shop_loop', 'radcliffe_2_woocommerce_sorting_wrap', 3 );
  120. if ( ! function_exists( 'radcliffe_2_woocommerce_sorting_wrap' ) ) {
  121. /**
  122. * Sorting wrapper
  123. *
  124. * @return void
  125. */
  126. function radcliffe_2_woocommerce_sorting_wrap() {
  127. echo '<div class="woocommerce-sorting-wrap">';
  128. }
  129. }
  130. add_action( 'woocommerce_before_shop_loop', 'radcliffe_2_woocommerce_sorting_wrap_close', 30 );
  131. if ( ! function_exists( 'radcliffe_2_woocommerce_sorting_wrap_close' ) ) {
  132. /**
  133. * Sorting wrapper close
  134. *
  135. * @return void
  136. */
  137. function radcliffe_2_woocommerce_sorting_wrap_close() {
  138. echo '</div><!-- END .sorting-wrap -->';
  139. }
  140. }
  141. add_action( 'woocommerce_after_shop_loop', 'radcliffe_2_woocommerce_product_columns_wrapper_close', 40 );
  142. /**
  143. * Remove default WooCommerce wrapper
  144. */
  145. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  146. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  147. if ( ! function_exists( 'radcliffe_2_woocommerce_wrapper_before' ) ) {
  148. /**
  149. * Before Content
  150. * Wraps all WooCommerce content in wrappers which match the theme markup
  151. *
  152. * @return void
  153. */
  154. function radcliffe_2_woocommerce_wrapper_before() {
  155. ?>
  156. <div id="primary" class="content-area">
  157. <main id="main" class="site-main">
  158. <?php
  159. }
  160. }
  161. add_action( 'woocommerce_before_main_content', 'radcliffe_2_woocommerce_wrapper_before' );
  162. if ( ! function_exists( 'radcliffe_2_woocommerce_wrapper_after' ) ) {
  163. /**
  164. * After Content
  165. * Closes the wrapping divs
  166. *
  167. * @return void
  168. */
  169. function radcliffe_2_woocommerce_wrapper_after() {
  170. ?>
  171. </main><!-- #main -->
  172. </div><!-- #primary -->
  173. <?php
  174. }
  175. }
  176. add_action( 'woocommerce_after_main_content', 'radcliffe_2_woocommerce_wrapper_after' );
  177. if ( ! function_exists( 'radcliffe_2_woocommerce_cart_link_fragment' ) ) {
  178. /**
  179. * Cart Fragments
  180. * Ensure cart contents update when products are added to the cart via AJAX. Also adds cart icon SVG
  181. *
  182. * @param array $fragments Fragments to refresh via AJAX.
  183. * @return array Fragments to refresh via AJAX
  184. */
  185. function radcliffe_2_woocommerce_cart_link_fragment( $fragments ) {
  186. global $woocommerce;
  187. ob_start();
  188. radcliffe_2_woocommerce_cart_link();
  189. $fragments['a.cart-contents'] = ob_get_clean();
  190. return $fragments;
  191. }
  192. }
  193. add_filter( 'woocommerce_add_to_cart_fragments', 'radcliffe_2_woocommerce_cart_link_fragment' );
  194. if ( ! function_exists( 'radcliffe_2_woocommerce_cart_link' ) ) {
  195. /**
  196. * Cart Link
  197. * Displayed a link to the cart including the number of items present and the cart total
  198. *
  199. * @return void
  200. */
  201. function radcliffe_2_woocommerce_cart_link() {
  202. ?>
  203. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'radcliffe-2' ); ?>">
  204. <?php echo radcliffe_2_get_svg( array( 'icon' => 'cart', 'title' => esc_html__( 'View your shopping cart', 'radcliffe-2' ) ) ); ?>
  205. <?php /* translators: number of items in the mini cart. */ ?>
  206. <span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span>
  207. <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'radcliffe-2' ), WC()->cart->get_cart_contents_count() ) );?></span>
  208. </a>
  209. <?php
  210. }
  211. }
  212. if ( ! function_exists( 'radcliffe_2_woocommerce_header_cart' ) ) {
  213. /**
  214. * Display Header Cart
  215. *
  216. * @return void
  217. */
  218. function radcliffe_2_woocommerce_header_cart() {
  219. if ( is_cart() ) {
  220. $class = 'current-menu-item';
  221. } else {
  222. $class = '';
  223. }
  224. ?>
  225. <ul id="site-header-cart" class="site-header-cart">
  226. <li class="<?php echo esc_attr( $class ); ?>">
  227. <?php radcliffe_2_woocommerce_cart_link(); ?>
  228. </li>
  229. <li>
  230. <?php the_widget( 'WC_Widget_Cart', 'title=' ); ?>
  231. </li>
  232. </ul>
  233. <?php
  234. }
  235. }
  236. /**
  237. * Remove breadcrumbs
  238. */
  239. remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
  240. /**
  241. * Add breadcrumbs after
  242. *
  243. * We know the content wrapper is added at priority 10, so
  244. * let's add the breadcrumbs before that, at priority 5
  245. */
  246. add_action( 'woocommerce_before_shop_loop', 'woocommerce_breadcrumb', 5 );