shshanker
10/5/2015 - 7:48 AM

Customize woocommerce breadcrum parameters.

Customize woocommerce breadcrum parameters.

<?php
// Customize  woocommerce breadcrum parameters
// define the woocommerce_get_breadcrumb callback
function filter_woocommerce_get_breadcrumb( $crumbs, $instance ) 
{
    // make filter magic happen here...

    	if(is_product_category()){ 

    		$cat_crumbs = $crumbs[1];
	    	if($cat_crumbs[0] == 'Products') { 
	    		$crumbs[1][0] = 'Our Products';
	    		$crumbs[1][1] =  get_permalink(51);
	    	}
    		return $crumbs; }

    	$full_path = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    	$parts = Explode('/', $full_path);
		$current_cat = $parts[count($parts) - 2];
    	$terms = get_term_by('slug', $current_cat, 'product_cat');
    	$term_id = $terms->term_id;
    	$term_name = $terms->name;
    	$ancestors = $terms->parent;
    	$ancestors= get_term_by('id', $ancestors, 'product_cat');
    	$ancestors_name = $ancestors->name;
    	$lastCrumbsElement = end($crumbs);

    	// change overall stucture of breadcrumbs
    	$parents = get_ancestors( $term_id, 'product_cat' );
    	$parents = array_reverse($parents);
    	$count = count($parents);
    	
    	$parrents_id = array();
    	$parrents_name = array();
    	foreach( $parents as $key => $values){
	    	$parent_cat = get_term_by('id', $values, 'product_cat');
	    	$parrents_id[] = $parent_cat->term_id;
	    	$parrents_name[] = $parent_cat->name;
    	}

    	$combined_id_parents = array_combine($parrents_id, $parrents_name);
    	   
	    	$i = 1;
	    	foreach($combined_id_parents as $key => $value){	    	
	    		$crumbs[$i][0] = $value;	    	
	    		$crumbs[$i][1] = get_term_link( $key, 'product_cat' );
	    		$i++;
	    		}	    	
	    		
		   		$crumbs[$i][0] = $term_name;
		   		$crumbs[$i][1] = get_term_link( $term_id, 'product_cat' );
		   		$i++;
		   		$crumbs[$i][0] = $lastCrumbsElement[0];
		   		$crumbs[$i][1] = '';

		   		if($crumbs[$i][1] == ''){
		   			$crumbs = array_slice($crumbs, 0, $i+1);
		   		}
		   		/*echo "<pre>";
		   		print_r($crumbs);
		   		echo "</pre>";*/
	   		
		//change "Proucts" breadcrumbs  
    	
    	
    	 //change breadcrumbs based on Products, size and Color.
    	if( $ancestors == NULL ){
    		if( $term_name == 'Products'){
	    		$crumbs[1][0] = 'Our Products';
	    		$crumbs[1][1] =  get_permalink(51);
	    		$crumbs[2][0] =  $lastCrumbsElement[0];
	    		$crumbs[2][1] =  '';
	    	} elseif($term_name == 'size'){
	    		$crumbs[1][0] = 'Size';
	    		$crumbs[1][1] =  get_term_link( $terms, '');	    	
	    		$crumbs[2][0] =  $lastCrumbsElement[0];
	    		$crumbs[2][1] =  '';
	    	}elseif($term_name == 'Color'){
	    		$crumbs[1][0] = 'Color';
	    		$crumbs[1][1] =  get_term_link( $terms, '');
	    		$crumbs[2][0] =  $lastCrumbsElement[0];
	    		$crumbs[2][1] =  '';
	    	}
	    	$crumbs = array_slice($crumbs, 0, 3); 
    		} /*else {     	
	    	if( $ancestors_name == 'Products'){
	    		$crumbs[1][0] = 'Our Products';
	    		$crumbs[1][1] =  get_permalink(51);
	    		$crumbs[2][0] =  $term_name;
	    		$crumbs[2][1] =  get_term_link( $terms, '');
	    		$crumbs[3][0] =  $lastCrumbsElement[0];
	    		$crumbs[3][1] =  '';
	    	} elseif($ancestors_name == 'size'){
	    		$crumbs[1][0] = 'Size';
	    		$crumbs[1][1] =  get_term_link( $ancestors, '');
	    		$crumbs[2][0] =  $term_name;
	    		$crumbs[2][1] =  get_term_link( $terms, '');
	    		$crumbs[3][0] =  $lastCrumbsElement[0];
	    		$crumbs[3][1] =  '';
	    	}elseif($ancestors_name == 'Color'){
	    		$crumbs[1][0] = 'Color';
	    		$crumbs[1][1] =  get_term_link( $ancestors, '');
	    		$crumbs[2][0] =  $term_name;
	    		$crumbs[2][1] =  get_term_link( $terms, '');
	    		$crumbs[3][0] =  $lastCrumbsElement[0];
	    		$crumbs[3][1] =  '';
	    	}
	    	$crumbs = array_slice($crumbs, 0, 4); 
    	}  */	
    	$cat_crumbs = $crumbs[1];
	    	if($cat_crumbs[0] == 'Products') { 
	    		$crumbs[1][0] = 'Our Products';
	    		$crumbs[1][1] =  get_permalink(51);
	    	}
    	 
    return $crumbs;
};        
// add the filter
add_filter( 'woocommerce_get_breadcrumb', 'filter_woocommerce_get_breadcrumb', 10, 2 );

?>