woocommerce.php 7.0 KB

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