woocommerce.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * WooCommerce Compatibility File
  4. *
  5. * @link https://woocommerce.com/
  6. *
  7. * @package _ip
  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 ip_woocommerce_setup() {
  18. add_theme_support( 'woocommerce', apply_filters( 'radcliffe_2_woocommerce_args', array(
  19. 'single_image_width' => 740,
  20. 'thumbnail_image_width' => 370,
  21. 'product_grid' => array(
  22. 'default_columns' => 3,
  23. 'default_rows' => 4,
  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', 'ip_woocommerce_setup' );
  34. /**
  35. * WooCommerce specific scripts & stylesheets.
  36. *
  37. * @return void
  38. */
  39. function ip_woocommerce_scripts() {
  40. wp_enqueue_style( 'ip-woocommerce-style', get_template_directory_uri() . '/woocommerce.css' );
  41. wp_style_add_data( 'ip-woocommerce-style', 'rtl', 'replace' );
  42. $fonts_path = WC()->plugin_url() . '/assets/fonts/';
  43. $fonts = array( 'star', 'WooCommerce' );
  44. $inline_font = '';
  45. foreach ( $fonts as $font ) {
  46. $inline_font .= '@font-face {
  47. font-family: "' . $font . '";
  48. src: url("' . $fonts_path . $font . '.eot");
  49. src: url("' . $fonts_path . $font . '.eot?#iefix") format("embedded-opentype"),
  50. url("' . $fonts_path . $font . '.woff") format("woff"),
  51. url("' . $fonts_path . $font . '.ttf") format("truetype"),
  52. url("' . $fonts_path . $font . '.svg#' . $font . '") format("svg");
  53. font-weight: normal;
  54. font-style: normal;
  55. }';
  56. }
  57. wp_add_inline_style( 'ip-woocommerce-style', $inline_font );
  58. }
  59. add_action( 'wp_enqueue_scripts', 'ip_woocommerce_scripts' );
  60. /**
  61. * Disable the default WooCommerce stylesheet.
  62. *
  63. * Removing the default WooCommerce stylesheet and enqueing your own will
  64. * protect you during WooCommerce core updates.
  65. *
  66. * @link https://docs.woocommerce.com/document/disable-the-default-stylesheet/
  67. */
  68. add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
  69. /**
  70. * Add 'woocommerce-active' class to the body tag.
  71. *
  72. * @param array $classes CSS classes applied to the body tag.
  73. * @return array $classes modified to include 'woocommerce-active' class.
  74. */
  75. function ip_woocommerce_active_body_class( $classes ) {
  76. $classes[] = 'woocommerce-active';
  77. return $classes;
  78. }
  79. add_filter( 'body_class', 'ip_woocommerce_active_body_class' );
  80. /**
  81. * Products per page.
  82. *
  83. * @return integer number of products.
  84. */
  85. function ip_woocommerce_products_per_page() {
  86. return 12;
  87. }
  88. // Legacy WooCommerce products per page filter.
  89. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  90. add_filter( 'loop_shop_per_page', 'ip_woocommerce_products_per_page' );
  91. }
  92. /**
  93. * Product gallery thumnbail columns.
  94. *
  95. * @return integer number of columns.
  96. */
  97. function ip_woocommerce_thumbnail_columns() {
  98. return 4;
  99. }
  100. add_filter( 'woocommerce_product_thumbnails_columns', 'ip_woocommerce_thumbnail_columns' );
  101. /**
  102. * Default loop columns on product archives.
  103. *
  104. * @return integer products per row.
  105. */
  106. function ip_woocommerce_loop_columns() {
  107. return 3;
  108. }
  109. // Legacy WooCommerce columns filter.
  110. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  111. add_filter( 'loop_shop_columns', 'ip_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 ip_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', 'ip_woocommerce_related_products_args' );
  128. if ( ! function_exists( 'ip_woocommerce_product_columns_wrapper' ) ) {
  129. /**
  130. * Product columns wrapper.
  131. *
  132. * @return void
  133. */
  134. function ip_woocommerce_product_columns_wrapper() {
  135. $columns = ip_loop_columns();
  136. echo '<div class="columns-' . absint( $columns ) . '">';
  137. }
  138. }
  139. add_action( 'woocommerce_before_shop_loop', 'ip_woocommerce_product_columns_wrapper', 40 );
  140. if ( ! function_exists( 'ip_loop_columns' ) ) {
  141. /**
  142. * Default loop columns on product archives
  143. *
  144. * @return integer products per row
  145. */
  146. function ip_loop_columns() {
  147. $columns = 3; // 3 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( 'ip_loop_columns', $columns );
  152. }
  153. }
  154. if ( ! function_exists( 'ip_woocommerce_product_columns_wrapper_close' ) ) {
  155. /**
  156. * Product columns wrapper close.
  157. *
  158. * @return void
  159. */
  160. function ip_woocommerce_product_columns_wrapper_close() {
  161. echo '</div>';
  162. }
  163. }
  164. add_action( 'woocommerce_after_shop_loop', 'ip_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. if ( ! function_exists( 'ip_woocommerce_wrapper_before' ) ) {
  171. /**
  172. * Before Content.
  173. *
  174. * Wraps all WooCommerce content in wrappers which match the theme markup.
  175. *
  176. * @return void
  177. */
  178. function ip_woocommerce_wrapper_before() {
  179. ?>
  180. <div id="primary" class="content-area">
  181. <main id="main" class="site-main" role="main">
  182. <?php
  183. }
  184. }
  185. add_action( 'woocommerce_before_main_content', 'ip_woocommerce_wrapper_before' );
  186. if ( ! function_exists( 'ip_woocommerce_wrapper_after' ) ) {
  187. /**
  188. * After Content.
  189. *
  190. * Closes the wrapping divs.
  191. *
  192. * @return void
  193. */
  194. function ip_woocommerce_wrapper_after() {
  195. ?>
  196. </main><!-- #main -->
  197. </div><!-- #primary -->
  198. <?php
  199. }
  200. }
  201. add_action( 'woocommerce_after_main_content', 'ip_woocommerce_wrapper_after' );
  202. /**
  203. * Sample implementation of the WooCommerce Mini Cart.
  204. *
  205. * You can add the WooCommerce Mini Cart to header.php like so ...
  206. *
  207. <?php
  208. if ( function_exists( 'ip_woocommerce_header_cart' ) ) {
  209. ip_woocommerce_header_cart();
  210. }
  211. ?>
  212. */
  213. if ( ! function_exists( 'ip_woocommerce_cart_link_fragment' ) ) {
  214. /**
  215. * Cart Fragments.
  216. *
  217. * Ensure cart contents update when products are added to the cart via AJAX.
  218. *
  219. * @param array $fragments Fragments to refresh via AJAX.
  220. * @return array Fragments to refresh via AJAX.
  221. */
  222. function ip_woocommerce_cart_link_fragment( $fragments ) {
  223. ob_start();
  224. ip_woocommerce_cart_link();
  225. $fragments['a.cart-contents'] = ob_get_clean();
  226. return $fragments;
  227. }
  228. }
  229. add_filter( 'woocommerce_add_to_cart_fragments', 'ip_woocommerce_cart_link_fragment' );
  230. if ( ! function_exists( 'ip_woocommerce_cart_link' ) ) {
  231. /**
  232. * Cart Link.
  233. *
  234. * Displayed a link to the cart including the number of items present and the cart total.
  235. *
  236. * @return void
  237. */
  238. function ip_woocommerce_cart_link() {
  239. ?>
  240. <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'independent-publisher-2' ); ?>">
  241. <?php /* translators: number of items in the mini cart. */ ?>
  242. <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(), 'independent-publisher-2' ), WC()->cart->get_cart_contents_count() ) );?></span>
  243. </a>
  244. <?php
  245. }
  246. }
  247. if ( ! function_exists( 'ip_woocommerce_header_cart' ) ) {
  248. /**
  249. * Display Header Cart.
  250. *
  251. * @return void
  252. */
  253. function ip_woocommerce_header_cart() {
  254. if ( is_cart() ) {
  255. $class = 'current-menu-item';
  256. } else {
  257. $class = '';
  258. }
  259. ?>
  260. <ul id="site-header-cart" class="site-header-cart">
  261. <li class="<?php echo esc_attr( $class ); ?>">
  262. <?php ip_woocommerce_cart_link(); ?>
  263. </li>
  264. <li>
  265. <?php
  266. $instance = array(
  267. 'title' => '',
  268. );
  269. the_widget( 'WC_Widget_Cart', $instance );
  270. ?>
  271. </li>
  272. </ul>
  273. <?php
  274. }
  275. }
  276. /**
  277. * Workaround to prevent is_shop() from failing due to WordPress core issue
  278. *
  279. * @link https://core.trac.wordpress.org/ticket/21790
  280. * @param array $args infinite scroll args.
  281. * @return array infinite scroll args.
  282. */
  283. function ip_woocommerce_is_shop_page() {
  284. global $wp_query;
  285. $front_page_id = get_option( 'page_on_front' );
  286. $current_page_id = $wp_query->get( 'page_id' );
  287. $shop_page_id = apply_filters( 'woocommerce_get_shop_page_id', get_option( 'woocommerce_shop_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. }
  296. /**
  297. * ip_woocommerce_image_dimensions Defines WC image sizes
  298. * @return void
  299. */
  300. function ip_woocommerce_image_dimensions() {
  301. global $pagenow;
  302. if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
  303. return;
  304. }
  305. $catalog = array(
  306. 'width' => '400', // px
  307. 'height' => '400', // px
  308. 'crop' => 1 // true
  309. );
  310. $single = array(
  311. 'width' => '740', // px
  312. 'height' => '740', // px
  313. 'crop' => 1 // true
  314. );
  315. $thumbnail = array(
  316. 'width' => '180', // px
  317. 'height' => '180', // px
  318. 'crop' => 0 // false
  319. );
  320. // Image sizes
  321. update_option( 'shop_catalog_image_size', $catalog ); // Product category thumbs
  322. update_option( 'shop_single_image_size', $single ); // Single product image
  323. update_option( 'shop_thumbnail_image_size', $thumbnail ); // Image gallery thumbs
  324. }
  325. add_action( 'after_switch_theme', 'ip_woocommerce_image_dimensions', 1 );