woocommerce.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * WooCommerce Compatibility File
  4. *
  5. * @link https://woocommerce.com/
  6. *
  7. * @package karuna
  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 karuna_woocommerce_setup() {
  18. add_theme_support( 'woocommerce', apply_filters( 'karuna_woocommerce_args', array(
  19. 'single_image_width' => 685,
  20. 'thumbnail_image_width' => 350,
  21. 'product_grid' => array(
  22. 'default_columns' => 2,
  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', 'karuna_woocommerce_setup' );
  34. /**
  35. * Define image sizes
  36. *
  37. * @link https://docs.woocommerce.com/document/set-woocommerce-image-dimensions-upon-theme-activation/
  38. *
  39. * @return void
  40. */
  41. function karuna_woocommerce_image_dimensions() {
  42. global $pagenow;
  43. if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
  44. return;
  45. }
  46. $catalog = array(
  47. 'width' => '692',
  48. 'height' => '692',
  49. 'crop' => 1,
  50. );
  51. $single = array(
  52. 'width' => '692',
  53. 'height' => '692',
  54. 'crop' => 1,
  55. );
  56. $thumbnail = array(
  57. 'width' => '154',
  58. 'height' => '154',
  59. 'crop' => 0,
  60. );
  61. update_option( 'shop_catalog_image_size', $catalog );
  62. update_option( 'shop_single_image_size', $single );
  63. update_option( 'shop_thumbnail_image_size', $thumbnail );
  64. }
  65. add_action( 'after_switch_theme', 'karuna_woocommerce_image_dimensions', 1 );
  66. /**
  67. * WooCommerce specific scripts & stylesheets.
  68. *
  69. * @return void
  70. */
  71. function karuna_woocommerce_scripts() {
  72. wp_enqueue_style( 'karuna-woocommerce-style', get_template_directory_uri() . '/woocommerce.css' );
  73. wp_style_add_data( 'karuna-woocommerce-style', 'rtl', 'replace' );
  74. }
  75. add_action( 'wp_enqueue_scripts', 'karuna_woocommerce_scripts' );
  76. /**
  77. * Disable the default WooCommerce stylesheet.
  78. *
  79. * Removing the default WooCommerce stylesheet and enqueing your own will
  80. * protect you during WooCommerce core updates.
  81. *
  82. * @link https://docs.woocommerce.com/document/disable-the-default-stylesheet/
  83. */
  84. add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
  85. /**
  86. * Add 'woocommerce-active' class to the body tag.
  87. *
  88. * @param array $classes CSS classes applied to the body tag.
  89. * @return array $classes modified to include 'woocommerce-active' class.
  90. */
  91. function karuna_woocommerce_active_body_class( $classes ) {
  92. $classes[] = 'woocommerce-active';
  93. return $classes;
  94. }
  95. add_filter( 'body_class', 'karuna_woocommerce_active_body_class' );
  96. /**
  97. * Products per page.
  98. *
  99. * @return integer number of products.
  100. */
  101. function karuna_woocommerce_products_per_page() {
  102. return absint( apply_filters( 'karuna_woocommerce_products_per_page', 12 ) );
  103. }
  104. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  105. add_filter( 'loop_shop_per_page', 'karuna_woocommerce_products_per_page' );
  106. }
  107. // Legacy WooCommerce products per page filter.
  108. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  109. add_filter( 'loop_shop_per_page', 'karuna_woocommerce_products_per_page' );
  110. }
  111. /**
  112. * Product gallery thumnbail columns.
  113. *
  114. * @return integer number of columns.
  115. */
  116. function karuna_woocommerce_thumbnail_columns() {
  117. return absint( apply_filters( 'karuna_woocommerce_product_thumbnail_columns', 4 ) );
  118. }
  119. add_filter( 'woocommerce_product_thumbnails_columns', 'karuna_woocommerce_thumbnail_columns' );
  120. /**
  121. * Default loop columns on product archives.
  122. *
  123. * @return integer products per row.
  124. */
  125. function karuna_woocommerce_loop_columns() {
  126. return absint( apply_filters( 'karuna_woocommerce_loop_columns', 2 ) );
  127. }
  128. // Legacy WooCommerce columns filter.
  129. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  130. add_filter( 'loop_shop_columns', 'karuna_woocommerce_loop_columns' );
  131. }
  132. /**
  133. * Related Products Args.
  134. *
  135. * @param array $args related products args.
  136. * @return array $args related products args.
  137. */
  138. function karuna_woocommerce_related_products_args( $args ) {
  139. $args = apply_filters( 'karuna_woocommerce_related_products_args', array(
  140. 'posts_per_page' => 2,
  141. 'columns' => 2,
  142. ) );
  143. return $args;
  144. }
  145. add_filter( 'woocommerce_output_related_products_args', 'karuna_woocommerce_related_products_args' );
  146. if ( ! function_exists( 'karuna_woocommerce_product_columns_wrapper' ) ) {
  147. /**
  148. * Product columns wrapper.
  149. *
  150. * @return void
  151. */
  152. function karuna_woocommerce_product_columns_wrapper() {
  153. $columns = karuna_loop_columns();
  154. echo '<div class="columns-' . absint( $columns ) . '">';
  155. }
  156. }
  157. add_action( 'woocommerce_before_shop_loop', 'karuna_woocommerce_product_columns_wrapper', 40 );
  158. if ( ! function_exists( 'karuna_loop_columns' ) ) {
  159. /**
  160. * Default loop columns on product archives
  161. *
  162. * @return integer products per row
  163. */
  164. function karuna_loop_columns() {
  165. $columns = 2; // 2 products per row
  166. if ( function_exists( 'wc_get_default_products_per_row' ) ) {
  167. $columns = wc_get_default_products_per_row();
  168. }
  169. return apply_filters( 'karuna_loop_columns', $columns );
  170. }
  171. }
  172. if ( ! function_exists( 'karuna_woocommerce_product_columns_wrapper_close' ) ) {
  173. /**
  174. * Product columns wrapper close.
  175. *
  176. * @return void
  177. */
  178. function karuna_woocommerce_product_columns_wrapper_close() {
  179. echo '</div>';
  180. }
  181. }
  182. add_action( 'woocommerce_after_shop_loop', 'karuna_woocommerce_product_columns_wrapper_close', 40 );
  183. /**
  184. * Remove default WooCommerce wrapper.
  185. */
  186. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  187. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  188. if ( ! function_exists( 'karuna_woocommerce_wrapper_before' ) ) {
  189. /**
  190. * Before Content.
  191. *
  192. * Wraps all WooCommerce content in wrappers which match the theme markup.
  193. *
  194. * @return void
  195. */
  196. function karuna_woocommerce_wrapper_before() {
  197. ?>
  198. <div id="primary" class="content-area">
  199. <main id="main" class="site-main" role="main">
  200. <?php
  201. }
  202. }
  203. add_action( 'woocommerce_before_main_content', 'karuna_woocommerce_wrapper_before' );
  204. if ( ! function_exists( 'karuna_woocommerce_wrapper_after' ) ) {
  205. /**
  206. * After Content.
  207. *
  208. * Closes the wrapping divs.
  209. *
  210. * @return void
  211. */
  212. function karuna_woocommerce_wrapper_after() {
  213. ?>
  214. </main><!-- #main -->
  215. </div><!-- #primary -->
  216. <?php
  217. }
  218. }
  219. add_action( 'woocommerce_after_main_content', 'karuna_woocommerce_wrapper_after' );
  220. function karuna_woocommerce_image_wrapper_open() {
  221. echo '<div class="woocommerce-image-wrapper">';
  222. }
  223. function karuna_woocommerce_image_wrapper_closed() {
  224. echo '</div>';
  225. }
  226. remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
  227. remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
  228. add_action( 'woocommerce_before_single_product_summary', 'woocommerce_template_single_title', 1 );
  229. add_action( 'woocommerce_before_single_product_summary', 'woocommerce_template_single_rating', 2 );
  230. add_action( 'woocommerce_before_single_product_summary', 'karuna_woocommerce_image_wrapper_open', 5 );
  231. add_action( 'woocommerce_before_single_product_summary', 'karuna_woocommerce_image_wrapper_closed', 25 );
  232. if ( ! function_exists( 'karuna_woocommerce_cart_link_fragment' ) ) {
  233. /**
  234. * Cart Fragments.
  235. *
  236. * Ensure cart contents update when products are added to the cart via AJAX.
  237. *
  238. * @param array $fragments Fragments to refresh via AJAX.
  239. * @return array Fragments to refresh via AJAX.
  240. */
  241. function karuna_woocommerce_cart_link_fragment( $fragments ) {
  242. ob_start();
  243. karuna_woocommerce_cart_link();
  244. $fragments['a.cart-contents'] = ob_get_clean();
  245. return $fragments;
  246. }
  247. }
  248. add_filter( 'woocommerce_add_to_cart_fragments', 'karuna_woocommerce_cart_link_fragment' );
  249. if ( ! function_exists( 'karuna_woocommerce_cart_link' ) ) {
  250. /**
  251. * Cart Link.
  252. *
  253. * Displayed a link to the cart including the number of items present and the cart total.
  254. *
  255. * @return void
  256. */
  257. function karuna_woocommerce_cart_link() {
  258. ?>
  259. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'karuna' ); ?>">
  260. <?php /* translators: number of items in the mini cart. */ ?>
  261. <span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span> <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'karuna' ), WC()->cart->get_cart_contents_count() ) );?></span>
  262. </a>
  263. <?php
  264. }
  265. }
  266. if ( ! function_exists( 'karuna_woocommerce_header_cart' ) ) {
  267. /**
  268. * Display Header Cart.
  269. *
  270. * @return void
  271. */
  272. function karuna_woocommerce_header_cart() {
  273. if ( is_cart() ) {
  274. $class = 'current-menu-item';
  275. } else {
  276. $class = '';
  277. }
  278. ?>
  279. <ul id="site-header-cart" class="site-header-cart">
  280. <li class="<?php echo esc_attr( $class ); ?>">
  281. <?php karuna_woocommerce_cart_link(); ?>
  282. </li>
  283. <li>
  284. <?php
  285. $instance = array(
  286. 'title' => '',
  287. );
  288. the_widget( 'WC_Widget_Cart', $instance );
  289. ?>
  290. </li>
  291. </ul>
  292. <?php
  293. }
  294. }
  295. /**
  296. * Workaround to prevent is_shop() from failing due to WordPress core issue
  297. *
  298. * @link https://core.trac.wordpress.org/ticket/21790
  299. * @param array $args infinite scroll args.
  300. * @return array infinite scroll args.
  301. */
  302. function karuna_woocommerce_is_shop_page() {
  303. global $wp_query;
  304. $front_page_id = get_option( 'page_on_front' );
  305. $current_page_id = $wp_query->get( 'page_id' );
  306. $is_static_front_page = 'page' === get_option( 'show_on_front' );
  307. if ( $is_static_front_page && $front_page_id === $current_page_id ) {
  308. $is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
  309. } else {
  310. $is_shop_page = is_shop();
  311. }
  312. return $is_shop_page;
  313. }
  314. /**
  315. * Override number of products per page in Jetpack infinite scroll.
  316. *
  317. * @param array $args infinite scroll args.
  318. * @return array infinite scroll args.
  319. */
  320. function karuna_woocommerce_jetpack_products_per_page( $args ) {
  321. if ( is_array( $args ) && ( karuna_woocommerce_is_shop_page() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
  322. $args['posts_per_page'] = karuna_woocommerce_products_per_page();
  323. }
  324. return $args;
  325. }
  326. add_filter( 'infinite_scroll_settings', 'karuna_woocommerce_jetpack_products_per_page' );