<?php

/*************************************************
## Woocommerce 
*************************************************/

if ( class_exists( 'woocommerce' ) ) {
add_theme_support( 'woocommerce' );
add_image_size('froday-woo-product', 450, 450, true);

// Remove woocommerce defauly styles
add_filter( 'woocommerce_enqueue_styles', '__return_false' );

// hide default shop title anasayfadaki title gizlemek için
add_filter('woocommerce_show_page_title', 'override_page_title');
function override_page_title() {
return false;
}

remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 ); /*remove result count above products*/

remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 ); //remove rating
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 ); //remove rating

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10);
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);

add_action( 'woocommerce_before_shop_loop_item', 'froday_shop_thumbnail', 10);
add_action( 'momizat_woo_product_head', 'woocommerce_template_loop_add_to_cart', 10);
add_action( 'momizat_woo_product_details', 'woocommerce_template_loop_add_to_cart', 20);

remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products',20);
remove_action( 'woocommerce_after_single_product', 'woocommerce_output_related_products',10);
add_action( 'woocommerce_after_single_product_summary', 'froday_related_products', 20);
function froday_related_products(){
    woocommerce_related_products( array('posts_per_page' => 2));
}

remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title',10);

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);

add_action('extra_woocommerce_single','woocommerce_template_single_add_to_cart');
add_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',11);

/*----------------------------
  Add my owns
 ----------------------------*/
function froday_shop_thumbnail () {
		$id = get_the_ID();
		global $product;
		global $woocommerce;
		$rating = wc_get_rating_html($product->get_average_rating()); //get rating
	    $cart_url = wc_get_cart_url();
		$price = $product->get_price_html();
		$sale_price_dates_to    = ( $date = get_post_meta( $id, '_sale_price_dates_to', true ) ) ? date_i18n( 'Y/m/d', $date ) : '';

		$output = '';

		$att=get_post_thumbnail_id();
		$image_src = wp_get_attachment_image_src( $att, 'full' );
		$image_src = $image_src[0]; 

		$percentage = '';
		if( $product->get_sale_price() && $product->get_regular_price()){
			$percentage .= '<div class="label-discount left-20 top-15">-';			
			$percentage .= ceil(100 - ($product->get_sale_price() / $product->get_regular_price()) * 100);			
			$percentage .= '%</div>';


		wp_enqueue_script('froday-countdown-shop');
		wp_localize_script('froday-countdown-shop', 'translate' , 
		array( 
			'enable' => ot_get_option('froday_translate_enable_countdown'),
			'week' => ot_get_option('froday_translate_week_countdown'),
			'day' => ot_get_option('froday_translate_day_countdown'),
			'day' => ot_get_option('froday_translate_day_countdown'),
			'days' => ot_get_option('froday_translate_days_plural_countdown'),
			'expired' => ot_get_option('froday_translate_expired_countdown'),
		));
			
		}

    ?>


	<?php

		$output .= '<div class="deal-single panel">';
		$output .= '<figure class="deal-thumbnail embed-responsive embed-responsive-16by9" data-bg-img="'.esc_url($image_src).'">';
		$output .= $percentage;
		$output .= '<div class="time-left bottom-15 right-20 font-md-14">';
		if($sale_price_dates_to){
		$output .= '<span>';
		$output .= '<i class="ico fa fa-clock-o mr-10"></i>';
		$output .= '<span class="t-uppercase" data-countdown="'.$sale_price_dates_to.'"></span>';
		$output .= '</span>';
		}
		$output .= '</div>';
		$output .= '<div class="deal-store-logo">';
		if(function_exists( 'klbshortcode' )){
		$output .= froday_store_thumbnail();
		}
		$output .= '</div>';
		$output .= '</figure>';
		$output .= '<div class="bg-white pt-20 pl-20 pr-15">';
		$output .= '<div class="pr-md-10">';
		$output .= '<div class="rating mb-10">';
		$output .= $rating;
		$output .= '</div>';
		$output .= '<h3 class="deal-title mb-10">';
		$output .= '<a href="'.get_permalink().'">'.get_the_title().'</a>';
		$output .= '</h3>';
		if(function_exists( 'klbshortcode' )){
		$output .= '<ul class="deal-meta list-inline mb-10 color-mid">';
		$output .= froday_location_name();
		$output .= '<li><i class="ico fa fa-shopping-basket mr-10"></i>'.froday_store_title().'</li>';
		$output .= '</ul>';
		}
		$output .= '<p class="text-muted mb-20">'.froday_limit_words(get_the_excerpt(), '11').'</p>';
		$output .= '</div>';
		$output .= '<div class="deal-price pos-r mb-15">';
		$output .= '<h3 class="price ptb-5 text-right">'.$price.'</h3>';
		$output .= '</div>';
		$output .= '</div>';
		$output .= '</div>';
		  
	 echo froday_sanitize_data($output);

}

/*************************************************
## Woocommerce Cart Text
*************************************************/

//add to cart button
function froday_add_to_cart_button(){
	global $product;
	$output = '';

	ob_start();
	woocommerce_template_loop_add_to_cart();
	$output .= ob_get_clean();

	if(!empty($output)){
		$pos = strpos($output, ">");
		
		if ($pos !== false) {
		    $output = substr_replace($output,">", $pos , strlen(1));
		}
	}
	
	if($product->get_type() == 'variable' && empty($output)){
		$output = "<a class='btn btn-primary add-to-cart cart-hover' href='".get_permalink($product->id)."'>".esc_html__('Select options','froday')."</a>";
	}

	if($product->get_type() == 'simple'){
		$output .= "";
	} else {
		$btclass  = "single_bt";
	}
	
	if($output) return "$output";
}


