<?php
/**
* Template hooks
* @package store-expert
*/


/**
 * Check if WooCommerce plugin is activated
 */
if ( ! function_exists( 'store_expert_is_woocommerce_activated' ) ) {
	/**
	 * Query WooCommerce activation
	 */
	function store_expert_is_woocommerce_activated() {
		return class_exists( 'WooCommerce' ) ? true : false;
	}
}

/**
 * Add Cart icon and count to header if WC is active
 */
function store_expert_wc_cart_count() {
	/**
	 * Check if WooCommerce plugin is activated
	 */
	if ( store_expert_is_woocommerce_activated() ) { 
		/**
		 * Fetch the total product in cart
		 */
		$count = WC()->cart->cart_contents_count; 
	?>
	<div class="logo-cart align-items-center">
		<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart','store-expert' ); ?>" class="cart-icon clearfix">			
			<div class="head-cart-count">
				<p class="cart-total-value">
					<?php
						/**
						 * Fetch the total cart value
						 */
						echo esc_html( wc_cart_totals_order_total_html() ); 
					?>
				</p>
			</div>
			<div class="head-cart-icon">
				<span class="icon-bag"></span>
				<span class="wishlist-count">
					<?php
						/**
						 * Echo the cart count
						 */
						echo esc_html( $count ); 
					?>
				</span>
			</div>
			
		</a>		
	</div>
	<?php
	}
}

/**
 * Ensure cart contents update when products are added to the cart via AJAX
 */
function store_expert_add_to_cart_fragment( $fragments ) {
 	
	ob_start();
	$count = WC()->cart->cart_contents_count;
	/**
	 * Fetch the total product in cart
	 */
	?>
	
	<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart','store-expert' ); ?>" class="cart-icon clearfix">			
		<div class="head-cart-count">
			<p class="cart-total-value">
				<?php
					/**
					 * Fetch the total cart value
					 */
					echo esc_html( wc_cart_totals_order_total_html() ); 
				?>
			</p>
		</div>
		<div class="head-cart-icon">
			<span class="icon-bag"></span>
			<span class="wishlist-count">
				<?php
					/**
					 * Echo the cart count
					 */
					echo esc_html( $count ); 
				?>
			</span>
		</div>
		
	</a>		
	
	<?php

		$fragments['a.cart-icon'] = ob_get_clean();

		return $fragments; 
	?>

	<?php
}

/**
 * Add heart icon and wishlist count to header if WC is active
 */
function store_expert_head_wishlist() {
	/**
	* Check if WooCommerce and Yith Wishlist plugins activated
	*/
	if ( function_exists( 'YITH_WCWL' ) && store_expert_is_woocommerce_activated() ) {
		/**
		* Get url of wishlist page
		*/
		$wishlist_url = YITH_WCWL()->get_wishlist_url();
		?>
		<div class="logo-love">
			<a href="<?php echo esc_url( $wishlist_url ); ?>" title="<?php esc_attr_e( 'Your Wishlist', 'store-expert' ); ?>" class="love-icon">
				<span class="icon-heart"></span>
			</a>
			<span class="wishlist-count">
				<?php echo esc_html( yith_wcwl_count_all_products() ); ?>
			</span>
		</div>
		<?php
	}
}

function store_expert_wishlist_ajax_update_count() {
	/**
	* Check if WooCommerce and Yith Wishlist plugins activated
	*/
	if ( function_exists( 'YITH_WCWL' ) && store_expert_is_woocommerce_activated() ) {
		wp_send_json( array(
		  'count' => yith_wcwl_count_all_products(),
		) );
	}
}


/**
 * Add to wishlist in product loop
 */
function store_expert_display_yith_wishlist_loop() {
	if ( function_exists( 'YITH_WCWL' ) && store_expert_is_woocommerce_activated() ) {
		echo do_shortcode( "[yith_wcwl_add_to_wishlist icon='fa-heart' label='' browse_wishlist_text='' already_in_wishslist_text='' product_added_text='']" );
	}
}


/**
 * Change number or products per row to 3
 */
if (!function_exists('store_expert_loop_columns')) {
	function store_expert_loop_columns() {
		return 4; // 3 products per row
	}
}

/**
 * Change number or products per page to 12
 */
function store_expert_shop_per_page( $cols ) {
  // $cols contains the current number of products per page based on the value stored on Options -> Reading
  // Return the number of products you wanna show per page.
  $cols = 12;
  return $cols;
}

/**
 * Change number or related products on product page
 */
function store_expert_related_products_limit() {
	global $product;
	$args['posts_per_page'] = 3;
	return $args;
}
function store_expert_related_products_args( $args ) {
	$args['posts_per_page'] = 4; // 4 related products
	$args['columns'] = 4; // arranged in 4 columns
	return $args;
}





add_action( 'store_expert_header_cart', 'store_expert_wc_cart_count' );
add_action( 'store_expert_wishlist', 'store_expert_head_wishlist' );
add_filter( 'woocommerce_add_to_cart_fragments', 'store_expert_add_to_cart_fragment' );
add_action( 'wp_ajax_yith_wcwl_update_wishlist_count', 'store_expert_wishlist_ajax_update_count' );
add_action( 'wp_ajax_nopriv_yith_wcwl_update_wishlist_count', 'store_expert_wishlist_ajax_update_count' );
add_action( 'woocommerce_before_shop_loop_item_title', 'store_expert_display_yith_wishlist_loop', 97 );
add_filter( 'loop_shop_columns', 'store_expert_loop_columns' );
add_filter( 'loop_shop_per_page', 'store_expert_shop_per_page', 20 );
add_filter( 'woocommerce_output_related_products_args', 'store_expert_related_products_args' );
/**
 * Remove the default page html
 */
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );





