cipher87

Untitled

Feb 3rd, 2020
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. function avia_woocommerce_breadcrumb( $trail, $args )
  2. {
  3. global $avia_config;
  4. $shop_id = 123;
  5.  
  6. if(is_woocommerce())
  7. {
  8. $front_id = avia_get_option('frontpage');
  9. $home = isset( $trail[0] ) ? $trail[0] : '';
  10. $last = array_pop($trail);
  11. $taxonomy = "product_cat";
  12.  
  13. // on the shop frontpage simply display the shop name, rather than shop name + "All Products"
  14. if(is_shop())
  15. {
  16. if(!empty($shop_id) && $shop_id != -1) $trail = array_merge( $trail, avia_breadcrumbs_get_parents( $shop_id ) );
  17. $last = "";
  18.  
  19. if(is_search())
  20. {
  21. $last = __('Search results for:','avia_framework').' '.esc_attr($_GET['s']);
  22. }
  23. }
  24.  
  25. // on the product page single page modify the breadcrumb to read [home] [if available:parent shop pages] [shop] [if available:parent categories] [category] [title]
  26. if(is_product())
  27. {
  28. //fetch all product categories and search for the ones with parents. if none are avalaible use the first category found
  29. $product_category = $parent_cat = array();
  30. $temp_cats = get_the_terms(get_the_ID(), $taxonomy);
  31.  
  32. if( is_array( $temp_cats ) && ! empty( $temp_cats ) )
  33. {
  34. foreach( $temp_cats as $key => $cat )
  35. {
  36. if($cat->parent != 0 && !in_array($cat->term_taxonomy_id, $parent_cat))
  37. {
  38. $product_category[] = $cat;
  39. $parent_cat[] = $cat->parent;
  40. }
  41. }
  42.  
  43. //if no categories with parents use the first one
  44. if(empty($product_category)) $product_category[] = reset($temp_cats);
  45.  
  46. }
  47. //unset the trail and build our own
  48. unset($trail);
  49.  
  50. $trail = ( empty( $home ) ) ? array() : array( 0 => $home );
  51. if(!empty($shop_id) && $shop_id != -1) $trail = array_merge( $trail, avia_breadcrumbs_get_parents( $shop_id ) );
  52. if(!empty($parent_cat)) $trail = array_merge( $trail, avia_breadcrumbs_get_term_parents( $parent_cat[0] , $taxonomy ) );
  53. if(!empty($product_category)) $trail[] = '<a href="' . get_term_link( $product_category[0]->slug, $taxonomy ) . '" title="' . esc_attr( $product_category[0]->name ) . '">' . $product_category[0]->name . '</a>';
  54.  
  55. }
  56.  
  57.  
  58. // add the [shop] trail to category/tag pages: [home] [if available:parent shop pages] [shop] [if available:parent categories] [category/tag]
  59. if(is_product_category() || is_product_tag())
  60. {
  61. if(!empty($shop_id) && $shop_id != -1)
  62. {
  63. $shop_trail = avia_breadcrumbs_get_parents( $shop_id ) ;
  64. array_splice($trail, 1, 0, $shop_trail);
  65. }
  66. }
  67.  
  68. if(is_product_tag())
  69. {
  70. $last = __("Tag",'avia_framework').": ".$last;
  71. }
  72.  
  73. if( ! empty( $last ) )
  74. {
  75. $trail['trail_end'] = $last;
  76. }
  77.  
  78. /**
  79. * Allow to remove "Shop" in breadcrumb when shop page is frontpage
  80. *
  81. * @since 4.2.7
  82. */
  83. $trail_count = count( $trail );
  84. if( ( $front_id == $shop_id ) && ! empty( $home ) && ( $trail_count > 1 ) )
  85. {
  86. $hide = apply_filters( 'avf_woocommerce_breadcrumb_hide_shop', 'hide', $trail, $args );
  87. if( 'hide' == $hide )
  88. {
  89. $title = get_the_title( $shop_id );
  90.  
  91. for( $i = 1; $i < $trail_count; $i++ )
  92. {
  93. if( false !== strpos( $trail[ $i ], $title ) )
  94. {
  95. unset( $trail[ $i ] );
  96. break;
  97. }
  98. }
  99.  
  100. $trail = array_merge( $trail );
  101. }
  102. }
  103. }
  104.  
  105. return $trail;
  106. }
  107.  
  108. add_filter('avia_breadcrumbs_trail','avia_woocommerce_breadcrumb', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment