woocommerce.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. /**
  3. * WooCommerce Compatibility File
  4. *
  5. * @link https://woocommerce.com/
  6. *
  7. * @package Pique
  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 pique_woocommerce_setup() {
  18. add_theme_support( 'woocommerce', apply_filters( 'pique_woocommerce_args', array(
  19. 'single_image_width' => 942,
  20. 'thumbnail_image_width' => 350,
  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', 'pique_woocommerce_setup' );
  34. /**
  35. * WooCommerce specific scripts & stylesheets.
  36. *
  37. * @return void
  38. */
  39. function pique_woocommerce_scripts() {
  40. wp_enqueue_script( 'pique-woocommerce-scripts', get_template_directory_uri() . '/assets/js/woocommerce.js', array( 'jquery' ), '', true );
  41. wp_enqueue_style( 'pique-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( 'pique-woocommerce-style', $inline_font );
  54. }
  55. add_action( 'wp_enqueue_scripts', 'pique_woocommerce_scripts' );
  56. /**
  57. * WooCommerce specific scripts & stylesheets.
  58. *
  59. * @return void
  60. */
  61. function pique_woocommerce_image_dimensions() {
  62. global $pagenow;
  63. if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
  64. return;
  65. }
  66. $single = array(
  67. 'width' => '942', // px
  68. 'height' => '942', // px
  69. 'crop' => false // true
  70. );
  71. // Image Single product image size
  72. update_option( 'shop_single_image_size', $single );
  73. }
  74. add_action( 'after_switch_theme', 'pique_woocommerce_image_dimensions', 1 );
  75. /**
  76. * Disable the default WooCommerce stylesheet.
  77. *
  78. * Removing the default WooCommerce stylesheet and enqueing your own will
  79. * protect you during WooCommerce core updates.
  80. *
  81. * @link https://docs.woocommerce.com/document/disable-the-default-stylesheet/
  82. */
  83. add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
  84. /**
  85. * Add 'woocommerce-active' class to the body tag.
  86. *
  87. * @param array $classes CSS classes applied to the body tag.
  88. * @return array $classes modified to include 'woocommerce-active' class.
  89. */
  90. function pique_woocommerce_active_body_class( $classes ) {
  91. $classes[] = 'woocommerce-active';
  92. return $classes;
  93. }
  94. add_filter( 'body_class', 'pique_woocommerce_active_body_class' );
  95. /**
  96. * Products per page.
  97. *
  98. * @return integer number of products.
  99. */
  100. function pique_woocommerce_products_per_page() {
  101. return absint( apply_filters( 'pique_woocommerce_products_per_page', 12 ) );
  102. }
  103. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  104. add_filter( 'loop_shop_per_page', 'pique_woocommerce_products_per_page' );
  105. }
  106. // Legacy WooCommerce products per page filter.
  107. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  108. add_filter( 'loop_shop_per_page', 'pique_woocommerce_products_per_page' );
  109. }
  110. /**
  111. * Product gallery thumnbail columns.
  112. *
  113. * @return integer number of columns.
  114. */
  115. function pique_woocommerce_thumbnail_columns() {
  116. return absint( apply_filters( 'pique_woocommerce_product_thumbnail_columns', 4 ) );
  117. }
  118. add_filter( 'woocommerce_product_thumbnails_columns', 'pique_woocommerce_thumbnail_columns' );
  119. /**
  120. * Default loop columns on product archives.
  121. *
  122. * @return integer products per row.
  123. */
  124. function pique_woocommerce_loop_columns() {
  125. return absint( apply_filters( 'pique_woocommerce_loop_columns', 3 ) );
  126. }
  127. // Legacy WooCommerce columns filter.
  128. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  129. add_filter( 'loop_shop_columns', 'pique_woocommerce_loop_columns' );
  130. }
  131. /**
  132. * Related Products Args.
  133. *
  134. * @param array $args related products args.
  135. * @return array $args related products args.
  136. */
  137. function pique_woocommerce_related_products_args( $args ) {
  138. $args = apply_filters( 'radcliffe_2_woocommerce_related_products_args', array(
  139. 'posts_per_page' => 3,
  140. 'columns' => 3,
  141. ) );
  142. return $args;
  143. }
  144. add_filter( 'woocommerce_output_related_products_args', 'pique_woocommerce_related_products_args' );
  145. if ( ! function_exists( 'pique_woocommerce_product_columns_wrapper' ) ) {
  146. /**
  147. * Product columns wrapper.
  148. *
  149. * @return void
  150. */
  151. function pique_woocommerce_product_columns_wrapper() {
  152. $columns = pique_loop_columns();
  153. echo '<div class="columns-' . absint( $columns ) . '">';
  154. }
  155. }
  156. add_action( 'woocommerce_before_shop_loop', 'pique_woocommerce_product_columns_wrapper', 40 );
  157. if ( ! function_exists( 'pique_loop_columns' ) ) {
  158. /**
  159. * Default loop columns on product archives
  160. *
  161. * @return integer products per row
  162. */
  163. function pique_loop_columns() {
  164. $columns = 3; // 3 products per row
  165. if ( function_exists( 'wc_get_default_products_per_row' ) ) {
  166. $columns = wc_get_default_products_per_row();
  167. }
  168. return apply_filters( 'pique_loop_columns', $columns );
  169. }
  170. }
  171. if ( ! function_exists( 'pique_woocommerce_product_columns_wrapper_close' ) ) {
  172. /**
  173. * Product columns wrapper close.
  174. *
  175. * @return void
  176. */
  177. function pique_woocommerce_product_columns_wrapper_close() {
  178. echo '</div>';
  179. }
  180. }
  181. add_action( 'woocommerce_after_shop_loop', 'pique_woocommerce_product_columns_wrapper_close', 40 );
  182. /**
  183. * Remove default WooCommerce wrapper.
  184. */
  185. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  186. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  187. if ( ! function_exists( 'pique_woocommerce_wrapper_before' ) ) {
  188. /**
  189. * Before Content.
  190. *
  191. * Wraps all WooCommerce content in wrappers which match the theme markup.
  192. *
  193. * @return void
  194. */
  195. function pique_woocommerce_wrapper_before() {
  196. ?>
  197. <div id="primary" class="content-area">
  198. <main id="main" class="site-main" role="main">
  199. <?php
  200. }
  201. }
  202. add_action( 'woocommerce_before_main_content', 'pique_woocommerce_wrapper_before' );
  203. if ( ! function_exists( 'pique_woocommerce_wrapper_after' ) ) {
  204. /**
  205. * After Content.
  206. *
  207. * Closes the wrapping divs.
  208. *
  209. * @return void
  210. */
  211. function pique_woocommerce_wrapper_after() {
  212. ?>
  213. </main><!-- #main -->
  214. </div><!-- #primary -->
  215. <?php
  216. }
  217. }
  218. add_action( 'woocommerce_after_main_content', 'pique_woocommerce_wrapper_after' );
  219. if ( ! function_exists( 'pique_woocommerce_cart_link_fragment' ) ) {
  220. /**
  221. * Cart Fragments.
  222. *
  223. * Ensure cart contents update when products are added to the cart via AJAX.
  224. *
  225. * @param array $fragments Fragments to refresh via AJAX.
  226. * @return array Fragments to refresh via AJAX.
  227. */
  228. function pique_woocommerce_cart_link_fragment( $fragments ) {
  229. ob_start();
  230. pique_woocommerce_cart_link();
  231. $fragments['a.cart-contents'] = ob_get_clean();
  232. return $fragments;
  233. }
  234. }
  235. add_filter( 'woocommerce_add_to_cart_fragments', 'pique_woocommerce_cart_link_fragment' );
  236. if ( ! function_exists( 'pique_woocommerce_cart_link' ) ) {
  237. /**
  238. * Cart Link.
  239. *
  240. * Displayed a link to the cart including the number of items present and the cart total.
  241. *
  242. * @return void
  243. */
  244. function pique_woocommerce_cart_link() {
  245. ?>
  246. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'pique' ); ?>">
  247. <?php /* translators: number of items in the mini cart. */ ?>
  248. <span class="amount"><i class="fa fa-shopping-cart"></i> <?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span>
  249. <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'pique' ), WC()->cart->get_cart_contents_count() ) );?></span>
  250. </a>
  251. <?php
  252. }
  253. }
  254. if ( ! function_exists( 'pique_woocommerce_header_cart' ) ) {
  255. /**
  256. * Display Header Cart.
  257. *
  258. * @return void
  259. */
  260. function pique_woocommerce_header_cart() {
  261. if ( is_cart() ) {
  262. $class = 'current-menu-item';
  263. } else {
  264. $class = '';
  265. }
  266. ?>
  267. <ul id="site-header-cart" class="site-header-cart">
  268. <li class="<?php echo esc_attr( $class ); ?>">
  269. <?php pique_woocommerce_cart_link(); ?>
  270. </li>
  271. <li>
  272. <?php
  273. $instance = array(
  274. 'title' => '',
  275. );
  276. the_widget( 'WC_Widget_Cart', $instance );
  277. ?>
  278. </li>
  279. </ul>
  280. <?php
  281. }
  282. }
  283. /**
  284. * Place a cart icon with number of items and total cost in the menu bar.
  285. *
  286. * Source: http://wordpress.org/plugins/woocommerce-menu-bar-cart/
  287. */
  288. add_filter( 'wp_nav_menu_items', 'pique_woocommerce_menu_cart', 10, 2 );
  289. function pique_woocommerce_menu_cart( $menu, $args ) {
  290. //
  291. if ( $args->theme_location == 'primary' ) {
  292. // $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
  293. ob_start();
  294. ob_start();
  295. pique_woocommerce_cart_link();
  296. $new_cart_link = ob_get_clean();
  297. ob_start();
  298. the_widget( 'WC_Widget_Cart', array( 'title' => '' ) );
  299. $new_cart_widget = ob_get_clean();
  300. $menu_item = '<li class="menu-item site-header-cart right">' . $new_cart_link;
  301. $menu_item .= '<ul class="sub-menu"><li>' . $new_cart_widget . '</li></ul>';
  302. $menu_item .= '</li>';
  303. echo $menu_item;
  304. $menu .= ob_get_clean();
  305. }
  306. return $menu;
  307. }
  308. /**
  309. * Adds custom classes to the array of post classes.
  310. *
  311. * @param array $classes Classes for the post element.
  312. * @return array
  313. */
  314. function pique_woocommerce_post_classes( $classes ) {
  315. // Remove the panel class from products in archive views
  316. if ( is_archive() && is_woocommerce() ) :
  317. // $classes[] = 'pique-panel';
  318. $classes = preg_grep( '/^pique-panel$/', $classes, PREG_GREP_INVERT );
  319. endif;
  320. return $classes;
  321. }
  322. add_filter( 'post_class', 'pique_woocommerce_post_classes' );
  323. /**
  324. * Remove result count & sorting
  325. */
  326. remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
  327. remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
  328. /**
  329. * Add result count & sorting to woocommerce archive description in page header
  330. */
  331. add_action( 'woocommerce_archive_description', 'woocommerce_result_count', 20 );
  332. add_action( 'woocommerce_archive_description', 'woocommerce_catalog_ordering', 30 );
  333. /**
  334. * Workaround to prevent is_shop() from failing due to WordPress core issue
  335. *
  336. * @link https://core.trac.wordpress.org/ticket/21790
  337. * @param array $args infinite scroll args.
  338. * @return array infinite scroll args.
  339. */
  340. function pique_woocommerce_is_shop_page() {
  341. global $wp_query;
  342. $front_page_id = get_option( 'page_on_front' );
  343. $current_page_id = $wp_query->get( 'page_id' );
  344. $shop_page_id = apply_filters( 'woocommerce_get_shop_page_id', get_option( 'woocommerce_shop_page_id' ) );
  345. $is_static_front_page = 'page' === get_option( 'show_on_front' );
  346. if ( $is_static_front_page && $front_page_id === $current_page_id ) {
  347. $is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
  348. } else {
  349. $is_shop_page = is_shop();
  350. }
  351. return $is_shop_page;
  352. }
  353. /**
  354. * Override number of products per page in Jetpack infinite scroll.
  355. *
  356. * @param array $args infinite scroll args.
  357. * @return array infinite scroll args.
  358. */
  359. function pique_woocommerce_jetpack_products_per_page( $args ) {
  360. if ( is_array( $args ) && ( pique_woocommerce_is_shop_page() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
  361. $args['posts_per_page'] = pique_woocommerce_products_per_page();
  362. }
  363. return $args;
  364. }
  365. add_filter( 'infinite_scroll_settings', 'pique_woocommerce_jetpack_products_per_page' );