woocommerce.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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', apply_filters( 'radcliffe_2_woocommerce_args', array(
  18. 'single_image_width' => 600,
  19. 'thumbnail_image_width' => 300,
  20. 'product_grid' => array(
  21. 'default_columns' => 3,
  22. 'default_rows' => 4,
  23. 'min_columns' => 1,
  24. 'max_columns' => 6,
  25. 'min_rows' => 1
  26. )
  27. ) ) );
  28. add_theme_support( 'wc-product-gallery-zoom' );
  29. add_theme_support( 'wc-product-gallery-lightbox' );
  30. add_theme_support( 'wc-product-gallery-slider' );
  31. }
  32. add_action( 'after_setup_theme', 'radcliffe_2_woocommerce_setup' );
  33. /**
  34. * WooCommerce specific scripts & stylesheets
  35. *
  36. * @return void
  37. */
  38. function radcliffe_2_woocommerce_scripts() {
  39. wp_enqueue_style( 'radcliffe-2-woocommerce-style', get_template_directory_uri() . '/woocommerce.css' );
  40. wp_style_add_data( 'radcliffe-2-woocommerce-style', 'rtl', get_stylesheet_directory_uri() . '/woocommerce-rtl.css' );
  41. }
  42. add_action( 'wp_enqueue_scripts', 'radcliffe_2_woocommerce_scripts' );
  43. /**
  44. * Disable the default WooCommerce stylesheet
  45. *
  46. * Removing the default WooCommerce stylesheet and enqueing your own will
  47. * protect you during WooCommerce core updates.
  48. *
  49. * See: https://docs.woocommerce.com/document/disable-the-default-stylesheet/
  50. */
  51. add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
  52. /**
  53. * Add 'woocommerce-active' class to the body tag
  54. *
  55. * @param array $classes css classes applied to the body tag.
  56. * @return array $classes modified to include 'woocommerce-active' class
  57. */
  58. function radcliffe_2_woocommerce_active_body_class( $classes ) {
  59. $classes[] = 'woocommerce-active';
  60. // If no header menu exists, add a body class to reposition the shop mini-cart
  61. if ( ! has_nav_menu( 'menu-1' ) ) {
  62. $classes[] = 'menu-1-empty';
  63. }
  64. return $classes;
  65. }
  66. add_filter( 'body_class', 'radcliffe_2_woocommerce_active_body_class' );
  67. /**
  68. * Products per page
  69. *
  70. * @return integer number of products
  71. */
  72. function radcliffe_2_woocommerce_products_per_page() {
  73. return absint( apply_filters( 'radcliffe_2_woocommerce_products_per_page', 12 ) );
  74. }
  75. // Legacy WooCommerce products per page filter.
  76. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  77. add_filter( 'loop_shop_per_page', 'radcliffe_2_woocommerce_products_per_page' );
  78. }
  79. /**
  80. * Product gallery thumnbail columns
  81. *
  82. * @return integer number of columns
  83. */
  84. function radcliffe_2_woocommerce_thumbnail_columns() {
  85. return absint( apply_filters( 'radcliffe_2_woocommerce_product_thumbnail_columns', 4 ) );
  86. }
  87. add_filter( 'woocommerce_product_thumbnails_columns', 'radcliffe_2_woocommerce_thumbnail_columns' );
  88. /**
  89. * Default loop columns on product archives
  90. *
  91. * @return integer products per row
  92. */
  93. function radcliffe_2_woocommerce_loop_columns() {
  94. return absint( apply_filters( 'radcliffe_2_woocommerce_loop_columns', 3 ) );
  95. }
  96. // Legacy WooCommerce columns filter.
  97. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  98. add_filter( 'loop_shop_columns', 'radcliffe_2_woocommerce_loop_columns' );
  99. }
  100. /**
  101. * Related Products Args
  102. *
  103. * @param array $args related products args.
  104. * @return array $args related products args
  105. */
  106. function radcliffe_2_woocommerce_related_products_args( $args ) {
  107. $args = apply_filters( 'radcliffe_2_woocommerce_related_products_args', array(
  108. 'posts_per_page' => 3,
  109. 'columns' => 3,
  110. ) );
  111. return $args;
  112. }
  113. add_filter( 'woocommerce_output_related_products_args', 'radcliffe_2_woocommerce_related_products_args' );
  114. if ( ! function_exists( 'radcliffe_2_woocommerce_product_columns_wrapper' ) ) {
  115. /**
  116. * Product columns wrapper
  117. *
  118. * @return void
  119. */
  120. function radcliffe_2_woocommerce_product_columns_wrapper() {
  121. $columns = radcliffe_2_loop_columns();
  122. echo '<div class="columns-' . absint( $columns ) . '">';
  123. }
  124. }
  125. add_action( 'woocommerce_before_shop_loop', 'radcliffe_2_woocommerce_product_columns_wrapper', 40 );
  126. if ( ! function_exists( 'radcliffe_2_loop_columns' ) ) {
  127. /**
  128. * Default loop columns on product archives
  129. *
  130. * @return integer products per row
  131. */
  132. function radcliffe_2_loop_columns() {
  133. $columns = 3; // 3 products per row
  134. if ( function_exists( 'wc_get_default_products_per_row' ) ) {
  135. $columns = wc_get_default_products_per_row();
  136. }
  137. return apply_filters( 'radcliffe_2_loop_columns', $columns );
  138. }
  139. }
  140. if ( ! function_exists( 'radcliffe_2_woocommerce_product_columns_wrapper_close' ) ) {
  141. /**
  142. * Product columns wrapper close
  143. *
  144. * @return void
  145. */
  146. function radcliffe_2_woocommerce_product_columns_wrapper_close() {
  147. echo '</div>';
  148. }
  149. }
  150. add_action( 'woocommerce_before_shop_loop', 'radcliffe_2_woocommerce_sorting_wrap', 3 );
  151. if ( ! function_exists( 'radcliffe_2_woocommerce_sorting_wrap' ) ) {
  152. /**
  153. * Sorting wrapper
  154. *
  155. * @return void
  156. */
  157. function radcliffe_2_woocommerce_sorting_wrap() {
  158. echo '<div class="woocommerce-sorting-wrap">';
  159. }
  160. }
  161. add_action( 'woocommerce_before_shop_loop', 'radcliffe_2_woocommerce_sorting_wrap_close', 30 );
  162. if ( ! function_exists( 'radcliffe_2_woocommerce_sorting_wrap_close' ) ) {
  163. /**
  164. * Sorting wrapper close
  165. *
  166. * @return void
  167. */
  168. function radcliffe_2_woocommerce_sorting_wrap_close() {
  169. echo '</div><!-- END .sorting-wrap -->';
  170. }
  171. }
  172. add_action( 'woocommerce_after_shop_loop', 'radcliffe_2_woocommerce_product_columns_wrapper_close', 40 );
  173. /**
  174. * Remove default WooCommerce wrapper
  175. */
  176. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  177. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  178. if ( ! function_exists( 'radcliffe_2_woocommerce_wrapper_before' ) ) {
  179. /**
  180. * Before Content
  181. * Wraps all WooCommerce content in wrappers which match the theme markup
  182. *
  183. * @return void
  184. */
  185. function radcliffe_2_woocommerce_wrapper_before() {
  186. ?>
  187. <div id="primary" class="content-area">
  188. <main id="main" class="site-main">
  189. <?php
  190. }
  191. }
  192. add_action( 'woocommerce_before_main_content', 'radcliffe_2_woocommerce_wrapper_before' );
  193. if ( ! function_exists( 'radcliffe_2_woocommerce_wrapper_after' ) ) {
  194. /**
  195. * After Content
  196. * Closes the wrapping divs
  197. *
  198. * @return void
  199. */
  200. function radcliffe_2_woocommerce_wrapper_after() {
  201. ?>
  202. </main><!-- #main -->
  203. </div><!-- #primary -->
  204. <?php
  205. }
  206. }
  207. add_action( 'woocommerce_after_main_content', 'radcliffe_2_woocommerce_wrapper_after' );
  208. if ( ! function_exists( 'radcliffe_2_woocommerce_cart_link_fragment' ) ) {
  209. /**
  210. * Cart Fragments
  211. * Ensure cart contents update when products are added to the cart via AJAX. Also adds cart icon SVG
  212. *
  213. * @param array $fragments Fragments to refresh via AJAX.
  214. * @return array Fragments to refresh via AJAX
  215. */
  216. function radcliffe_2_woocommerce_cart_link_fragment( $fragments ) {
  217. global $woocommerce;
  218. ob_start();
  219. radcliffe_2_woocommerce_cart_link();
  220. $fragments['a.cart-contents'] = ob_get_clean();
  221. return $fragments;
  222. }
  223. }
  224. add_filter( 'woocommerce_add_to_cart_fragments', 'radcliffe_2_woocommerce_cart_link_fragment' );
  225. if ( ! function_exists( 'radcliffe_2_woocommerce_cart_link' ) ) {
  226. /**
  227. * Cart Link
  228. * Displayed a link to the cart including the number of items present and the cart total
  229. *
  230. * @return void
  231. */
  232. function radcliffe_2_woocommerce_cart_link() {
  233. ?>
  234. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'radcliffe-2' ); ?>">
  235. <?php echo radcliffe_2_get_svg( array( 'icon' => 'cart', 'title' => esc_html__( 'View your shopping cart', 'radcliffe-2' ) ) ); ?>
  236. <?php /* translators: number of items in the mini cart. */ ?>
  237. <span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span>
  238. <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>
  239. </a>
  240. <?php
  241. }
  242. }
  243. if ( ! function_exists( 'radcliffe_2_woocommerce_header_cart' ) ) {
  244. /**
  245. * Display Header Cart
  246. *
  247. * @return void
  248. */
  249. function radcliffe_2_woocommerce_header_cart() {
  250. if ( is_cart() ) {
  251. $class = 'current-menu-item';
  252. } else {
  253. $class = '';
  254. }
  255. ?>
  256. <ul id="site-header-cart" class="site-header-cart">
  257. <li class="<?php echo esc_attr( $class ); ?>">
  258. <?php radcliffe_2_woocommerce_cart_link(); ?>
  259. </li>
  260. <li>
  261. <?php the_widget( 'WC_Widget_Cart', 'title=' ); ?>
  262. </li>
  263. </ul>
  264. <?php
  265. }
  266. }
  267. /**
  268. * Remove breadcrumbs
  269. */
  270. remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
  271. /**
  272. * Add breadcrumbs after
  273. *
  274. * We know the content wrapper is added at priority 10, so
  275. * let's add the breadcrumbs before that, at priority 5
  276. */
  277. add_action( 'woocommerce_before_shop_loop', 'woocommerce_breadcrumb', 5 );
  278. /**
  279. * Workaround to prevent is_shop() from failing due to WordPress core issue
  280. *
  281. * @link https://core.trac.wordpress.org/ticket/21790
  282. * @param array $args infinite scroll args.
  283. * @return array infinite scroll args.
  284. */
  285. function radcliffe_2_woocommerce_is_shop_page() {
  286. global $wp_query;
  287. $front_page_id = get_option( 'page_on_front' );
  288. $current_page_id = $wp_query->get( 'page_id' );
  289. $is_static_front_page = 'page' === get_option( 'show_on_front' );
  290. if ( $is_static_front_page && $front_page_id === $current_page_id ) {
  291. $is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
  292. } else {
  293. $is_shop_page = is_shop();
  294. }
  295. return $is_shop_page;
  296. }
  297. /**
  298. * Move the sale flash badge
  299. */
  300. remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
  301. add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 6 );
  302. remove_action ( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
  303. add_action ( 'woocommerce_single_product_summary', 'woocommerce_show_product_sale_flash', 5 );