/*************************************************
## Shop Coupon Post Type Filter
*************************************************/ 

function froday_woocommerce_register_post_type_shop_coupon( $products_args ) { 

	$products_args = array(
						'labels'              => array(
								'name'                  => esc_html__( 'Coupons', 'froday' ),
								'singular_name'         => esc_html__( 'Coupon', 'froday' ),
								'menu_name'             => esc_html_x( 'Coupons', 'Admin menu name', 'froday' ),
								'add_new'               => esc_html__( 'Add coupon', 'froday' ),
								'add_new_item'          => esc_html__( 'Add new coupon', 'froday' ),
								'edit'                  => esc_html__( 'Edit', 'froday' ),
								'edit_item'             => esc_html__( 'Edit coupon', 'froday' ),
								'new_item'              => esc_html__( 'New coupon', 'froday' ),
								'view_item'             => esc_html__( 'View coupon', 'froday' ),
								'search_items'          => esc_html__( 'Search coupons', 'froday' ),
								'not_found'             => esc_html__( 'No coupons found', 'froday' ),
								'not_found_in_trash'    => esc_html__( 'No coupons found in trash', 'froday' ),
								'parent'                => esc_html__( 'Parent coupon', 'froday' ),
								'filter_items_list'     => esc_html__( 'Filter coupons', 'froday' ),
								'items_list_navigation' => esc_html__( 'Coupons navigation', 'froday' ),
								'items_list'            => esc_html__( 'Coupons list', 'froday' ),
							),
						'description'         => esc_html__( 'This is where you can add new coupons that customers can use in your store.', 'froday' ),
						'public'              => true,
						'show_ui'             => true,
						'capability_type'     => 'shop_coupon',
						'map_meta_cap'        => true,
						'publicly_queryable'  => true,
						'show_in_menu'        => current_user_can( 'manage_woocommerce' ) ? 'woocommerce' : true,
						'hierarchical'        => false,
						'rewrite'             => false,
						'has_archive'        => false,
						'query_var'           => false,
						'supports'            => array( 'title' ),
						'show_in_nav_menus'   => true,
						'show_in_admin_bar'   => true,
					);

    // make filter magic happen here... 
    return $products_args; 
}; 
         
// add the filter 
add_filter( 'woocommerce_register_post_type_shop_coupon', 'froday_woocommerce_register_post_type_shop_coupon', 10, 1 ); 


/*************************************************
## Woo Cart Ajax
*************************************************/ 

add_filter('add_to_cart_fragments', 'froday_header_add_to_cart_fragment');
function froday_header_add_to_cart_fragment( $fragments ) {
	global $woocommerce;
	ob_start();
	?>
	
	<span class="cart-number"> <?php echo sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'froday'), $woocommerce->cart->cart_contents_count);?> </span>
	

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

	return $fragments;
}

/*************************************************
## Froday Woo Search Form
*************************************************/ 

add_filter( 'get_product_search_form' , 'froday_custom_product_searchform' );

function froday_custom_product_searchform( $form ) {

	$form = '
	<form class="search-form woocommerce-product-search" role="search" method="get" id="searchform" action="' . esc_url( home_url( '/'  ) ) . '">
		<div class="input-group">
			<input class="query form-control input-lg search-input" type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="' . esc_html__( 'Enter Keyword Here ...', 'froday' ) . '" />
			<div class="input-group-btn">
				<div class="input-group">
					<div class="input-group-btn">
						<button type="submit" class="btn btn-lg btn-search btn-block">
							<i class="fa fa-search font-16"></i>
						</button>
						<input type="hidden" name="post_type" value="product" />
					</div>
				</div>
			</div>
		</div>
	</form>';

	return $form;
}


/*************************************************
## Froday Woo Advanced Search Form
*************************************************/ 

function froday_advanced_product_searchform() {
?>
<script>
jQuery.noConflict();
(function ($) {
  "use strict";
	$(document).ready(function(e) {
		$('.search-select').on('change', function(){
			if(($(this).val() == 'shop_coupon')||($(this).val() == 'masonry')) {
				$('.product_post_type_search').attr("disabled", true);				
				$('.coupon_post_type_search').removeAttr("disabled", true);				
			} else {
			
				$('.coupon_post_type_search').attr("disabled", true);	
				$('.product_post_type_search').removeAttr("disabled", true);	
				
			}
		});
	});
})(jQuery);	
</script>

<?php

	$form = '
	<form class="search-form woocommerce-product-search" role="search" method="get" id="searchform_advanced" action="' . esc_url( home_url( '/'  ) ) . '">
		<div class="input-group">
			<input class="query form-control input-lg search-input" type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="' . esc_html__( 'Enter Keyword Here ...', 'froday' ) . '" />
			<div class="input-group-btn">
				<div class="input-group">
					<select class="form-control input-lg search-select">
						<option>'.esc_html__('Select Your Category','froday').'</option>
						<option name="post_type" value="product">'.esc_html__('Deals','froday').'</option>
						<option name="post_type" value="shop_coupon">'.esc_html__('Coupons','froday').'</option>
					</select>
					<div class="input-group-btn">
						<button type="submit" class="btn btn-lg btn-search btn-block">
							<i class="fa fa-search font-16"></i>
						</button>
						
						<input type="hidden" class="product_post_type_search" name="post_type" value="product" />
						<input type="hidden" class="coupon_post_type_search"name="post_type" value="shop_coupon" disabled>
					</div>
				</div>
			</div>
		</div>
	</form>';

	return $form;
}


} // is woocommerce activated

?>