woocommerce.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /**
  4. * WooCommerce Compatibility File
  5. *
  6. * @link https://woocommerce.com/
  7. *
  8. * @package Varia
  9. */
  10. /**
  11. * WooCommerce setup function.
  12. *
  13. * @link https://docs.woocommerce.com/document/third-party-custom-theme-compatibility/
  14. * @link https://github.com/woocommerce/woocommerce/wiki/Enabling-product-gallery-features-(zoom,-swipe,-lightbox)-in-3.0.0
  15. *
  16. * @return void
  17. */
  18. function varia_woocommerce_setup() {
  19. add_theme_support( 'woocommerce', apply_filters( 'varia_woocommerce_args', array(
  20. 'single_image_width' => 750,
  21. 'thumbnail_image_width' => 350,
  22. 'product_grid' => array(
  23. 'default_columns' => 3,
  24. 'default_rows' => 6,
  25. 'min_columns' => 1,
  26. 'max_columns' => 6,
  27. 'min_rows' => 1
  28. )
  29. ) ) );
  30. add_theme_support( 'wc-product-gallery-zoom' );
  31. add_theme_support( 'wc-product-gallery-lightbox' );
  32. add_theme_support( 'wc-product-gallery-slider' );
  33. }
  34. add_action( 'after_setup_theme', 'varia_woocommerce_setup' );
  35. /**
  36. * Add a custom wrapper for woocomerce content
  37. */
  38. function varia_wrapper_start() {
  39. echo '<article id="woocommerce-wrapper" class="responsive-max-width">';
  40. }
  41. add_action('woocommerce_before_main_content', 'varia_wrapper_start', 10);
  42. function varia_wrapper_end() {
  43. echo '</article>';
  44. }
  45. add_action('woocommerce_after_main_content', 'varia_wrapper_end', 10);
  46. /**
  47. * Display category image on category archive
  48. */
  49. function varia_category_image() {
  50. if ( is_product_category() ){
  51. global $wp_query;
  52. $cat = $wp_query->get_queried_object();
  53. $thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
  54. $image = wp_get_attachment_url( $thumbnail_id );
  55. if ( $image ) {
  56. echo '<img src="' . $image . '" alt="' . $cat->name . '" />';
  57. }
  58. }
  59. }
  60. add_action( 'woocommerce_archive_description', 'varia_category_image', 2 );
  61. /**
  62. * Remove WooCommerce Sidebar
  63. */
  64. remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
  65. /**
  66. * Enqueue scripts and styles.
  67. */
  68. function varia_woocommerce_scripts() {
  69. // WooCommerce styles
  70. wp_enqueue_style( 'varia-woocommerce-style', get_stylesheet_directory_uri() . '/style-woocommerce.css', array(), wp_get_theme()->get( 'Version' ) );
  71. // WooCommerce RTL styles
  72. wp_style_add_data( 'varia-woocommerce-style', 'rtl', 'replace' );
  73. }
  74. add_action( 'wp_enqueue_scripts', 'varia_woocommerce_scripts' );
  75. /**
  76. * Setup cart link for main menu
  77. */
  78. if ( ! function_exists( 'varia_cart_link' ) ) {
  79. /**
  80. * Cart Link
  81. * Display a link to the cart including the number of items present and the cart total
  82. *
  83. * @return void
  84. * @since 1.0.0
  85. */
  86. function varia_cart_link() {
  87. $link_output = sprintf(
  88. '<a class="woocommerce-cart-link" href="%1$s" title="%2$s">
  89. %3$s
  90. <span class="woocommerce-cart-subtotal">%4$s</span>
  91. <small class="woocommerce-cart-count">%5$s</small>
  92. </a>',
  93. esc_url( wc_get_cart_url() ),
  94. esc_attr__( 'View your shopping cart', 'varia' ),
  95. varia_get_icon_svg( 'shopping_cart', 16 ),
  96. wp_kses_post( WC()->cart->get_cart_subtotal() ),
  97. wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'varia' ), WC()->cart->get_cart_contents_count() ) )
  98. );
  99. return $link_output;
  100. }
  101. }
  102. /**
  103. * Setup cart fragments
  104. */
  105. if ( ! function_exists( 'varia_cart_subtotal_fragment' ) ) {
  106. /**
  107. * Cart Subtotal Fragments
  108. * Ensure cart subtotal amount update when products are added to the cart via AJAX
  109. *
  110. * @param array $fragments Fragments to refresh via AJAX.
  111. * @return array Fragments to refresh via AJAX
  112. */
  113. function varia_cart_subtotal_fragment( $fragments ) {
  114. ob_start();
  115. echo '<span class="woocommerce-cart-subtotal">' . wp_kses_post( WC()->cart->get_cart_subtotal() ) . '</span>';
  116. $fragments['.woocommerce-cart-subtotal'] = ob_get_clean();
  117. return $fragments;
  118. }
  119. }
  120. if ( ! function_exists( 'varia_cart_count_fragment' ) ) {
  121. /**
  122. * Cart Count Fragments
  123. * Ensure cart item count update when products are added to the cart via AJAX
  124. *
  125. * @param array $fragments Fragments to refresh via AJAX.
  126. * @return array Fragments to refresh via AJAX
  127. */
  128. function varia_cart_count_fragment( $fragments ) {
  129. ob_start();
  130. echo '<small class="woocommerce-cart-count">' . wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'varia' ), WC()->cart->get_cart_contents_count() ) ) . '</small>';
  131. $fragments['.woocommerce-cart-count'] = ob_get_clean();
  132. return $fragments;
  133. }
  134. }
  135. /**
  136. * Setup cart widget for mini-cart dropdown
  137. */
  138. if ( ! function_exists( 'varia_cart_widget' ) ) {
  139. /**
  140. * Cart Items List
  141. * Ensure cart contents update when products are added to the cart via AJAX
  142. *
  143. * @param array $fragments Fragments to refresh via AJAX.
  144. * @return array Fragments to refresh via AJAX
  145. */
  146. function varia_cart_widget() {
  147. ob_start();
  148. the_widget( 'WC_Widget_Cart', 'title=' );
  149. $widget_output = ob_get_contents();
  150. ob_end_clean();
  151. return $widget_output;
  152. }
  153. }
  154. /**
  155. * Add cart fragment filters
  156. *
  157. * @see varia_cart_subtotal_fragment() and varia_cart_count_fragment()
  158. */
  159. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.3', '>=' ) ) {
  160. add_filter( 'woocommerce_add_to_cart_fragments', 'varia_cart_subtotal_fragment', 10, 1 );
  161. add_filter( 'woocommerce_add_to_cart_fragments', 'varia_cart_count_fragment', 10, 1 );
  162. } else {
  163. add_filter( 'add_to_cart_fragments', 'varia_cart_subtotal_fragment' );
  164. add_filter( 'add_to_cart_fragments', 'varia_cart_count_fragment' );
  165. }
  166. /**
  167. * Add WooCommerce mini-cart link to primary menu
  168. */
  169. function varia_add_cart_menu( $nav, $args ) {
  170. if ( $args->theme_location == 'menu-1' ) {
  171. update_option( 'varia_cart_icon', true );
  172. if ( empty( $nav ) ) {
  173. update_option( 'varia_cart_icon', false );
  174. }
  175. $has_cart_icon = get_option( 'varia_cart_icon' );
  176. if ( $has_cart_icon && ! empty( $nav ) ) {
  177. return sprintf(
  178. '%1$s
  179. </ul></div>
  180. <input type="checkbox" role="button" aria-haspopup="true" id="woocommerce-toggle" class="hide-visually">
  181. <label for="woocommerce-toggle" id="toggle-cart" class="button">%2$s %3$s
  182. <span class="dropdown-icon open">+</span>
  183. <span class="dropdown-icon close">×</span>
  184. <span class="hide-visually expanded-text">%4$s</span>
  185. <span class="hide-visually collapsed-text">%5$s</span>
  186. </label>
  187. <div class="woocommerce-menu-container">
  188. <ul id="woocommerce-menu" class="main-menu" aria-label="submenu">
  189. <li class="menu-item woocommerce-menu-item %10$s" title="%6$s">
  190. %8$s
  191. <ul class="sub-menu">
  192. <li class="woocommerce-cart-widget" title="%7$s">
  193. %9$s
  194. </li>
  195. </ul>
  196. </li>',
  197. $nav,
  198. varia_get_icon_svg( 'shopping_cart', 16 ),
  199. esc_html__( 'Cart', 'varia' ),
  200. esc_html__( 'expanded', 'varia' ),
  201. esc_html__( 'collapsed', 'varia' ),
  202. esc_attr__( 'View your shopping cart', 'varia' ),
  203. esc_attr__( 'View your shopping list', 'varia' ),
  204. varia_cart_link(),
  205. varia_cart_widget(),
  206. is_cart() ? 'current-menu-item' : ''
  207. );
  208. }
  209. }
  210. // Our primary menu isn't set, return the regular nav
  211. return $nav;
  212. }
  213. add_filter( 'wp_nav_menu_items', 'varia_add_cart_menu', 10, 2 );