woocommerce.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 12;
  77. }
  78. add_filter( 'loop_shop_per_page', 'button_2_woocommerce_products_per_page' );
  79. /**
  80. * Product gallery thumnbail columns.
  81. *
  82. * @return integer number of columns.
  83. */
  84. function button_2_woocommerce_thumbnail_columns() {
  85. return 4;
  86. }
  87. add_filter( 'woocommerce_product_thumbnails_columns', 'button_2_woocommerce_thumbnail_columns' );
  88. /**
  89. * Default loop columns on product archives.
  90. *
  91. * @return integer products per row.
  92. */
  93. function button_2_woocommerce_loop_columns() {
  94. return 3;
  95. }
  96. add_filter( 'loop_shop_columns', 'button_2_woocommerce_loop_columns' );
  97. /**
  98. * Related Products Args.
  99. *
  100. * @param array $args related products args.
  101. * @return array $args related products args.
  102. */
  103. function button_2_woocommerce_related_products_args( $args ) {
  104. $defaults = array(
  105. 'posts_per_page' => 3,
  106. 'columns' => 3,
  107. );
  108. $args = wp_parse_args( $defaults, $args );
  109. return $args;
  110. }
  111. add_filter( 'woocommerce_output_related_products_args', 'button_2_woocommerce_related_products_args' );
  112. if ( ! function_exists( 'button_2_woocommerce_product_columns_wrapper' ) ) {
  113. /**
  114. * Product columns wrapper.
  115. *
  116. * @return void
  117. */
  118. function button_2_woocommerce_product_columns_wrapper() {
  119. $columns = button_2_woocommerce_loop_columns();
  120. echo '<div class="columns columns-' . absint( $columns ) . '">';
  121. }
  122. }
  123. add_action( 'woocommerce_before_shop_loop', 'button_2_woocommerce_product_columns_wrapper', 40 );
  124. if ( ! function_exists( 'button_2_woocommerce_product_columns_wrapper_close' ) ) {
  125. /**
  126. * Product columns wrapper close.
  127. *
  128. * @return void
  129. */
  130. function button_2_woocommerce_product_columns_wrapper_close() {
  131. echo '</div>';
  132. }
  133. }
  134. add_action( 'woocommerce_after_shop_loop', 'button_2_woocommerce_product_columns_wrapper_close', 40 );
  135. /**
  136. * Remove default WooCommerce wrapper.
  137. */
  138. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  139. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  140. if ( ! function_exists( 'button_2_woocommerce_wrapper_before' ) ) {
  141. /**
  142. * Before Content.
  143. *
  144. * Wraps all WooCommerce content in wrappers which match the theme markup.
  145. *
  146. * @return void
  147. */
  148. function button_2_woocommerce_wrapper_before() {
  149. ?>
  150. <div id="primary" class="content-area">
  151. <main id="main" class="site-main" role="main">
  152. <?php
  153. }
  154. }
  155. add_action( 'woocommerce_before_main_content', 'button_2_woocommerce_wrapper_before' );
  156. if ( ! function_exists( 'button_2_woocommerce_wrapper_after' ) ) {
  157. /**
  158. * After Content.
  159. *
  160. * Closes the wrapping divs.
  161. *
  162. * @return void
  163. */
  164. function button_2_woocommerce_wrapper_after() {
  165. ?>
  166. </main><!-- #main -->
  167. </div><!-- #primary -->
  168. <?php
  169. }
  170. }
  171. add_action( 'woocommerce_after_main_content', 'button_2_woocommerce_wrapper_after' );
  172. if ( ! function_exists( 'button_2_woocommerce_cart_link_fragment' ) ) {
  173. /**
  174. * Cart Fragments.
  175. *
  176. * Ensure cart contents update when products are added to the cart via AJAX.
  177. *
  178. * @param array $fragments Fragments to refresh via AJAX.
  179. * @return array Fragments to refresh via AJAX.
  180. */
  181. function button_2_woocommerce_cart_link_fragment( $fragments ) {
  182. ob_start();
  183. button_2_woocommerce_cart_link();
  184. $fragments['a.cart-contents'] = ob_get_clean();
  185. return $fragments;
  186. }
  187. }
  188. add_filter( 'woocommerce_add_to_cart_fragments', 'button_2_woocommerce_cart_link_fragment' );
  189. if ( ! function_exists( 'button_2_woocommerce_cart_link' ) ) {
  190. /**
  191. * Cart Link.
  192. *
  193. * Displayed a link to the cart including the number of items present and the cart total.
  194. *
  195. * @return void
  196. */
  197. function button_2_woocommerce_cart_link() {
  198. ?>
  199. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', '_s' ); ?>">
  200. <?php /* translators: number of items in the mini cart. */ ?>
  201. <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>
  202. </a>
  203. <?php
  204. }
  205. }
  206. if ( ! function_exists( 'button_2_woocommerce_header_cart' ) ) {
  207. /**
  208. * Display Header Cart.
  209. *
  210. * @return void
  211. */
  212. function button_2_woocommerce_header_cart() {
  213. if ( is_cart() ) {
  214. $class = 'current-menu-item';
  215. } else {
  216. $class = '';
  217. }
  218. ?>
  219. <ul id="site-header-cart" class="site-header-cart">
  220. <li class="<?php echo esc_attr( $class ); ?>">
  221. <?php button_2_woocommerce_cart_link(); ?>
  222. </li>
  223. <li>
  224. <?php
  225. $instance = array(
  226. 'title' => '',
  227. );
  228. the_widget( 'WC_Widget_Cart', $instance );
  229. ?>
  230. </li>
  231. </ul>
  232. <?php
  233. }
  234. }
  235. /**
  236. * Workaround to prevent is_shop() from failing due to WordPress core issue
  237. *
  238. * @link https://core.trac.wordpress.org/ticket/21790
  239. * @param array $args infinite scroll args.
  240. * @return array infinite scroll args.
  241. */
  242. function button_2_woocommerce_is_shop_page() {
  243. global $wp_query;
  244. $front_page_id = get_option( 'page_on_front' );
  245. $current_page_id = $wp_query->get( 'page_id' );
  246. $is_static_front_page = 'page' === get_option( 'show_on_front' );
  247. if ( $is_static_front_page && $front_page_id === $current_page_id ) {
  248. $is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
  249. } else {
  250. $is_shop_page = is_shop();
  251. }
  252. return $is_shop_page;
  253. }
  254. /**
  255. * Jetpack infinite scroll duplicates posts where orderby is anything other than modified or date
  256. * This filter offsets the products returned by however many are displayed per page
  257. *
  258. * @link https://github.com/Automattic/jetpack/issues/1135
  259. * @param array $args infinite scroll args.
  260. * @return array infinite scroll args.
  261. */
  262. function button_2_woocommerce_jetpack_duplicate_products( $args ) {
  263. if ( ( isset( $args['post_type'] ) && 'product' === $args['post_type'] ) || ( isset( $args['taxonomy'] ) && 'product_cat' === $args['taxonomy'] ) ) {
  264. $args['offset'] = $args['posts_per_page'] * $args['paged'];
  265. }
  266. return $args;
  267. }
  268. add_filter( 'infinite_scroll_query_args', 'button_2_woocommerce_jetpack_duplicate_products', 100 );
  269. /**
  270. * Override number of products per page in Jetpack infinite scroll.
  271. *
  272. * @param array $args infinite scroll args.
  273. * @return array infinite scroll args.
  274. */
  275. function button_2_woocommerce_jetpack_products_per_page( $args ) {
  276. if ( is_array( $args ) && ( button_2_woocommerce_is_shop_page() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
  277. $args['posts_per_page'] = button_2_woocommerce_products_per_page();
  278. }
  279. return $args;
  280. }
  281. add_filter( 'infinite_scroll_settings', 'button_2_woocommerce_jetpack_products_per_page' );