Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function avia_woocommerce_breadcrumb( $trail, $args )
- {
- global $avia_config;
- $shop_id = 123;
- if(is_woocommerce())
- {
- $front_id = avia_get_option('frontpage');
- $home = isset( $trail[0] ) ? $trail[0] : '';
- $last = array_pop($trail);
- $taxonomy = "product_cat";
- // on the shop frontpage simply display the shop name, rather than shop name + "All Products"
- if(is_shop())
- {
- if(!empty($shop_id) && $shop_id != -1) $trail = array_merge( $trail, avia_breadcrumbs_get_parents( $shop_id ) );
- $last = "";
- if(is_search())
- {
- $last = __('Search results for:','avia_framework').' '.esc_attr($_GET['s']);
- }
- }
- // on the product page single page modify the breadcrumb to read [home] [if available:parent shop pages] [shop] [if available:parent categories] [category] [title]
- if(is_product())
- {
- //fetch all product categories and search for the ones with parents. if none are avalaible use the first category found
- $product_category = $parent_cat = array();
- $temp_cats = get_the_terms(get_the_ID(), $taxonomy);
- if( is_array( $temp_cats ) && ! empty( $temp_cats ) )
- {
- foreach( $temp_cats as $key => $cat )
- {
- if($cat->parent != 0 && !in_array($cat->term_taxonomy_id, $parent_cat))
- {
- $product_category[] = $cat;
- $parent_cat[] = $cat->parent;
- }
- }
- //if no categories with parents use the first one
- if(empty($product_category)) $product_category[] = reset($temp_cats);
- }
- //unset the trail and build our own
- unset($trail);
- $trail = ( empty( $home ) ) ? array() : array( 0 => $home );
- if(!empty($shop_id) && $shop_id != -1) $trail = array_merge( $trail, avia_breadcrumbs_get_parents( $shop_id ) );
- if(!empty($parent_cat)) $trail = array_merge( $trail, avia_breadcrumbs_get_term_parents( $parent_cat[0] , $taxonomy ) );
- 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>';
- }
- // add the [shop] trail to category/tag pages: [home] [if available:parent shop pages] [shop] [if available:parent categories] [category/tag]
- if(is_product_category() || is_product_tag())
- {
- if(!empty($shop_id) && $shop_id != -1)
- {
- $shop_trail = avia_breadcrumbs_get_parents( $shop_id ) ;
- array_splice($trail, 1, 0, $shop_trail);
- }
- }
- if(is_product_tag())
- {
- $last = __("Tag",'avia_framework').": ".$last;
- }
- if( ! empty( $last ) )
- {
- $trail['trail_end'] = $last;
- }
- /**
- * Allow to remove "Shop" in breadcrumb when shop page is frontpage
- *
- * @since 4.2.7
- */
- $trail_count = count( $trail );
- if( ( $front_id == $shop_id ) && ! empty( $home ) && ( $trail_count > 1 ) )
- {
- $hide = apply_filters( 'avf_woocommerce_breadcrumb_hide_shop', 'hide', $trail, $args );
- if( 'hide' == $hide )
- {
- $title = get_the_title( $shop_id );
- for( $i = 1; $i < $trail_count; $i++ )
- {
- if( false !== strpos( $trail[ $i ], $title ) )
- {
- unset( $trail[ $i ] );
- break;
- }
- }
- $trail = array_merge( $trail );
- }
- }
- }
- return $trail;
- }
- add_filter('avia_breadcrumbs_trail','avia_woocommerce_breadcrumb', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment