woocommerce-integration.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * WooCommerce-related functions and filters
  4. *
  5. * @package vamtam/consulting
  6. */
  7. /**
  8. * Alias for function_exists('is_woocommerce')
  9. * @return bool whether WooCommerce is active
  10. */
  11. function vamtam_has_woocommerce() {
  12. return function_exists( 'is_woocommerce' );
  13. }
  14. if ( vamtam_has_woocommerce() ) {
  15. // we have woocommerce
  16. // replace the default pagination with ours
  17. function woocommerce_pagination() {
  18. echo VamtamTemplates::pagination_list(); // xss ok
  19. }
  20. function vamtam_woocommerce_columns() {
  21. return 4;
  22. }
  23. // remove the WooCommerce breadcrumbs
  24. remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20,0 );
  25. // remove the WooCommerve sidebars
  26. remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
  27. /**
  28. * Redefine woocommerce_output_related_products()
  29. */
  30. function woocommerce_output_related_products() {
  31. if ( is_product() ) {
  32. if ( ! class_exists( 'Vamtam_Columns' ) || Vamtam_Columns::had_limit_wrapper() ) {
  33. echo '</div>';
  34. }
  35. woocommerce_related_products( array(
  36. 'columns' => 4,
  37. 'posts_per_page' => 12,
  38. ) );
  39. if ( ! class_exists( 'Vamtam_Columns' ) || Vamtam_Columns::had_limit_wrapper() ) {
  40. echo '<div class="limit-wrapper">';
  41. }
  42. }
  43. }
  44. // move related products after the content + sidebars
  45. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
  46. add_action( 'woocommerce_after_main_content', 'woocommerce_output_related_products', 999 );
  47. function vamtam_woocommerce_upsell_display() {
  48. if ( ! class_exists( 'Vamtam_Columns' ) || Vamtam_Columns::had_limit_wrapper() ) {
  49. echo '</div>';
  50. }
  51. woocommerce_upsell_display();
  52. if ( ! class_exists( 'Vamtam_Columns' ) || Vamtam_Columns::had_limit_wrapper() ) {
  53. echo '<div class="limit-wrapper">';
  54. }
  55. }
  56. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
  57. add_action( 'woocommerce_after_main_content', 'vamtam_woocommerce_upsell_display', 990 );
  58. add_filter( 'woocommerce_upsells_columns', 'vamtam_woocommerce_columns' );
  59. /**
  60. * Set the number of gallery thumbnails per row
  61. */
  62. add_filter( 'woocommerce_product_thumbnails_columns', 'vamtam_woocommerce_columns' );
  63. /**
  64. * star rating used in the single product template
  65. */
  66. function vamtam_woocommerce_rating() {
  67. global $product;
  68. if ( ! isset( $product ) || get_option( 'woocommerce_enable_review_rating' ) != 'yes' ) return;
  69. $count = $product->get_rating_count();
  70. if ( $count > 0 ) {
  71. $average = $product->get_average_rating();
  72. echo '<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
  73. echo '<div class="star-rating" title="' . esc_attr( sprintf( esc_html__( 'Rated %s out of 5', 'vamtam-consulting' ), $average ) ) . '"><span style="width:' . esc_attr( ( ( $average / 5 ) * 100 ) ) . '%"><strong itemprop="ratingValue" class="rating">' . esc_html( $average ) . '</strong> <span class="visuallyhidden">' . esc_html__( 'out of 5', 'vamtam-consulting' ) . '</span></span></div>';
  74. echo '</div>';
  75. }
  76. }
  77. add_action( 'woocommerce_single_product_summary', 'vamtam_woocommerce_rating', 15, 0 );
  78. remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
  79. /**
  80. * Move .onsale inside the images div
  81. */
  82. remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
  83. add_action( 'vamtam_woocommerce_before_single_product_images', 'woocommerce_show_product_sale_flash', 10 );
  84. /**
  85. * star rating for the shop loop, related product, etc.
  86. */
  87. function woocommerce_template_loop_rating() {
  88. vamtam_woocommerce_rating();
  89. }
  90. /**
  91. * wrap the product thumbnails in div.product-thumbnail
  92. */
  93. function woocommerce_template_loop_product_thumbnail() {
  94. echo '<div class="product-thumbnail">' . wp_kses_post( woocommerce_get_product_thumbnail() ) . '</div>';
  95. }
  96. /**
  97. * Show the product title in the product loop. By default this is an H3.
  98. */
  99. function woocommerce_template_loop_product_title() {
  100. echo '<h3 class="woocommerce-loop-product__title">' . get_the_title() . '</h3>';
  101. }
  102. /**
  103. * WooCommerce catalog/related products excerpt
  104. */
  105. function vamtam_woocommerce_catalog_excerpt() {
  106. global $post;
  107. if ( ! $post->post_excerpt ) return;
  108. $excerpt_length = apply_filters( 'vamtam_woocommerce_catalog_excerpt_length', 60 );
  109. $excerpt = explode( "\n", wordwrap( $post->post_excerpt, $excerpt_length ) );
  110. if (count( $excerpt ) > 1)
  111. $excerpt[0] .= '...';
  112. $excerpt = $excerpt[0];
  113. ?>
  114. <?php
  115. }
  116. add_action( 'woocommerce_after_shop_loop_item_title','vamtam_woocommerce_catalog_excerpt', 0 );
  117. function vamtam_woocommerce_cart_dropdown() {
  118. get_template_part( 'templates/cart-dropdown' );
  119. }
  120. add_action( 'vamtam_header_cart', 'vamtam_woocommerce_cart_dropdown' );
  121. function vamtam_woocommerce_body_class( $class ) {
  122. if ( is_cart() || is_checkout() || is_account_page() ) {
  123. $class[] = 'woocommerce';
  124. }
  125. return $class;
  126. }
  127. add_action( 'body_class', 'vamtam_woocommerce_body_class' );
  128. function vamtam_woocommerce_product_review_comment_form_args( $comment_form ) {
  129. $comment_form['comment_field'] = '';
  130. if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' ) {
  131. $comment_form['comment_field'] = '<p class="comment-form-rating"><label for="rating">' . esc_html__( 'Your Rating', 'vamtam-consulting' ) . '</label><select name="rating" id="rating">
  132. <option value="">' . esc_html__( 'Rate&hellip;', 'vamtam-consulting' ) . '</option>
  133. <option value="5">' . esc_html__( 'Perfect', 'vamtam-consulting' ) . '</option>
  134. <option value="4">' . esc_html__( 'Good', 'vamtam-consulting' ) . '</option>
  135. <option value="3">' . esc_html__( 'Average', 'vamtam-consulting' ) . '</option>
  136. <option value="2">' . esc_html__( 'Not that bad', 'vamtam-consulting' ) . '</option>
  137. <option value="1">' . esc_html__( 'Very Poor', 'vamtam-consulting' ) . '</option>
  138. </select></p>'; }
  139. $comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . esc_html__( 'Your Review', 'vamtam-consulting' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" required></textarea></p>';
  140. return $comment_form;
  141. }
  142. add_filter( 'woocommerce_product_review_comment_form_args', 'vamtam_woocommerce_product_review_comment_form_args' );
  143. add_filter( 'woocommerce_get_stock_html', 'vamtam_wc_get_stock_html', 10, 3 );
  144. function vamtam_wc_get_stock_html( $availability_html, $product ) {
  145. $availability = $product->get_availability();
  146. return empty( $availability['availability'] ) ? '' : '<p class="stock ' . esc_attr( $availability['class'] ) . '"><small>' . wp_kses_post( $availability['availability'] ) . '</small></p>';
  147. }
  148. // before_shop_loop_item_title
  149. remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
  150. if ( ! class_exists( 'WC_pac' ) || get_option( 'wc_pac_rating' ) !== 'no' ) {
  151. add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_rating', 45 );
  152. }
  153. add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 40 );
  154. remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
  155. if ( ! class_exists( 'WC_pac' ) || get_option( 'wc_pac_add_to_cart' ) !== 'no' ) {
  156. add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_add_to_cart', 50 );
  157. }
  158. add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 65 );
  159. // reorder wC PAC categories
  160. function vamtam_reorder_wc_pac() {
  161. if ( isset( $GLOBALS['wp_filter']['woocommerce_after_shop_loop_item']->callbacks[30] ) ) {
  162. foreach ( $GLOBALS['wp_filter']['woocommerce_after_shop_loop_item']->callbacks[30] as $key => $callback ) {
  163. if ( is_array( $callback['function'] ) && $callback['function'][1] === 'woocommerce_pac_show_product_categories' ) {
  164. remove_action( 'woocommerce_after_shop_loop_item', $callback['function'], 30 );
  165. add_action( 'woocommerce_before_shop_loop_item_title', $callback['function'], 60 );
  166. }
  167. }
  168. }
  169. }
  170. add_action( 'wp', 'vamtam_reorder_wc_pac', 100 );
  171. // after_shop_loop_item
  172. remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
  173. if ( ! class_exists( 'WC_pac' ) || get_option( 'wc_pac_rating' ) !== 'no' ) {
  174. add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_rating', 45 );
  175. }
  176. remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
  177. if ( ! class_exists( 'WC_pac' ) || get_option( 'wc_pac_price' ) !== 'no' ) {
  178. add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_price', 50 );
  179. }
  180. remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
  181. function vamtam_before_single_product_summary() {
  182. // check vamtam_after_single_product_summary() if modifying this line
  183. echo '<div class="limit-wrapper vamtam-box-outer-padding clearfix">';
  184. }
  185. add_action( 'woocommerce_before_single_product_summary', 'vamtam_before_single_product_summary', 1 );
  186. function vamtam_after_single_product_summary() {
  187. // check vamtam_before_single_product_summary() if modifying this line
  188. echo '</div>';
  189. }
  190. add_action( 'woocommerce_after_single_product_summary', 'vamtam_after_single_product_summary', 1 );
  191. remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );
  192. add_action( 'woocommerce_after_cart_table', 'woocommerce_cross_sell_display' );
  193. add_filter( 'woocommerce_cross_sells_columns', 'vamtam_woocommerce_columns' );
  194. add_filter( 'woocommerce_product_description_heading', '__return_false' );
  195. /**
  196. * Show a shop page description on product archives
  197. *
  198. * @access public
  199. * @subpackage Archives
  200. * @return void
  201. */
  202. function woocommerce_product_archive_description() {
  203. if ( is_post_type_archive( 'product' ) && get_query_var( 'paged' ) == 0 && ! is_search() ) {
  204. $shop_page = get_post( wc_get_page_id( 'shop' ) );
  205. if ( $shop_page ) {
  206. // this IS content, why not apply the filters anyway?
  207. echo apply_filters( 'the_content', wp_kses_post( $shop_page->post_content ) ); // xss ok
  208. }
  209. }
  210. }
  211. }