woocommerce.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. wp_style_add_data( 'libre-2-woocommerce-style', 'rtl', get_stylesheet_directory_uri() . '/woocommerce-rtl.css' );
  43. $font_path = WC()->plugin_url() . '/assets/fonts/';
  44. $inline_font = '@font-face {
  45. font-family: "star";
  46. src: url("' . $font_path . 'star.eot");
  47. src: url("' . $font_path . 'star.eot?#iefix") format("embedded-opentype"),
  48. url("' . $font_path . 'star.woff") format("woff"),
  49. url("' . $font_path . 'star.ttf") format("truetype"),
  50. url("' . $font_path . 'star.svg#star") format("svg");
  51. font-weight: normal;
  52. font-style: normal;
  53. }';
  54. wp_add_inline_style( 'libre-2-woocommerce-style', $inline_font );
  55. }
  56. add_action( 'wp_enqueue_scripts', 'libre_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 libre_2_woocommerce_active_body_class( $classes ) {
  73. $classes[] = 'woocommerce-active';
  74. return $classes;
  75. }
  76. add_filter( 'body_class', 'libre_2_woocommerce_active_body_class' );
  77. /**
  78. * Products per page.
  79. *
  80. * @return integer number of products.
  81. */
  82. function libre_2_woocommerce_products_per_page() {
  83. return absint( apply_filters( 'libre_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', '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 absint( apply_filters( 'libre_2_woocommerce_product_thumbnail_columns', 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 absint( apply_filters( 'libre_2_woocommerce_loop_columns', 3 ) );
  104. }
  105. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  106. add_filter( 'loop_shop_columns', 'libre_2_woocommerce_loop_columns' );
  107. }
  108. /**
  109. * Related Products Args.
  110. *
  111. * @param array $args related products args.
  112. * @return array $args related products args.
  113. */
  114. function libre_2_woocommerce_related_products_args( $args ) {
  115. $args = apply_filters( 'libre_2_woocommerce_related_products_args', array(
  116. 'posts_per_page' => 3,
  117. 'columns' => 3,
  118. ) );
  119. return $args;
  120. }
  121. add_filter( 'woocommerce_output_related_products_args', 'libre_2_woocommerce_related_products_args' );
  122. if ( ! function_exists( 'libre_2_woocommerce_product_columns_wrapper' ) ) {
  123. /**
  124. * Product columns wrapper.
  125. *
  126. * @return void
  127. */
  128. function libre_2_woocommerce_product_columns_wrapper() {
  129. $columns = libre_2_loop_columns();
  130. echo '<div class="columns-' . absint( $columns ) . '">';
  131. }
  132. }
  133. add_action( 'woocommerce_before_shop_loop', 'libre_2_woocommerce_product_columns_wrapper', 40 );
  134. if ( ! function_exists( 'libre_2_loop_columns' ) ) {
  135. /**
  136. * Default loop columns on product archives
  137. *
  138. * @return integer products per row
  139. */
  140. function libre_2_loop_columns() {
  141. $columns = 3; // 3 products per row
  142. if ( function_exists( 'wc_get_default_products_per_row' ) ) {
  143. $columns = wc_get_default_products_per_row();
  144. }
  145. return apply_filters( 'libre_2_loop_columns', $columns );
  146. }
  147. }
  148. if ( ! function_exists( 'libre_2_woocommerce_product_columns_wrapper_close' ) ) {
  149. /**
  150. * Product columns wrapper close.
  151. *
  152. * @return void
  153. */
  154. function libre_2_woocommerce_product_columns_wrapper_close() {
  155. echo '</div>';
  156. }
  157. }
  158. add_action( 'woocommerce_after_shop_loop', 'libre_2_woocommerce_product_columns_wrapper_close', 40 );
  159. add_action( 'woocommerce_before_shop_loop', 'libre_2_woocommerce_sorting_wrap', 3 );
  160. if ( ! function_exists( 'libre_2_woocommerce_sorting_wrap' ) ) {
  161. /**
  162. * Sorting wrapper
  163. *
  164. * @return void
  165. */
  166. function libre_2_woocommerce_sorting_wrap() {
  167. echo '<div class="woocommerce-sorting-wrap">';
  168. }
  169. }
  170. add_action( 'woocommerce_before_shop_loop', 'libre_2_woocommerce_sorting_wrap_close', 30 );
  171. if ( ! function_exists( 'libre_2_woocommerce_sorting_wrap_close' ) ) {
  172. /**
  173. * Sorting wrapper close
  174. *
  175. * @return void
  176. */
  177. function libre_2_woocommerce_sorting_wrap_close() {
  178. echo '</div><!-- END .sorting-wrap -->';
  179. }
  180. }
  181. /**
  182. * Remove default WooCommerce wrapper.
  183. */
  184. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  185. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  186. if ( ! function_exists( 'libre_2_woocommerce_wrapper_before' ) ) {
  187. /**
  188. * Before Content.
  189. *
  190. * Wraps all WooCommerce content in wrappers which match the theme markup.
  191. *
  192. * @return void
  193. */
  194. function libre_2_woocommerce_wrapper_before() {
  195. ?>
  196. <div id="primary" class="content-area">
  197. <main id="main" class="site-main" role="main">
  198. <?php
  199. }
  200. }
  201. add_action( 'woocommerce_before_main_content', 'libre_2_woocommerce_wrapper_before' );
  202. if ( ! function_exists( 'libre_2_woocommerce_wrapper_after' ) ) {
  203. /**
  204. * After Content.
  205. *
  206. * Closes the wrapping divs.
  207. *
  208. * @return void
  209. */
  210. function libre_2_woocommerce_wrapper_after() {
  211. ?>
  212. </main><!-- #main -->
  213. </div><!-- #primary -->
  214. <?php
  215. }
  216. }
  217. add_action( 'woocommerce_after_main_content', 'libre_2_woocommerce_wrapper_after' );
  218. if ( ! function_exists( 'libre_2_woocommerce_cart_link_fragment' ) ) {
  219. /**
  220. * Cart Fragments.
  221. *
  222. * Ensure cart contents update when products are added to the cart via AJAX.
  223. *
  224. * @param array $fragments Fragments to refresh via AJAX.
  225. * @return array Fragments to refresh via AJAX.
  226. */
  227. function libre_2_woocommerce_cart_link_fragment( $fragments ) {
  228. ob_start();
  229. libre_2_woocommerce_cart_link();
  230. $fragments['a.cart-contents'] = ob_get_clean();
  231. return $fragments;
  232. }
  233. }
  234. add_filter( 'woocommerce_add_to_cart_fragments', 'libre_2_woocommerce_cart_link_fragment' );
  235. if ( ! function_exists( 'libre_2_woocommerce_cart_link' ) ) {
  236. /**
  237. * Cart Link.
  238. *
  239. * Displayed a link to the cart including the number of items present and the cart total.
  240. *
  241. * @return void
  242. */
  243. function libre_2_woocommerce_cart_link() {
  244. ?>
  245. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'libre-2' ); ?>">
  246. <?php /* translators: number of items in the mini cart. */ ?>
  247. <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>
  248. </a>
  249. <?php
  250. }
  251. }
  252. if ( ! function_exists( 'libre_2_woocommerce_header_cart' ) ) {
  253. /**
  254. * Display Header Cart.
  255. *
  256. * @return void
  257. */
  258. function libre_2_woocommerce_header_cart() {
  259. if ( is_cart() ) {
  260. $class = 'current-menu-item';
  261. } else {
  262. $class = '';
  263. }
  264. ?>
  265. <ul id="site-header-cart" class="site-header-cart">
  266. <li class="<?php echo esc_attr( $class ); ?>">
  267. <?php libre_2_woocommerce_cart_link(); ?>
  268. </li>
  269. <li>
  270. <?php
  271. $instance = array(
  272. 'title' => '',
  273. );
  274. the_widget( 'WC_Widget_Cart', $instance );
  275. ?>
  276. </li>
  277. </ul>
  278. <?php
  279. }
  280. }
  281. /**
  282. * Workaround to prevent is_shop() from failing due to WordPress core issue
  283. *
  284. * @link https://core.trac.wordpress.org/ticket/21790
  285. * @param array $args infinite scroll args.
  286. * @return array infinite scroll args.
  287. */
  288. function libre_2_woocommerce_is_shop_page() {
  289. global $wp_query;
  290. $front_page_id = get_option( 'page_on_front' );
  291. $current_page_id = $wp_query->get( 'page_id' );
  292. $is_static_front_page = 'page' === get_option( 'show_on_front' );
  293. if ( $is_static_front_page && $front_page_id === $current_page_id ) {
  294. $is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
  295. } else {
  296. $is_shop_page = is_shop();
  297. }
  298. return $is_shop_page;
  299. }