woocommerce.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * @link https://docs.woocommerce.com/document/third-party-custom-theme-compatibility/
  13. * @link https://github.com/woocommerce/woocommerce/wiki/Enabling-product-gallery-features-(zoom,-swipe,-lightbox)-in-3.0.0
  14. *
  15. * @return void
  16. */
  17. function button_2_woocommerce_setup() {
  18. add_theme_support( 'woocommerce', array(
  19. 'thumbnail_image_width' => 584,
  20. 'single_image_width' => 584,
  21. ) );
  22. add_theme_support( 'wc-product-gallery-zoom' );
  23. add_theme_support( 'wc-product-gallery-lightbox' );
  24. add_theme_support( 'wc-product-gallery-slider' );
  25. }
  26. add_action( 'after_setup_theme', 'button_2_woocommerce_setup' );
  27. /**
  28. * WooCommerce specific scripts & stylesheets.
  29. *
  30. * @return void
  31. */
  32. function button_2_woocommerce_scripts() {
  33. wp_enqueue_style( 'button-2-woocommerce-style', get_template_directory_uri() . '/woocommerce.css' );
  34. wp_style_add_data( 'button-2-woocommerce-style', 'rtl', 'replace' );
  35. $font_path = WC()->plugin_url() . '/assets/fonts/';
  36. $inline_font = '@font-face {
  37. font-family: "star";
  38. src: url("' . $font_path . 'star.eot");
  39. src: url("' . $font_path . 'star.eot?#iefix") format("embedded-opentype"),
  40. url("' . $font_path . 'star.woff") format("woff"),
  41. url("' . $font_path . 'star.ttf") format("truetype"),
  42. url("' . $font_path . 'star.svg#star") format("svg");
  43. font-weight: normal;
  44. font-style: normal;
  45. }';
  46. wp_add_inline_style( 'button-2-woocommerce-style', $inline_font );
  47. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/assets/fonts/genericons/genericons.css', array(), '3.4.1' );
  48. }
  49. add_action( 'wp_enqueue_scripts', 'button_2_woocommerce_scripts' );
  50. /**
  51. * Disable the default WooCommerce stylesheet.
  52. *
  53. * Removing the default WooCommerce stylesheet and enqueing your own will
  54. * protect you during WooCommerce core updates.
  55. *
  56. * @link https://docs.woocommerce.com/document/disable-the-default-stylesheet/
  57. */
  58. add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
  59. /**
  60. * Add 'woocommerce-active' class to the body tag.
  61. *
  62. * @param array $classes CSS classes applied to the body tag.
  63. * @return array $classes modified to include 'woocommerce-active' class.
  64. */
  65. function button_2_woocommerce_active_body_class( $classes ) {
  66. $classes[] = 'woocommerce-active';
  67. return $classes;
  68. }
  69. add_filter( 'body_class', 'button_2_woocommerce_active_body_class' );
  70. /**
  71. * Products per page.
  72. *
  73. * @return integer number of products.
  74. */
  75. function button_2_woocommerce_products_per_page() {
  76. return absint( apply_filters( 'button_2_woocommerce_products_per_page', 12 ) );
  77. }
  78. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  79. add_filter( 'loop_shop_per_page', 'button_2_woocommerce_products_per_page' );
  80. }
  81. /**
  82. * Product gallery thumnbail columns.
  83. *
  84. * @return integer number of columns.
  85. */
  86. function button_2_woocommerce_thumbnail_columns() {
  87. return absint( apply_filters( 'button_2_woocommerce_product_thumbnail_columns', 4 ) );
  88. }
  89. add_filter( 'woocommerce_product_thumbnails_columns', 'button_2_woocommerce_thumbnail_columns' );
  90. /**
  91. * Default loop columns on product archives.
  92. *
  93. * @return integer products per row.
  94. */
  95. function button_2_woocommerce_loop_columns() {
  96. return absint( apply_filters( 'button_2_woocommerce_loop_columns', 3 ) );
  97. }
  98. add_filter( 'loop_shop_columns', 'button_2_woocommerce_loop_columns' );
  99. /**
  100. * Related Products Args.
  101. *
  102. * @param array $args related products args.
  103. * @return array $args related products args.
  104. */
  105. function button_2_woocommerce_related_products_args( $args ) {
  106. $args = apply_filters( 'radcliffe_2_woocommerce_related_products_args', array(
  107. 'posts_per_page' => 3,
  108. 'columns' => 3,
  109. ) );
  110. return $args;
  111. }
  112. add_filter( 'woocommerce_output_related_products_args', 'button_2_woocommerce_related_products_args' );
  113. if ( ! function_exists( 'button_2_woocommerce_product_columns_wrapper' ) ) {
  114. /**
  115. * Product columns wrapper.
  116. *
  117. * @return void
  118. */
  119. function button_2_woocommerce_product_columns_wrapper() {
  120. $columns = button_2_woocommerce_loop_columns();
  121. echo '<div class="columns columns-' . absint( $columns ) . '">';
  122. }
  123. }
  124. add_action( 'woocommerce_before_shop_loop', 'button_2_woocommerce_product_columns_wrapper', 40 );
  125. if ( ! function_exists( 'button_2_woocommerce_product_columns_wrapper_close' ) ) {
  126. /**
  127. * Product columns wrapper close.
  128. *
  129. * @return void
  130. */
  131. function button_2_woocommerce_product_columns_wrapper_close() {
  132. echo '</div>';
  133. }
  134. }
  135. add_action( 'woocommerce_after_shop_loop', 'button_2_woocommerce_product_columns_wrapper_close', 40 );
  136. /**
  137. * Remove default WooCommerce wrapper.
  138. */
  139. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  140. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  141. if ( ! function_exists( 'button_2_woocommerce_wrapper_before' ) ) {
  142. /**
  143. * Before Content.
  144. *
  145. * Wraps all WooCommerce content in wrappers which match the theme markup.
  146. *
  147. * @return void
  148. */
  149. function button_2_woocommerce_wrapper_before() {
  150. ?>
  151. <div id="primary" class="content-area">
  152. <main id="main" class="site-main" role="main">
  153. <?php
  154. }
  155. }
  156. add_action( 'woocommerce_before_main_content', 'button_2_woocommerce_wrapper_before' );
  157. if ( ! function_exists( 'button_2_woocommerce_wrapper_after' ) ) {
  158. /**
  159. * After Content.
  160. *
  161. * Closes the wrapping divs.
  162. *
  163. * @return void
  164. */
  165. function button_2_woocommerce_wrapper_after() {
  166. ?>
  167. </main><!-- #main -->
  168. </div><!-- #primary -->
  169. <?php
  170. }
  171. }
  172. add_action( 'woocommerce_after_main_content', 'button_2_woocommerce_wrapper_after' );
  173. if ( ! function_exists( 'button_2_woocommerce_cart_link_fragment' ) ) {
  174. /**
  175. * Cart Fragments.
  176. *
  177. * Ensure cart contents update when products are added to the cart via AJAX.
  178. *
  179. * @param array $fragments Fragments to refresh via AJAX.
  180. * @return array Fragments to refresh via AJAX.
  181. */
  182. function button_2_woocommerce_cart_link_fragment( $fragments ) {
  183. ob_start();
  184. button_2_woocommerce_cart_link();
  185. $fragments['a.cart-contents'] = ob_get_clean();
  186. return $fragments;
  187. }
  188. }
  189. add_filter( 'woocommerce_add_to_cart_fragments', 'button_2_woocommerce_cart_link_fragment' );
  190. if ( ! function_exists( 'button_2_woocommerce_cart_link' ) ) {
  191. /**
  192. * Cart Link.
  193. *
  194. * Displayed a link to the cart including the number of items present and the cart total.
  195. *
  196. * @return void
  197. */
  198. function button_2_woocommerce_cart_link() {
  199. ?>
  200. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', '_s' ); ?>">
  201. <?php /* translators: number of items in the mini cart. */ ?>
  202. <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>
  203. </a>
  204. <?php
  205. }
  206. }
  207. if ( ! function_exists( 'button_2_woocommerce_header_cart' ) ) {
  208. /**
  209. * Display Header Cart.
  210. *
  211. * @return void
  212. */
  213. function button_2_woocommerce_header_cart() {
  214. if ( is_cart() ) {
  215. $class = 'current-menu-item';
  216. } else {
  217. $class = '';
  218. }
  219. ?>
  220. <ul id="site-header-cart" class="site-header-cart">
  221. <li class="<?php echo esc_attr( $class ); ?>">
  222. <?php button_2_woocommerce_cart_link(); ?>
  223. </li>
  224. <li>
  225. <?php
  226. $instance = array(
  227. 'title' => '',
  228. );
  229. the_widget( 'WC_Widget_Cart', $instance );
  230. ?>
  231. </li>
  232. </ul>
  233. <?php
  234. }
  235. }
  236. /**
  237. * Workaround to prevent is_shop() from failing due to WordPress core issue
  238. *
  239. * @link https://core.trac.wordpress.org/ticket/21790
  240. * @param array $args infinite scroll args.
  241. * @return array infinite scroll args.
  242. */
  243. function button_2_woocommerce_is_shop_page() {
  244. global $wp_query;
  245. $front_page_id = get_option( 'page_on_front' );
  246. $current_page_id = $wp_query->get( 'page_id' );
  247. $is_static_front_page = 'page' === get_option( 'show_on_front' );
  248. if ( $is_static_front_page && $front_page_id === $current_page_id ) {
  249. $is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
  250. } else {
  251. $is_shop_page = is_shop();
  252. }
  253. return $is_shop_page;
  254. }
  255. /**
  256. * Override number of products per page in Jetpack infinite scroll.
  257. *
  258. * @param array $args infinite scroll args.
  259. * @return array infinite scroll args.
  260. */
  261. function button_2_woocommerce_jetpack_products_per_page( $args ) {
  262. if ( is_array( $args ) && ( button_2_woocommerce_is_shop_page() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
  263. $args['posts_per_page'] = button_2_woocommerce_products_per_page();
  264. }
  265. return $args;
  266. }
  267. add_filter( 'infinite_scroll_settings', 'button_2_woocommerce_jetpack_products_per_page' );