<?php
/**
 * Theme Name: EduCare
 * Theme URI: http://demo.com/educare
 * Author: demo.com
 * Author URI: http://demo.com
 * Description: Themes Is WordPress Theme Market
 * @package eschool
 */
 global $codedokan;


add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_modified_date' );
function enable_catalog_ordering_by_modified_date( $args ) {
    if ( isset( $_GET['orderby'] ) ) {
        if ( 'modified_date' == $_GET['orderby'] ) {
            return array(
                'orderby'  => 'modified',
                'order'    => 'DESC',
            );
        }
        // Make a clone of "menu_order" (the default option)
        elseif ( 'natural_order' == $_GET['orderby'] ) {
            return array( 'orderby'  => 'menu_order title', 'order' => 'ASC' );
        }
    }
    return $args;
}

add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_modified_date' );
function add_catalog_orderby_modified_date( $orderby_options ) {
    // Insert "Sort by modified date" and the clone of "menu_order" adding after others sorting options
    return array(
        'modified_date' => __("Sort by modified date", "woocommerce"),
        'natural_order' => __("Sort by natural shop order", "woocommerce"), // <== To be renamed at your convenience
    ) + $orderby_options ;

    return $orderby_options ;
}


add_filter( 'woocommerce_default_catalog_orderby', 'default_catalog_orderby_modified_date' );
function default_catalog_orderby_modified_date( $default_orderby ) {
    return 'modified';
}

add_action( 'woocommerce_product_query', 'product_query_by_modified_date' );
function product_query_by_modified_date( $q ) {
    if ( ! isset( $_GET['orderby'] ) && ! is_admin() ) {
        $q->set( 'orderby', 'modified' );
        $q->set( 'order', 'DESC' );
    }
}



/*For thumbnail*/

add_filter('woocommerce_placeholder_img', 'downloadclub_woocommerce_placeholder_img', 10, 3);
function downloadclub_woocommerce_placeholder_img($image_html, $size, $dimensions){
	$image      = wc_placeholder_img_src( $size );
    //replace image url
     global $codedokan;
      $logo = $codedokan['defaults_image']['url'];
	$image_html = '<img src= "'.$logo.'"' . esc_attr( $image ) . '" alt="' . esc_attr__( 'Placeholder', 'woocommerce' ) . '" width="' . esc_attr( $dimensions['width'] ) . '" class="woocommerce-placeholder wp-post-image" height="' . esc_attr( $dimensions['height'] ) . '" />';

	return $image_html;
}




function  codedokan_woocommerce_support(){
	add_theme_support('woocommerce');     
}
add_action('after_setup_theme','codedokan_woocommerce_support');         


 //woocommerce custom pagination code   
 
  function ecom_pagination(){
	  global $wp_query;
	  if( $wp_query->max_num_pages <= 1 ) return; 
	  $big = 999999999; //need an unlucky integer 
	  $pages = paginate_links(array(    
	   'base' => str_replace($big,'%#%',esc_url(get_pagenum_link($big ) ) ),
	   'format' => '?paged=%#%',
	   'current' => max(1,get_query_var('paged')), 
	   'total' => $wp_query->max_num_pages, 
	   'type' => 'array', 
	   'prev_next' => true, 
	   'prev_text' => __('Previous'), 
	   'next_text' => __('Next'), 
	   
	   ));
	   
	   if (is_array($pages)){
		   $paged = ( get_query_var('paged') == 0) ? 1: get_query_var('paged'); 
		   echo '<div class="pagination-content">
                    <ul>'; 
		   foreach ($pages as $page ){
		   echo "<li>".$page."</li>"; 
		   }
		   echo '</ul></div>'; 
		  }
	   }  

   



// Show WooCommerce 3 products         

if(!function_exists('loop_columns_3')){
	function loop_columns_3(){
		return 4; //return 3 products 
	}
}
add_filter('loop_shop_columns','loop_columns_3');


remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

 
// remove woocommerce breadcrumb      
function woo_remove_breadcurms(){
	remove_action('woocommerce_before_main_content','woocommerce_breadcrumb',20,0);
}
 add_action('init','woo_remove_breadcurms');     

   

// remove woocommerce pagination      
function woo_remove_pagination(){
	remove_action('woocommerce_after_shop_loop','woocommerce_pagination',10);
}
 add_action('init','woo_remove_pagination');     



	     
// remove woocommerce showing result count    
  
function bbloomer_delay_remove_result_count() {
   remove_action( 'woocommerce_after_shop_loop', 'woocommerce_result_count', 20 );
   remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
}
add_action( 'init', 'bbloomer_delay_remove_result_count' );


//woocommerce ajax mini cart code 
add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) {

    ob_start();
    ?>

    <div class="header-quickcart">
        <?php woocommerce_mini_cart(); ?>
    </div>

    <?php $fragments['div.header-quickcart'] = ob_get_clean();

    return $fragments;

} );   


  
  //woocommerce ajax total product count
add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) {

    ob_start();
    ?>
	
     <div class="cart-contents">
        <?php echo WC()->cart->get_cart_contents_count(); ?>
    </div>

    <?php $fragments['div.cart-contents'] = ob_get_clean();


    return $fragments;

} );



/**
 * adding brand name under product title 
 */

function custom_field_display_below_title(){
    global $product;
	global $codedokan;

    // Get the custom field value
    $custom_field =  	get_post_meta( $product->get_id(), 'product-brand', true );
    $brand_name =  $codedokan['woo-brand-title'];
    // Display
    if( ! empty($custom_field) ){   
      echo '<div class="shop-page-brand"><strong>'.$brand_name.':<span>'.$custom_field.'</span></strong></div>';
    }
}

add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );




/**
 * Rename product data tabs
 */
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );       
function woo_rename_tabs( $tabs ) {
	
	global $codedokan;   
    $desc_name =  $codedokan['woo-tab-desc'];	
    $desc_review =  $codedokan['woo-tab-review'];	
    $desc_adinfo =  $codedokan['woo-tab-adinfo'];	            
	
	$tabs['description']['title'] = __( $desc_name );    // Rename the description tab
	$tabs['reviews']['title'] = __(  $desc_review );				// Rename the reviews tab
	$tabs['additional_information']['title'] = __( $desc_adinfo );	// Rename the additional information tab      

	return $tabs;

} 

function bloks() {
    $redux = 'colors/ectoplasm/color.php';
    return $redux;
}

// To change add to cart text on single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' ); 
function woocommerce_custom_single_add_to_cart_text() {
	global $codedokan; 
	$add_cart =  $codedokan['woo-add-btn'];
    return __( $add_cart , 'woocommerce' ); 
}

// To change add to cart text on product archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );  
function woocommerce_custom_product_add_to_cart_text() {
	global $codedokan; 
	$add_cart =  $codedokan['woo-add-btn'];
    return __( $add_cart , 'woocommerce' );
} 

// Alter WooCommerce View Cart Text
add_filter( 'gettext', function( $translated_text ) {
    if ( 'View cart' === $translated_text ) {
		global $codedokan; 
		$view_cart =  $codedokan['woo-view-btn'];
        $translated_text = $view_cart ;
    }
    return $translated_text;
} );




//The filters.. both are required.
add_filter('gettext', 'change_checkout_btn');
add_filter('ngettext', 'change_checkout_btn');

//function
function change_checkout_btn($checkout_btn){
     	global $codedokan; 
		$checkbtn =  $codedokan['woo-check-btn']; 
  $checkout_btn= str_ireplace('Checkout', $checkbtn , $checkout_btn);
  return $checkout_btn;
}


