woocommerce.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /**
  4. * WooCommerce Compatibility File
  5. *
  6. * @link https://woocommerce.com/
  7. *
  8. * @package Seedlet
  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 seedlet_woocommerce_setup() {
  19. add_theme_support( 'woocommerce', apply_filters( 'seedlet_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', 'seedlet_woocommerce_setup' );
  35. /**
  36. * Add a custom wrapper for woocomerce content
  37. */
  38. function seedlet_content_wrapper_start() {
  39. echo '<article id="woocommerce-wrapper" class="wide-max-width">';
  40. }
  41. add_action('woocommerce_before_main_content', 'seedlet_content_wrapper_start', 10);
  42. function seedlet_content_wrapper_end() {
  43. echo '</article>';
  44. }
  45. add_action('woocommerce_after_main_content', 'seedlet_content_wrapper_end', 10);
  46. /**
  47. * Add a custom wrapper for woocomerce cart
  48. */
  49. function seedlet_cart_wrapper_start() {
  50. echo '<div id="woocommerce-cart-wrapper" class="wide-max-width">';
  51. }
  52. add_action('woocommerce_before_cart', 'seedlet_cart_wrapper_start', 10);
  53. add_action('woocommerce_before_checkout_form', 'seedlet_cart_wrapper_start', 10);
  54. function seedlet_cart_wrapper_end() {
  55. echo '</div>';
  56. }
  57. add_action('woocommerce_after_cart', 'seedlet_cart_wrapper_end', 10);
  58. add_action('woocommerce_after_checkout_form', 'seedlet_cart_wrapper_end', 10);
  59. /**
  60. * Add a custom wrapper for woocomerce my-account
  61. */
  62. function seedlet_my_account_wrapper_start() {
  63. echo '<div id="woocommerce-my-account-wrapper" class="wide-max-width">';
  64. }
  65. add_action('woocommerce_before_account_navigation', 'seedlet_my_account_wrapper_start', 10);
  66. add_action('woocommerce_before_customer_login_form', 'seedlet_my_account_wrapper_start', 10);
  67. function seedlet_my_account_wrapper_end() {
  68. echo '</div>';
  69. }
  70. add_action('woocommerce_account_dashboard', 'seedlet_my_account_wrapper_end', 10);
  71. add_action('woocommerce_after_customer_login_form', 'seedlet_my_account_wrapper_end', 10);
  72. /**
  73. * Display category image on category archive
  74. */
  75. function seedlet_category_image() {
  76. if ( is_product_category() ){
  77. global $wp_query;
  78. $cat = $wp_query->get_queried_object();
  79. $thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
  80. $image = wp_get_attachment_url( $thumbnail_id );
  81. if ( $image ) {
  82. echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $cat->name ) . '" />';
  83. }
  84. }
  85. }
  86. add_action( 'woocommerce_archive_description', 'seedlet_category_image', 2 );
  87. /**
  88. * Remove WooCommerce Sidebar
  89. */
  90. remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
  91. /**
  92. * Enqueue scripts and styles.
  93. */
  94. function seedlet_woocommerce_scripts() {
  95. // WooCommerce styles
  96. wp_enqueue_style( 'seedlet-woocommerce-style', get_template_directory_uri() . '/assets/css/style-woocommerce.css', array(), wp_get_theme()->get( 'Version' ) );
  97. // WooCommerce RTL styles
  98. wp_style_add_data( 'seedlet-woocommerce-style', 'rtl', 'replace' );
  99. }
  100. add_action( 'wp_enqueue_scripts', 'seedlet_woocommerce_scripts' );
  101. /**
  102. * Setup cart link for main menu
  103. */
  104. if ( ! function_exists( 'seedlet_cart_link' ) ) {
  105. /**
  106. * Cart Link
  107. * Display a link to the cart including the number of items present and the cart total
  108. *
  109. * @return void
  110. * @since 1.0.0
  111. */
  112. function seedlet_cart_link() {
  113. $link_output = sprintf(
  114. '<a class="woocommerce-cart-link" href="%1$s" title="%2$s">
  115. %3$s
  116. <span class="woocommerce-cart-subtotal">%4$s</span>
  117. <small class="woocommerce-cart-count">%5$s</small>
  118. </a>',
  119. esc_url( wc_get_cart_url() ),
  120. esc_attr__( 'View your shopping cart', 'seedlet' ),
  121. seedlet_get_icon_svg( 'shopping_cart', 16 ),
  122. wp_kses_post( WC()->cart->get_cart_subtotal() ),
  123. wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'seedlet' ), WC()->cart->get_cart_contents_count() ) )
  124. );
  125. return $link_output;
  126. }
  127. }
  128. /**
  129. * Setup cart fragments
  130. */
  131. if ( ! function_exists( 'seedlet_cart_subtotal_fragment' ) ) {
  132. /**
  133. * Cart Subtotal Fragments
  134. * Ensure cart subtotal amount update when products are added to the cart via AJAX
  135. *
  136. * @param array $fragments Fragments to refresh via AJAX.
  137. * @return array Fragments to refresh via AJAX
  138. */
  139. function seedlet_cart_subtotal_fragment( $fragments ) {
  140. ob_start();
  141. echo '<span class="woocommerce-cart-subtotal">' . wp_kses_post( WC()->cart->get_cart_subtotal() ) . '</span>';
  142. $fragments['.woocommerce-cart-subtotal'] = ob_get_clean();
  143. return $fragments;
  144. }
  145. }
  146. if ( ! function_exists( 'seedlet_cart_count_fragment' ) ) {
  147. /**
  148. * Cart Count Fragments
  149. * Ensure cart item count update when products are added to the cart via AJAX
  150. *
  151. * @param array $fragments Fragments to refresh via AJAX.
  152. * @return array Fragments to refresh via AJAX
  153. */
  154. function seedlet_cart_count_fragment( $fragments ) {
  155. ob_start();
  156. echo '<small class="woocommerce-cart-count">' . wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'seedlet' ), WC()->cart->get_cart_contents_count() ) ) . '</small>';
  157. $fragments['.woocommerce-cart-count'] = ob_get_clean();
  158. return $fragments;
  159. }
  160. }
  161. /**
  162. * Setup cart widget for mini-cart dropdown
  163. */
  164. if ( ! function_exists( 'seedlet_cart_widget' ) ) {
  165. /**
  166. * Cart Items List
  167. * Ensure cart contents update when products are added to the cart via AJAX
  168. *
  169. * @param array $fragments Fragments to refresh via AJAX.
  170. * @return array Fragments to refresh via AJAX
  171. */
  172. function seedlet_cart_widget() {
  173. ob_start();
  174. the_widget( 'WC_Widget_Cart', 'title=' );
  175. $widget_output = ob_get_contents();
  176. ob_end_clean();
  177. return $widget_output;
  178. }
  179. }
  180. /**
  181. * Add cart fragment filters
  182. *
  183. * @see seedlet_cart_subtotal_fragment() and seedlet_cart_count_fragment()
  184. */
  185. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.3', '>=' ) ) {
  186. add_filter( 'woocommerce_add_to_cart_fragments', 'seedlet_cart_subtotal_fragment', 10, 1 );
  187. add_filter( 'woocommerce_add_to_cart_fragments', 'seedlet_cart_count_fragment', 10, 1 );
  188. } else {
  189. add_filter( 'add_to_cart_fragments', 'seedlet_cart_subtotal_fragment' );
  190. add_filter( 'add_to_cart_fragments', 'seedlet_cart_count_fragment' );
  191. }