<?php
/**
 * Shop breadcrumb
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/global/breadcrumb.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see 	    https://docs.woocommerce.com/document/template-structure/
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     2.3.0
 * @see         woocommerce_breadcrumb()
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
global $wp;
global $wp_query;

if ( ! empty( $breadcrumb ) ) {
    $childTerms = false;
    if ( ( $wp_query->is_archive && isset( $wp_query->query['product_cat'] ) ) || is_singular( 'product' ) ) {  //means we have posts by category
        echo $wrap_before;
        if ( is_singular( 'product' ) ) {
            global $post;
            $parentTerm = diatex_get_parent_term( $post->ID );
            if ( $parentTerm ) {
                echo '<a class="current-custom-breadcrumb" href="' . get_term_link( $parentTerm->term_id ) . '">' . __( 'Shop', 'erfolg' ) . ' ' . $parentTerm->name . '</a>';
                echo '<span class="breadcrumb-separator"> / </span>';
                $childTerms = diatex_get_child_terms( $parentTerm->term_id );
                if ( $childTerms ) {
                    $counter = count( $childTerms );
                    $r = 1;
                    foreach ( $childTerms as $childTerm ) {
                        echo '<a href="' . get_term_link( $childTerm->term_id ) . '">' . $childTerm->name . '</a>';

                        echo ( $r == $counter ? '' : '<span class="breadcrumb-separator"> / </span>' );
                        $r++;
                    }
                }
            }
        }
        if ( isset( $wp_query->query['product_cat'] ) ) {
            $currentCat = get_queried_object();
            if ( $currentCat->parent == 0 ) { //we are on category without parents
                echo '<a class="current-custom-breadcrumb" href="' . get_term_link( $currentCat->term_id ) . '">' . __( 'Shop', 'erfolg' ) . ' ' . $currentCat->name . '</a><span class="breadcrumb-separator"> / </span>';
                //we get the children now
                diatex_child_terms_html_breadcrumb( $currentCat->term_id, $currentCat->term_id );
            } else {
                $termParent = get_term( $currentCat->parent );
                echo '<a href="' . get_term_link( $termParent->term_id ) . '">' . __( 'Shop', 'erfolg' ) . ' ' . $termParent->name . '</a><span class="breadcrumb-separator"> / </span>';
                diatex_child_terms_html_breadcrumb( $termParent->term_id, $currentCat->term_id );
            }
        }
    }
    
    /**
     * https://redmine.reea.net/issues/116479#note-14
     * 
     * uncomment update_option and set active so soldes show or inactive so soldes don't show
     * comment out after run
     */

    //update_option( 'on_sale_active', 'active' );
    if ( get_option( 'on_sale_active' ) == 'active' ) {
        if ( is_tax( 'product_cat' )  ) {
            echo '<div class="' . get_option( 'on_sale_active' ) . ' on-sale-sort"><a href="' . home_url( $wp->request ) . '/?orderby=on_sale&d"><img src="' . get_stylesheet_directory_uri() . '/images/soldes2.gif"></a></div>';
        }
    }
    echo $wrap_after;
}

