<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */


/**
* Integration WooCommerce
*
* @since Campoal 1.0.0
*/
if(!( function_exists('conikal_woocommerce_support') )) :
	function conikal_woocommerce_support() {
		add_theme_support( 'woocommerce' );
		add_theme_support( 'wc-product-gallery-zoom' );
		add_theme_support( 'wc-product-gallery-lightbox' );
		add_theme_support( 'wc-product-gallery-slider' );
	}
endif;
add_action( 'after_setup_theme', 'conikal_woocommerce_support' );


/**
 * Woocommerce page template
 *
 * @since Campoal 1.0.0
 */
if(!( function_exists('conikal_woocommerce_page_template') )) :
	function conikal_woocommerce_page_template( $template ) {
		global $post;
		
		if( is_archive() || is_404() || is_home() || !( isset($post->post_content) ) || is_search() )
			return $template;
			
		if( 
			has_shortcode($post->post_content, 'woocommerce_cart') || 
			has_shortcode($post->post_content, 'woocommerce_checkout') || 
			has_shortcode($post->post_content, 'woocommerce_my_account') 
		){
			
			$new_template = locate_template( array( 'page_woocommerce.php' ) );
			
			if (!( '' == $new_template )){
				return $new_template;
			}
			
		}
		
		return $template;
	}
	add_filter( 'template_include', 'conikal_woocommerce_page_template', 99 );
endif;


/**
 * Display Header Cart
 *
 * @since Campoal 1.0.0
 */
if ( !function_exists( 'conikal_header_cart' ) ) :
	function conikal_header_cart() {
		global $campoal_options;
		$cart_link_menu = isset($campoal_options['conikal_cart_link_menu_field']) ? $campoal_options['conikal_cart_link_menu_field'] : 1;
		$cart_subtotal = isset($campoal_options['conikal_cart_subtotal_field']) ? $campoal_options['conikal_cart_subtotal_field'] : 1;
		$cart_item_count = isset($campoal_options['conikal_cart_item_count_field']) ? $campoal_options['conikal_cart_item_count_field'] : 1;

		if (class_exists('WooCommerce')) {
			if (!is_cart() && !is_checkout() && $cart_link_menu == 1) { ?>
			<a class="item cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>">
				<i class="shopping bag icon"></i>
				<div class="conikal-cart">
					<?php echo wp_kses_post($cart_subtotal ? WC()->cart->get_cart_subtotal() : ''); ?>
					<?php echo esc_html($cart_subtotal && $cart_item_count ? ' - ' : '') ?>
					<?php echo wp_kses_data($cart_item_count ? sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'campoal' ), WC()->cart->get_cart_contents_count() ) : '' ); ?>
				</div>
				
			</a>
			<div class="mini-cart-popup ui popup bottom center transition hidden">
				<?php the_widget( 'WC_Widget_Cart', 'title=' ); ?>
			</div>
		<?php }
		}
	}
endif;

/**
 * Show cart count item
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_ajax_cart_total') ) :
	function conikal_ajax_cart_total( $fragments ) {
		global $woocommerce;
		global $campoal_options;
		$cart_subtotal = isset($campoal_options['conikal_cart_subtotal_field']) ? $campoal_options['conikal_cart_subtotal_field'] : 1;
		$cart_item_count = isset($campoal_options['conikal_cart_item_count_field']) ? $campoal_options['conikal_cart_item_count_field'] : 1;

		ob_start();
		echo '<div class="conikal-cart">' . wp_kses_post($cart_subtotal ? WC()->cart->get_cart_subtotal() : '') . esc_html($cart_subtotal && $cart_item_count ? ' - ' : '') . wp_kses_data($cart_item_count ? sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'campoal' ), WC()->cart->get_cart_contents_count() ) : '') .'</div>';
		$fragments['div.conikal-cart'] = ob_get_clean();
		return $fragments;
	}
endif;
add_filter('woocommerce_add_to_cart_fragments', 'conikal_ajax_cart_total');
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);


/**
 * Replace price html
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_price_html') ) :
	function conikal_price_html( $price, $product ) {
		$find = array('<span class="woocommerce-Price-amount amount">', '<del><span class="h4 inline-block">', '</del>', '<ins><span class="h4 inline-block">', '</ins>');
		$replace = array('<span class="h4 inline-block">', '<span class="h4 inline-block type strikethrough">', '', '<span class="h4 inline-block">', '');
		$price = str_replace($find, $replace, $price);                                              
	    return $price;
	}
endif;
// add_filter( 'woocommerce_get_price_html', 'conikal_price_html', 100, 2 );

/**
 * Output the view cart button
 * 
 * @since Campoal 1.0.0
 */
if( !function_exists('woocommerce_widget_shopping_cart_button_view_cart') ) :
	function woocommerce_widget_shopping_cart_button_view_cart() {
		echo '<a href="' . esc_url( wc_get_cart_url() ) . '" class="ui small right labeled icon button wc-forward"><i class="arrow right icon"></i>' . esc_html__( 'View cart', 'campoal' ) . '</a>';
	}
endif;


/**
 * Output the proceed to checkout button
 * 
 * @since Campoal 1.0.0
 */
if( !function_exists('woocommerce_widget_shopping_cart_proceed_to_checkout') ) :
	function woocommerce_widget_shopping_cart_proceed_to_checkout() {
		echo '<a href="' . esc_url( wc_get_checkout_url() ) . '" class="ui small right labeled icon button checkout wc-forward"><i class="arrow right icon"></i>' . esc_html__( 'Checkout', 'campoal' ) . '</a>';
	}
endif;

/**
 * Rename product data tabs
 * 
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_rename_tabs') ) :
	function conikal_rename_tabs( $tabs ) {

		if (isset($tabs['description'])) {
			$tabs['description']['title'] = esc_html__('Details', 'campoal');		// Rename the description tab
		}

		if (isset($tabs['reviews'])) {
			$tabs['reviews']['title'] = esc_html__('Reviews', 'campoal');				// Rename the reviews tab
		}
		
		if (isset($tabs['additional_information'])) {
			$tabs['additional_information']['title'] = esc_html__('Specification', 'campoal');	// Rename the additional information tab
		}

		return $tabs;
	}
endif;
add_filter( 'woocommerce_product_tabs', 'conikal_rename_tabs', 98 );