woocommerce.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * This file contains compatibility functions for WooCommerce to improve Jetpack feature support.
  4. */
  5. add_action( 'woocommerce_init', 'jetpack_woocommerce_integration' );
  6. function jetpack_woocommerce_integration() {
  7. /**
  8. * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry.
  9. */
  10. if ( ! class_exists( 'WooCommerce' ) ) {
  11. return;
  12. }
  13. add_action( 'woocommerce_share', 'jetpack_woocommerce_social_share_icons', 10 );
  14. /**
  15. * Wrap in function exists check since this requires WooCommerce 3.3+.
  16. */
  17. if ( function_exists( 'wc_get_default_products_per_row' ) ) {
  18. add_filter( 'infinite_scroll_render_callbacks', 'jetpack_woocommerce_infinite_scroll_render_callback', 10 );
  19. add_action( 'wp_enqueue_scripts', 'jetpack_woocommerce_infinite_scroll_style', 10 );
  20. }
  21. }
  22. /*
  23. * Make sure the social sharing icons show up under the product's short description
  24. */
  25. function jetpack_woocommerce_social_share_icons() {
  26. if ( function_exists( 'sharing_display' ) ) {
  27. remove_filter( 'the_content', 'sharing_display', 19 );
  28. remove_filter( 'the_excerpt', 'sharing_display', 19 );
  29. echo sharing_display();
  30. }
  31. }
  32. /**
  33. * Remove sharing display from account, cart, and checkout pages in WooCommerce.
  34. */
  35. function jetpack_woocommerce_remove_share() {
  36. /**
  37. * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry.
  38. */
  39. if ( ! class_exists( 'WooCommerce' ) ) {
  40. return;
  41. }
  42. if ( is_cart() || is_checkout() || is_account_page() ) {
  43. remove_filter( 'the_content', 'sharing_display', 19 );
  44. if ( class_exists( 'Jetpack_Likes' ) ) {
  45. remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
  46. }
  47. }
  48. }
  49. add_action( 'loop_start', 'jetpack_woocommerce_remove_share' );
  50. /**
  51. * Add a callback for WooCommerce product rendering in infinite scroll.
  52. *
  53. * @param array $callbacks
  54. * @return array
  55. */
  56. function jetpack_woocommerce_infinite_scroll_render_callback( $callbacks ) {
  57. $callbacks[] = 'jetpack_woocommerce_infinite_scroll_render';
  58. return $callbacks;
  59. }
  60. /**
  61. * Add a default renderer for WooCommerce products within infinite scroll.
  62. */
  63. function jetpack_woocommerce_infinite_scroll_render() {
  64. if ( ! is_shop() && ! is_product_taxonomy() && ! is_product_category() && ! is_product_tag() ) {
  65. return;
  66. }
  67. woocommerce_product_loop_start();
  68. while ( have_posts() ) {
  69. the_post();
  70. wc_get_template_part( 'content', 'product' );
  71. }
  72. woocommerce_product_loop_end();
  73. }
  74. /**
  75. * Basic styling when infinite scroll is active only.
  76. */
  77. function jetpack_woocommerce_infinite_scroll_style() {
  78. $custom_css = "
  79. .infinite-scroll .woocommerce-pagination {
  80. display: none;
  81. }";
  82. wp_add_inline_style( 'woocommerce-layout', $custom_css );
  83. }
  84. function jetpack_woocommerce_lazy_images_compat() {
  85. wp_add_inline_script( 'wc-cart-fragments', "
  86. jQuery( 'body' ).bind( 'wc_fragments_refreshed', function() {
  87. jQuery( 'body' ).trigger( 'jetpack-lazy-images-load' );
  88. } );
  89. " );
  90. }
  91. add_action( 'wp_enqueue_scripts', 'jetpack_woocommerce_lazy_images_compat', 11 );