woocommerce.php 9.8 KB

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