woocommerce.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * WooCommerce Compatibility File
  4. *
  5. * @link https://woocommerce.com/
  6. *
  7. * @package Dara
  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 dara_woocommerce_setup() {
  18. add_theme_support( 'woocommerce', apply_filters( 'dara_woocommerce_args', array(
  19. 'single_image_width' => 624,
  20. 'thumbnail_image_width' => 300,
  21. 'product_grid' => array(
  22. 'default_columns' => 4,
  23. 'default_rows' => 3,
  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', 'dara_woocommerce_setup' );
  34. /**
  35. * WooCommerce specific scripts & stylesheets.
  36. *
  37. * @return void
  38. */
  39. function dara_woocommerce_scripts() {
  40. wp_enqueue_style( 'dara-woocommerce-style', get_template_directory_uri() . '/woocommerce.css' );
  41. wp_style_add_data( 'dara-woocommerce-style', 'rtl', 'replace' );
  42. /* Load WooCommerce custom fonts. */
  43. $fonts_path = WC()->plugin_url() . '/assets/fonts/';
  44. $fonts = array( 'star', 'WooCommerce' );
  45. $inline_font = '';
  46. foreach ( $fonts as $font ) {
  47. $inline_font .= '@font-face {
  48. font-family: "' . $font . '";
  49. src: url("' . $fonts_path . $font . '.eot");
  50. src: url("' . $fonts_path . $font . '.eot?#iefix") format("embedded-opentype"),
  51. url("' . $fonts_path . $font . '.woff") format("woff"),
  52. url("' . $fonts_path . $font . '.ttf") format("truetype"),
  53. url("' . $fonts_path . $font . '.svg#' . $font . '") format("svg");
  54. font-weight: normal;
  55. font-style: normal;
  56. }';
  57. }
  58. wp_add_inline_style( 'dara-woocommerce-style', $inline_font );
  59. }
  60. add_action( 'wp_enqueue_scripts', 'dara_woocommerce_scripts' );
  61. /**
  62. * Disable the default WooCommerce stylesheet.
  63. *
  64. * Removing the default WooCommerce stylesheet and enqueing your own will
  65. * protect you during WooCommerce core updates.
  66. *
  67. * @link https://docs.woocommerce.com/document/disable-the-default-stylesheet/
  68. */
  69. add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
  70. /**
  71. * Add 'woocommerce-active' class to the body tag.
  72. *
  73. * @param array $classes CSS classes applied to the body tag.
  74. * @return array $classes modified to include 'woocommerce-active' class.
  75. */
  76. function dara_woocommerce_active_body_class( $classes ) {
  77. $classes[] = 'woocommerce-active';
  78. return $classes;
  79. }
  80. add_filter( 'body_class', 'dara_woocommerce_active_body_class' );
  81. /**
  82. * Products per page.
  83. *
  84. * @return integer number of products.
  85. */
  86. function dara_woocommerce_products_per_page() {
  87. return 12;
  88. }
  89. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  90. add_filter( 'loop_shop_per_page', 'dara_woocommerce_products_per_page' );
  91. }
  92. /**
  93. * Product gallery thumnbail columns.
  94. *
  95. * @return integer number of columns.
  96. */
  97. function dara_woocommerce_thumbnail_columns() {
  98. return absint( apply_filters( 'dara_woocommerce_product_thumbnail_columns', 4 ) );
  99. }
  100. add_filter( 'woocommerce_product_thumbnails_columns', 'dara_woocommerce_thumbnail_columns' );
  101. /**
  102. * Default loop columns on product archives.
  103. *
  104. * @return integer products per row.
  105. */
  106. function dara_woocommerce_loop_columns() {
  107. return absint( apply_filters( 'dara_woocommerce_loop_columns', 4 ) );
  108. }
  109. // Legacy WooCommerce columns filter.
  110. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  111. add_filter( 'loop_shop_columns', 'dara_woocommerce_loop_columns' );
  112. }
  113. /**
  114. * Related Products Args.
  115. *
  116. * @param array $args related products args.
  117. * @return array $args related products args.
  118. */
  119. function dara_woocommerce_related_products_args( $args ) {
  120. $defaults = array(
  121. 'posts_per_page' => 4,
  122. 'columns' => 4,
  123. );
  124. $args = wp_parse_args( $defaults, $args );
  125. return $args;
  126. }
  127. add_filter( 'woocommerce_output_related_products_args', 'dara_woocommerce_related_products_args' );
  128. if ( ! function_exists( 'dara_woocommerce_product_columns_wrapper' ) ) {
  129. /**
  130. * Product columns wrapper.
  131. *
  132. * @return void
  133. */
  134. function dara_woocommerce_product_columns_wrapper() {
  135. $columns = dara_loop_columns();
  136. echo '<div class="columns-' . absint( $columns ) . '">';
  137. }
  138. }
  139. add_action( 'woocommerce_before_shop_loop', 'dara_woocommerce_product_columns_wrapper', 40 );
  140. if ( ! function_exists( 'dara_loop_columns' ) ) {
  141. /**
  142. * Default loop columns on product archives
  143. *
  144. * @return integer products per row
  145. */
  146. function dara_loop_columns() {
  147. $columns = 4; // 4 products per row
  148. if ( function_exists( 'wc_get_default_products_per_row' ) ) {
  149. $columns = wc_get_default_products_per_row();
  150. }
  151. return apply_filters( 'dara_loop_columns', $columns );
  152. }
  153. }
  154. if ( ! function_exists( 'dara_woocommerce_product_columns_wrapper_close' ) ) {
  155. /**
  156. * Product columns wrapper close.
  157. *
  158. * @return void
  159. */
  160. function dara_woocommerce_product_columns_wrapper_close() {
  161. echo '</div>';
  162. }
  163. }
  164. add_action( 'woocommerce_after_shop_loop', 'dara_woocommerce_product_columns_wrapper_close', 40 );
  165. /**
  166. * Remove default WooCommerce wrapper.
  167. */
  168. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  169. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  170. /**
  171. * Remove WooCommerce sidebar.
  172. */
  173. remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
  174. if ( ! function_exists( 'dara_woocommerce_wrapper_before' ) ) {
  175. /**
  176. * Before Content.
  177. *
  178. * Wraps all WooCommerce content in wrappers which match the theme markup.
  179. *
  180. * @return void
  181. */
  182. function dara_woocommerce_wrapper_before() {
  183. $classes = array( 'content-wrapper' );
  184. if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  185. $classes[] = 'full-width';
  186. }
  187. $classes = implode( ' ', $classes );
  188. ?>
  189. <div class="<?php echo esc_attr( $classes ); ?>">
  190. <div id="primary" class="content-area">
  191. <main id="main" class="site-main" role="main">
  192. <?php
  193. }
  194. }
  195. add_action( 'woocommerce_before_main_content', 'dara_woocommerce_wrapper_before' );
  196. if ( ! function_exists( 'dara_woocommerce_wrapper_after' ) ) {
  197. /**
  198. * After Content.
  199. *
  200. * Closes the wrapping divs.
  201. *
  202. * @return void
  203. */
  204. function dara_woocommerce_wrapper_after() {
  205. ?>
  206. </main><!-- #main -->
  207. </div><!-- #primary -->
  208. <?php get_sidebar( 'shop' ); ?>
  209. </div><!-- .content-wrapper -->
  210. <?php
  211. }
  212. }
  213. add_action( 'woocommerce_after_main_content', 'dara_woocommerce_wrapper_after' );
  214. if ( ! function_exists( 'dara_woocommerce_cart_link_fragment' ) ) {
  215. /**
  216. * Cart Fragments.
  217. *
  218. * Ensure cart contents update when products are added to the cart via AJAX.
  219. *
  220. * @param array $fragments Fragments to refresh via AJAX.
  221. * @return array Fragments to refresh via AJAX.
  222. */
  223. function dara_woocommerce_cart_link_fragment( $fragments ) {
  224. ob_start();
  225. dara_woocommerce_cart_link();
  226. $fragments['a.cart-contents'] = ob_get_clean();
  227. return $fragments;
  228. }
  229. }
  230. add_filter( 'woocommerce_add_to_cart_fragments', 'dara_woocommerce_cart_link_fragment' );
  231. if ( ! function_exists( 'dara_woocommerce_cart_link' ) ) {
  232. /**
  233. * Cart Link.
  234. *
  235. * Displayed a link to the cart including the number of items present and the cart total.
  236. *
  237. * @return void
  238. */
  239. function dara_woocommerce_cart_link() {
  240. ?>
  241. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'dara' ); ?>">
  242. <?php /* translators: number of items in the mini cart. */ ?>
  243. <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(), 'dara' ), WC()->cart->get_cart_contents_count() ) );?></span>
  244. </a>
  245. <?php
  246. }
  247. }
  248. if ( ! function_exists( 'dara_woocommerce_header_cart' ) ) {
  249. /**
  250. * Display Header Cart.
  251. *
  252. * @return void
  253. */
  254. function dara_woocommerce_header_cart() {
  255. if ( is_cart() ) {
  256. $class = 'current-menu-item';
  257. } else {
  258. $class = '';
  259. }
  260. ?>
  261. <ul id="site-header-cart" class="site-header-cart">
  262. <li class="<?php echo esc_attr( $class ); ?>">
  263. <?php dara_woocommerce_cart_link(); ?>
  264. </li>
  265. <li>
  266. <?php
  267. $instance = array(
  268. 'title' => '',
  269. );
  270. the_widget( 'WC_Widget_Cart', $instance );
  271. ?>
  272. </li>
  273. </ul>
  274. <?php
  275. }
  276. }
  277. /**
  278. * Workaround to prevent is_shop() from failing due to WordPress core issue
  279. *
  280. * @link https://core.trac.wordpress.org/ticket/21790
  281. * @param array $args infinite scroll args.
  282. * @return array infinite scroll args.
  283. */
  284. function dara_woocommerce_is_shop_page() {
  285. global $wp_query;
  286. $front_page_id = get_option( 'page_on_front' );
  287. $current_page_id = $wp_query->get( 'page_id' );
  288. $is_static_front_page = 'page' === get_option( 'show_on_front' );
  289. if ( $is_static_front_page && $front_page_id === $current_page_id ) {
  290. $is_shop_page = ( $current_page_id === wc_get_page_id( 'shop' ) ) ? true : false;
  291. } else {
  292. $is_shop_page = is_shop();
  293. }
  294. return $is_shop_page;
  295. }