woocommerce.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * WooCommerce Compatibility File
  4. *
  5. * @link https://woocommerce.com/
  6. *
  7. * @package _s
  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 shoreditch_woocommerce_setup() {
  17. add_theme_support( 'woocommerce', apply_filters( 'shoreditch_woocommerce_args', array(
  18. 'single_image_width' => 580,
  19. 'thumbnail_image_width' => 290,
  20. 'product_grid' => array(
  21. 'default_columns' => 2,
  22. 'default_rows' => 3,
  23. 'min_columns' => 1,
  24. 'max_columns' => 5,
  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', 'shoreditch_woocommerce_setup' );
  33. /**
  34. * WooCommerce specific scripts & stylesheets
  35. *
  36. * @return void
  37. */
  38. function shoreditch_woocommerce_scripts() {
  39. wp_enqueue_style( 'shoreditch-woocommerce-style', get_template_directory_uri() . '/woocommerce.css' );
  40. wp_style_add_data( 'shoreditch-woocommerce-style', 'rtl', 'replace' );
  41. }
  42. add_action( 'wp_enqueue_scripts', 'shoreditch_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 shoreditch_woocommerce_active_body_class( $classes ) {
  59. $classes[] = 'woocommerce-active';
  60. return $classes;
  61. }
  62. add_filter( 'body_class', 'shoreditch_woocommerce_active_body_class' );
  63. /**
  64. * Products per page
  65. *
  66. * @return integer number of products
  67. */
  68. function shoreditch_woocommerce_products_per_page() {
  69. return absint( apply_filters( 'shoreditch_woocommerce_products_per_page', 6 ) );
  70. }
  71. // Legacy WooCommerce products per page filter.
  72. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  73. add_filter( 'loop_shop_per_page', 'shoreditch_woocommerce_products_per_page' );
  74. }
  75. /**
  76. * Product gallery thumnbail columns
  77. *
  78. * @return integer number of columns
  79. */
  80. function shoreditch_woocommerce_thumbnail_columns() {
  81. return absint( apply_filters( 'shoreditch_woocommerce_product_thumbnail_columns', 4 ) );
  82. }
  83. add_filter( 'woocommerce_product_thumbnails_columns', 'shoreditch_woocommerce_thumbnail_columns' );
  84. /**
  85. * Default loop columns on product archives
  86. *
  87. * @return integer products per row
  88. */
  89. function shoreditch_woocommerce_loop_columns() {
  90. return absint( apply_filters( 'shoreditch_woocommerce_loop_columns', 2 ) );
  91. }
  92. // Legacy WooCommerce columns filter.
  93. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  94. add_filter( 'loop_shop_columns', 'shoreditch_woocommerce_loop_columns' );
  95. }
  96. /**
  97. * Related Products Args
  98. *
  99. * @param array $args related products args.
  100. * @return array $args related products args
  101. */
  102. function shoreditch_woocommerce_related_products_args( $args ) {
  103. $args = apply_filters( 'shoreditch_woocommerce_related_products_args', array(
  104. 'posts_per_page' => 2,
  105. 'columns' => 2,
  106. ) );
  107. return $args;
  108. }
  109. add_filter( 'woocommerce_output_related_products_args', 'shoreditch_woocommerce_related_products_args' );
  110. if ( ! function_exists( 'shoreditch_woocommerce_product_columns_wrapper' ) ) {
  111. /**
  112. * Product columns wrapper
  113. *
  114. * @return void
  115. */
  116. function shoreditch_woocommerce_product_columns_wrapper() {
  117. $columns = shoreditch_loop_columns();
  118. echo '<div class="columns-' . absint( $columns ) . '">';
  119. }
  120. }
  121. add_action( 'woocommerce_before_shop_loop', 'shoreditch_woocommerce_product_columns_wrapper', 40 );
  122. if ( ! function_exists( 'shoreditch_loop_columns' ) ) {
  123. /**
  124. * Default loop columns on product archives
  125. *
  126. * @return integer products per row
  127. */
  128. function shoreditch_loop_columns() {
  129. $columns = 2; // 2 products per row
  130. if ( function_exists( 'wc_get_default_products_per_row' ) ) {
  131. $columns = wc_get_default_products_per_row();
  132. }
  133. return apply_filters( 'shoreditch_loop_columns', $columns );
  134. }
  135. }
  136. if ( ! function_exists( 'shoreditch_woocommerce_product_columns_wrapper_close' ) ) {
  137. /**
  138. * Product columns wrapper close
  139. *
  140. * @return void
  141. */
  142. function shoreditch_woocommerce_product_columns_wrapper_close() {
  143. echo '</div>';
  144. }
  145. }
  146. add_action( 'woocommerce_after_shop_loop', 'shoreditch_woocommerce_product_columns_wrapper_close', 40 );
  147. if ( ! function_exists( 'shoreditch_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 shoreditch_woocommerce_wrapper_before() {
  155. ?>
  156. <div class="site-content-wrapper">
  157. <div id="primary" class="content-area">
  158. <main id="main" class="site-main" role="main">
  159. <div class="hentry">
  160. <div class="hentry-wrapper">
  161. <?php
  162. }
  163. }
  164. add_action( 'woocommerce_before_main_content', 'shoreditch_woocommerce_wrapper_before' );
  165. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  166. if ( ! function_exists( 'shoreditch_woocommerce_wrapper_after' ) ) {
  167. /**
  168. * After Content
  169. * Closes the wrapping divs
  170. *
  171. * @return void
  172. */
  173. function shoreditch_woocommerce_wrapper_after() {
  174. ?>
  175. </div><!-- .hentry-wrapper -->
  176. </div><!-- .hentry -->
  177. </main><!-- #main -->
  178. </div><!-- #primary -->
  179. <?php get_sidebar(); ?>
  180. </div><!-- .site-content-wrapper -->
  181. <?php
  182. }
  183. }
  184. add_action( 'woocommerce_after_main_content', 'shoreditch_woocommerce_wrapper_after' );
  185. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  186. if ( ! function_exists( 'shoreditch_woocommerce_cart_link_fragment' ) ) {
  187. /**
  188. * Cart Fragments
  189. * Ensure cart contents update when products are added to the cart via AJAX
  190. *
  191. * @param array $fragments Fragments to refresh via AJAX.
  192. * @return array Fragments to refresh via AJAX
  193. */
  194. function shoreditch_woocommerce_cart_link_fragment( $fragments ) {
  195. global $woocommerce;
  196. ob_start();
  197. shoreditch_woocommerce_cart_link();
  198. $fragments['a.cart-contents'] = ob_get_clean();
  199. return $fragments;
  200. }
  201. }
  202. add_filter( 'woocommerce_add_to_cart_fragments', 'shoreditch_woocommerce_cart_link_fragment' );
  203. if ( ! function_exists( 'shoreditch_woocommerce_cart_link' ) ) {
  204. /**
  205. * Cart Link
  206. * Displayed a link to the cart including the number of items present and the cart total
  207. *
  208. * @return void
  209. */
  210. function shoreditch_woocommerce_cart_link() {
  211. ?>
  212. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', '_s' ); ?>">
  213. <span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span> <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), '_s' ), WC()->cart->get_cart_contents_count() ) );?></span>
  214. </a>
  215. <?php
  216. }
  217. }
  218. if ( ! function_exists( 'shoreditch_woocommerce_header_cart' ) ) {
  219. /**
  220. * Display Header Cart
  221. *
  222. * @return void
  223. */
  224. function shoreditch_woocommerce_header_cart() {
  225. if ( is_cart() ) {
  226. $class = 'current-menu-item';
  227. } else {
  228. $class = '';
  229. }
  230. ?>
  231. <ul id="site-header-cart" class="site-header-cart">
  232. <li class="<?php echo esc_attr( $class ); ?>">
  233. <?php shoreditch_woocommerce_cart_link(); ?>
  234. </li>
  235. <li>
  236. <?php the_widget( 'WC_Widget_Cart', 'title=' ); ?>
  237. </li>
  238. </ul>
  239. <?php
  240. }
  241. }
  242. /**
  243. * Workaround to prevent is_shop() from failing due to WordPress core issue
  244. *
  245. * @link https://core.trac.wordpress.org/ticket/21790
  246. * @param array $args infinite scroll args.
  247. * @return array infinite scroll args.
  248. */
  249. function shoreditch_woocommerce_is_shop_page() {
  250. global $wp_query;
  251. $front_page_id = get_option( 'page_on_front' );
  252. $current_page_id = $wp_query->get( 'page_id' );
  253. $is_static_front_page = 'page' === get_option( 'show_on_front' );
  254. if ( $is_static_front_page && $front_page_id === $current_page_id ) {
  255. $is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
  256. } else {
  257. $is_shop_page = is_shop();
  258. }
  259. return $is_shop_page;
  260. }