woocommerce.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * WooCommerce Compatibility File
  4. *
  5. * @link https://woocommerce.com/
  6. *
  7. * @package Libre 2
  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 libre_2_woocommerce_setup() {
  18. add_theme_support( 'woocommerce', apply_filters( 'libre_2_woocommerce_args', array(
  19. 'single_image_width' => 823,
  20. 'thumbnail_image_width' => 450,
  21. 'product_grid' => array(
  22. 'default_columns' => 3,
  23. 'default_rows' => 4,
  24. 'min_columns' => 1,
  25. 'max_columns' => 6,
  26. 'min_rows' => 1
  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', 'libre_2_woocommerce_setup' );
  34. /**
  35. * WooCommerce specific scripts & stylesheets.
  36. *
  37. * @return void
  38. */
  39. function libre_2_woocommerce_scripts() {
  40. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons/genericons.css', array(), '3.4.1' );
  41. wp_enqueue_style( 'libre-2-woocommerce-style', get_template_directory_uri() . '/woocommerce.css' );
  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( 'libre-2-woocommerce-style', $inline_font );
  54. }
  55. add_action( 'wp_enqueue_scripts', 'libre_2_woocommerce_scripts' );
  56. /**
  57. * Disable the default WooCommerce stylesheet.
  58. *
  59. * Removing the default WooCommerce stylesheet and enqueing your own will
  60. * protect you during WooCommerce core updates.
  61. *
  62. * @link https://docs.woocommerce.com/document/disable-the-default-stylesheet/
  63. */
  64. add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
  65. /**
  66. * Add 'woocommerce-active' class to the body tag.
  67. *
  68. * @param array $classes CSS classes applied to the body tag.
  69. * @return array $classes modified to include 'woocommerce-active' class.
  70. */
  71. function libre_2_woocommerce_active_body_class( $classes ) {
  72. $classes[] = 'woocommerce-active';
  73. return $classes;
  74. }
  75. add_filter( 'body_class', 'libre_2_woocommerce_active_body_class' );
  76. /**
  77. * Products per page.
  78. *
  79. * @return integer number of products.
  80. */
  81. function libre_2_woocommerce_products_per_page() {
  82. return absint( apply_filters( 'libre_2_woocommerce_products_per_page', 12 ) );
  83. }
  84. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  85. add_filter( 'loop_shop_per_page', 'libre_2_woocommerce_products_per_page' );
  86. }
  87. // Legacy WooCommerce products per page filter.
  88. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  89. add_filter( 'loop_shop_per_page', 'libre_2_woocommerce_products_per_page' );
  90. }
  91. /**
  92. * Product gallery thumnbail columns.
  93. *
  94. * @return integer number of columns.
  95. */
  96. function libre_2_woocommerce_thumbnail_columns() {
  97. return absint( apply_filters( 'libre_2_woocommerce_product_thumbnail_columns', 4 ) );
  98. }
  99. add_filter( 'woocommerce_product_thumbnails_columns', 'libre_2_woocommerce_thumbnail_columns' );
  100. /**
  101. * Default loop columns on product archives.
  102. *
  103. * @return integer products per row.
  104. */
  105. function libre_2_woocommerce_loop_columns() {
  106. return absint( apply_filters( 'libre_2_woocommerce_loop_columns', 3 ) );
  107. }
  108. // Legacy WooCommerce columns filter.
  109. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  110. add_filter( 'loop_shop_columns', 'libre_2_woocommerce_loop_columns' );
  111. }
  112. /**
  113. * Related Products Args.
  114. *
  115. * @param array $args related products args.
  116. * @return array $args related products args.
  117. */
  118. function libre_2_woocommerce_related_products_args( $args ) {
  119. $args = apply_filters( 'libre_2_woocommerce_related_products_args', array(
  120. 'posts_per_page' => 3,
  121. 'columns' => 3,
  122. ) );
  123. return $args;
  124. }
  125. add_filter( 'woocommerce_output_related_products_args', 'libre_2_woocommerce_related_products_args' );
  126. if ( ! function_exists( 'libre_2_woocommerce_product_columns_wrapper' ) ) {
  127. /**
  128. * Product columns wrapper.
  129. *
  130. * @return void
  131. */
  132. function libre_2_woocommerce_product_columns_wrapper() {
  133. $columns = libre_2_loop_columns();
  134. echo '<div class="columns-' . absint( $columns ) . '">';
  135. }
  136. }
  137. add_action( 'woocommerce_before_shop_loop', 'libre_2_woocommerce_product_columns_wrapper', 40 );
  138. if ( ! function_exists( 'libre_2_loop_columns' ) ) {
  139. /**
  140. * Default loop columns on product archives
  141. *
  142. * @return integer products per row
  143. */
  144. function libre_2_loop_columns() {
  145. $columns = 3; // 3 products per row
  146. if ( function_exists( 'wc_get_default_products_per_row' ) ) {
  147. $columns = wc_get_default_products_per_row();
  148. }
  149. return apply_filters( 'libre_2_loop_columns', $columns );
  150. }
  151. }
  152. if ( ! function_exists( 'libre_2_woocommerce_product_columns_wrapper_close' ) ) {
  153. /**
  154. * Product columns wrapper close.
  155. *
  156. * @return void
  157. */
  158. function libre_2_woocommerce_product_columns_wrapper_close() {
  159. echo '</div>';
  160. }
  161. }
  162. add_action( 'woocommerce_after_shop_loop', 'libre_2_woocommerce_product_columns_wrapper_close', 40 );
  163. add_action( 'woocommerce_before_shop_loop', 'libre_2_woocommerce_sorting_wrap', 3 );
  164. if ( ! function_exists( 'libre_2_woocommerce_sorting_wrap' ) ) {
  165. /**
  166. * Sorting wrapper
  167. *
  168. * @return void
  169. */
  170. function libre_2_woocommerce_sorting_wrap() {
  171. echo '<div class="woocommerce-sorting-wrap">';
  172. }
  173. }
  174. add_action( 'woocommerce_before_shop_loop', 'libre_2_woocommerce_sorting_wrap_close', 30 );
  175. if ( ! function_exists( 'libre_2_woocommerce_sorting_wrap_close' ) ) {
  176. /**
  177. * Sorting wrapper close
  178. *
  179. * @return void
  180. */
  181. function libre_2_woocommerce_sorting_wrap_close() {
  182. echo '</div><!-- END .sorting-wrap -->';
  183. }
  184. }
  185. /**
  186. * Remove default WooCommerce wrapper.
  187. */
  188. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  189. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  190. if ( ! function_exists( 'libre_2_woocommerce_wrapper_before' ) ) {
  191. /**
  192. * Before Content.
  193. *
  194. * Wraps all WooCommerce content in wrappers which match the theme markup.
  195. *
  196. * @return void
  197. */
  198. function libre_2_woocommerce_wrapper_before() {
  199. ?>
  200. <div id="primary" class="content-area">
  201. <main id="main" class="site-main" role="main">
  202. <?php
  203. }
  204. }
  205. add_action( 'woocommerce_before_main_content', 'libre_2_woocommerce_wrapper_before' );
  206. if ( ! function_exists( 'libre_2_woocommerce_wrapper_after' ) ) {
  207. /**
  208. * After Content.
  209. *
  210. * Closes the wrapping divs.
  211. *
  212. * @return void
  213. */
  214. function libre_2_woocommerce_wrapper_after() {
  215. ?>
  216. </main><!-- #main -->
  217. </div><!-- #primary -->
  218. <?php
  219. }
  220. }
  221. add_action( 'woocommerce_after_main_content', 'libre_2_woocommerce_wrapper_after' );
  222. if ( ! function_exists( 'libre_2_woocommerce_cart_link_fragment' ) ) {
  223. /**
  224. * Cart Fragments.
  225. *
  226. * Ensure cart contents update when products are added to the cart via AJAX.
  227. *
  228. * @param array $fragments Fragments to refresh via AJAX.
  229. * @return array Fragments to refresh via AJAX.
  230. */
  231. function libre_2_woocommerce_cart_link_fragment( $fragments ) {
  232. ob_start();
  233. libre_2_woocommerce_cart_link();
  234. $fragments['a.cart-contents'] = ob_get_clean();
  235. return $fragments;
  236. }
  237. }
  238. add_filter( 'woocommerce_add_to_cart_fragments', 'libre_2_woocommerce_cart_link_fragment' );
  239. if ( ! function_exists( 'libre_2_woocommerce_cart_link' ) ) {
  240. /**
  241. * Cart Link.
  242. *
  243. * Displayed a link to the cart including the number of items present and the cart total.
  244. *
  245. * @return void
  246. */
  247. function libre_2_woocommerce_cart_link() {
  248. ?>
  249. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'libre-2' ); ?>">
  250. <?php /* translators: number of items in the mini cart. */ ?>
  251. <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(), 'libre-2' ), WC()->cart->get_cart_contents_count() ) );?></span>
  252. </a>
  253. <?php
  254. }
  255. }
  256. if ( ! function_exists( 'libre_2_woocommerce_header_cart' ) ) {
  257. /**
  258. * Display Header Cart.
  259. *
  260. * @return void
  261. */
  262. function libre_2_woocommerce_header_cart() {
  263. if ( is_cart() ) {
  264. $class = 'current-menu-item';
  265. } else {
  266. $class = '';
  267. }
  268. ?>
  269. <ul id="site-header-cart" class="site-header-cart">
  270. <li class="<?php echo esc_attr( $class ); ?>">
  271. <?php libre_2_woocommerce_cart_link(); ?>
  272. </li>
  273. <li>
  274. <?php
  275. $instance = array(
  276. 'title' => '',
  277. );
  278. the_widget( 'WC_Widget_Cart', $instance );
  279. ?>
  280. </li>
  281. </ul>
  282. <?php
  283. }
  284. }
  285. /**
  286. * Workaround to prevent is_shop() from failing due to WordPress core issue
  287. *
  288. * @link https://core.trac.wordpress.org/ticket/21790
  289. * @param array $args infinite scroll args.
  290. * @return array infinite scroll args.
  291. */
  292. function libre_2_woocommerce_is_shop_page() {
  293. global $wp_query;
  294. $front_page_id = get_option( 'page_on_front' );
  295. $current_page_id = $wp_query->get( 'page_id' );
  296. $is_static_front_page = 'page' === get_option( 'show_on_front' );
  297. if ( $is_static_front_page && $front_page_id === $current_page_id ) {
  298. $is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
  299. } else {
  300. $is_shop_page = is_shop();
  301. }
  302. return $is_shop_page;
  303. }
  304. /**
  305. * Override number of products per page in Jetpack infinite scroll.
  306. *
  307. * @param array $args infinite scroll args.
  308. * @return array infinite scroll args.
  309. */
  310. function libre_2_woocommerce_jetpack_products_per_page( $args ) {
  311. if ( is_array( $args ) && ( libre_2_woocommerce_is_shop_page() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
  312. $args['posts_per_page'] = libre_2_woocommerce_products_per_page();
  313. }
  314. return $args;
  315. }
  316. add_filter( 'infinite_scroll_settings', 'libre_2_woocommerce_jetpack_products_per_page' );