woocommerce.php 6.6 KB

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