woocommerce.php 8.9 KB

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