<?php 

if ( ! function_exists( 'woocommerce_support' ) ) {
	function woocommerce_support() {
		add_theme_support( 'woocommerce' );
	}
}
add_action( 'after_setup_theme', 'woocommerce_support' );


/**
 * Hooks to WooCommerce actions, filters
 */

add_action( 'after_setup_theme', 'yourtheme_setup' );
 
function yourtheme_setup() {
	add_theme_support( 'wc-product-gallery-lightbox' );
}


//	Products Columns
add_filter('loop_shop_columns', 'restorator_shop_columns');
if (!function_exists('restorator_shop_columns')) {
	function restorator_shop_columns() {
		global $restorator_options;
		
		if( isset( $restorator_options['shop_columns'] ) && $restorator_options['shop_columns'] == '4col') {
			return 4;
		}
		return 3;
	}
}

//	Products per page
global $restorator_options;

$restorator_products_per_page = ( strlen( $restorator_options['products_per_page'] ) > 0 ) ? intval( $restorator_options['products_per_page'] ) : 8;
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return ' . $restorator_products_per_page . ';' ), 20 );

//	Page Title
add_filter('woocommerce_show_page_title', 'restorator_woo_title_none');
if (!function_exists('restorator_woo_title_none')) {
	function restorator_woo_title_none(){
		return false;
	}
}

//	WooCommerce Product Item
add_action('woocommerce_before_shop_loop_item', 'restorator_before_shop_loop_item',5);
add_action('woocommerce_after_shop_loop_item', 'restorator_after_shop_loop_item',5);

if ( ! function_exists( 'restorator_before_shop_loop_item' ) ) {
	function restorator_before_shop_loop_item() {
		global $woocommerce, $product, $post, $restorator_options;
		
		$restorator_products_list_type = isset( $restorator_options['products_list_type'] ) ? $restorator_options['products_list_type'] : 'type1';
		?>
		<div class="product_wrap <?php if ( $product->is_on_sale() ) { echo 'product_sale'; } ?> <?php echo 'products_list_' . $restorator_products_list_type; ?>">

			<?php if( $restorator_products_list_type == 'type2' ) { ?>
				
				<div class="shop_list_product_image">
					<a href="<?php the_permalink(); ?>">
						<?php 
							echo woocommerce_get_product_thumbnail();
							
							$attachment_ids = $product->get_gallery_attachment_ids();
							if ( $attachment_ids ) {
								$secondary_image_id = $attachment_ids['0'];
								echo wp_get_attachment_image( $secondary_image_id, 'shop_catalog', '', $attr = array( 'class' => 'secondary-image attachment-shop-catalog' ) );
							}
						?>
					</a>
					<?php if ( $product->is_on_sale() ) {

						$restorator_sale_text = esc_attr__( 'Sale', 'restorator' );
				
						echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . $restorator_sale_text . '</span>', $post, $product );
						
					} ?>
				</div>
				<div class="shop_list_product_descr">
					<?php echo $product->get_categories( ', ', '<span class="product_categories">', '</span>' ); ?>
					<h6 class="product-title"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h6>
					<div class="price_wrap"><?php woocommerce_template_loop_price(); ?></div>
				</div>
			
			<?php } elseif( $restorator_products_list_type == 'type3' ) { ?>
				
				<div class="shop_list_product_image">
					<a href="<?php the_permalink(); ?>">
						<?php 
							echo woocommerce_get_product_thumbnail();
							
							$attachment_ids = $product->get_gallery_attachment_ids();
							if ( $attachment_ids ) {
								$secondary_image_id = $attachment_ids['0'];
								echo wp_get_attachment_image( $secondary_image_id, 'shop_catalog', '', $attr = array( 'class' => 'secondary-image attachment-shop-catalog' ) );
							}
						?>
					</a>
					<?php if ( $product->is_on_sale() ) {

						$restorator_sale_text = esc_attr__( 'Sale', 'restorator' );
				
						echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . $restorator_sale_text . '</span>', $post, $product );
						
					} ?>
					<?php woocommerce_template_loop_add_to_cart(); ?>
				</div>
				<div class="shop_list_product_descr">
					<div class="clearfix">
						<h6 class="product-title pull-left"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h6>
						<?php woocommerce_template_loop_price(); ?>
					</div>
					<?php woocommerce_template_loop_rating(); ?>
				</div>
			
			<?php } elseif( $restorator_products_list_type == 'type4' ) { ?>
				
				<div class="shop_list_product_image">
					<a href="<?php the_permalink(); ?>">
						<?php 
							echo woocommerce_get_product_thumbnail();
							
							$attachment_ids = $product->get_gallery_attachment_ids();
							if ( $attachment_ids ) {
								$secondary_image_id = $attachment_ids['0'];
								echo wp_get_attachment_image( $secondary_image_id, 'shop_catalog', '', $attr = array( 'class' => 'secondary-image attachment-shop-catalog' ) );
							}
						?>
					</a>
					<?php if ( $product->is_on_sale() ) {

						$restorator_sale_text = esc_attr__( 'Sale', 'restorator' );
				
						echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . $restorator_sale_text . '</span>', $post, $product );
						
					} ?>
					<?php woocommerce_template_loop_add_to_cart(); ?>
				</div>
				<div class="shop_list_product_descr text-center">
					<?php echo $product->get_categories( ', ', '<span class="product_categories">', '</span>' ); ?>
					
					<h6 class="product-title"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h6>
					
					<?php woocommerce_template_loop_price(); ?>
				</div>
				
			<?php } elseif( $restorator_products_list_type == 'type5' ) { ?>
				
				<div class="shop_list_product_image">
					<a href="<?php the_permalink(); ?>">
						<?php 
							echo woocommerce_get_product_thumbnail();
							
							$attachment_ids = $product->get_gallery_attachment_ids();
							if ( $attachment_ids ) {
								$secondary_image_id = $attachment_ids['0'];
								echo wp_get_attachment_image( $secondary_image_id, 'shop_catalog', '', $attr = array( 'class' => 'secondary-image attachment-shop-catalog' ) );
							}
						?>
					</a>
					<?php if ( $product->is_on_sale() ) {

						$restorator_sale_text = esc_attr__( 'Sale', 'restorator' );
				
						echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . $restorator_sale_text . '</span>', $post, $product );
						
					} ?>
				</div>
				<div class="shop_list_product_descr">
					<h6 class="product-title"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h6>
					<?php echo $product->get_categories( ', ', '<span class="product_categories">', '</span>' ); ?>
					<?php woocommerce_template_loop_price(); ?>
				</div>
				
			<?php } else { ?>
				
				<div class="shop_list_product_image">
					<a href="<?php the_permalink(); ?>">
						<?php 
							echo woocommerce_get_product_thumbnail();
							
							$attachment_ids = $product->get_gallery_attachment_ids();
							if ( $attachment_ids ) {
								$secondary_image_id = $attachment_ids['0'];
								echo wp_get_attachment_image( $secondary_image_id, 'shop_catalog', '', $attr = array( 'class' => 'secondary-image attachment-shop-catalog' ) );
							}
						?>
					</a>
					<?php if ( $product->is_on_sale() ) {

						$restorator_sale_text = esc_attr__( 'Sale', 'restorator' );
				
						echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . $restorator_sale_text . '</span>', $post, $product );
						
					} ?>
				</div>
				<div class="shop_list_product_descr">
					<h6 class="product-title"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h6>
					
					<?php woocommerce_template_loop_rating(); ?>
					
					<div class="clearfix">
						<span class="price"><?php woocommerce_template_loop_price(); ?></span>
						
						<?php woocommerce_template_loop_add_to_cart(); ?>
					</div>
				</div>
				
			<?php } ?>

			<div class="hide">
			<?php
	}
}

if ( ! function_exists( 'restorator_after_shop_loop_item' ) ) {
	function restorator_after_shop_loop_item() {
		?>
			</div>
		</div>
		<?php
	}
}


// Custom Shop Pagination
remove_action('woocommerce_after_shop_loop', 'woocommerce_pagination', 10);
add_action('woocommerce_after_shop_loop', 'restorator_get_shop_pagination', 10);
if (!function_exists('restorator_get_shop_pagination')) {	
	function restorator_get_shop_pagination() {
		echo restorator_pagination();
	}
}


/* Change Positions */
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title',5);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price',10);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt',20);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta',40);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_sharing',50);

add_action('woocommerce_single_product_summary', 'woocommerce_template_single_title', 10);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 20);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 30);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 40);	
add_action('woocommerce_single_product_summary', 'restorator_woocommerce_template_single_meta', 50);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 60);


/* Product Single Share Buttons */
add_action('woocommerce_share','restorator_wooshare');
function restorator_wooshare(){
	
	global $post, $restorator_options;

	$restorator_featured_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), false, '' );
?>
	
	<div class="cswoo_sharebox">
		<b><?php echo esc_attr__( 'Share', 'restorator' ); ?></b>
		
		<div class="cswoo_sharebox_links">
			<?php if( isset( $restorator_options['single_post_sharebox_facebook'] ) && $restorator_options['single_post_sharebox_facebook'] != '0') { ?>
				<a class="cswoo_social_link facebook" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" title="<?php esc_html_e( 'Facebook', 'restorator') ?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><i class="fa fa-facebook"></i></a>
			<?php } ?>
			
			<?php if( isset( $restorator_options['single_post_sharebox_twitter'] ) && $restorator_options['single_post_sharebox_twitter'] != '0') { ?>
				<a class="cswoo_social_link twitter" href="http://twitter.com/home?status=<?php the_title(); ?> <?php the_permalink(); ?>" title="<?php esc_html_e( 'Twitter', 'restorator') ?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><i class="fa fa-twitter"></i></a>
			<?php } ?>
			
			<?php if( isset( $restorator_options['single_post_sharebox_google'] ) && $restorator_options['single_post_sharebox_google'] != '0') { ?>
				<a class="cswoo_social_link google-plus" href="http://plus.google.com/share?url=<?php the_permalink() ?>&amp;title=<?php echo str_replace(' ', '+', the_title('', '', false)); ?>" title="<?php esc_html_e( 'Google+', 'restorator') ?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><i class="fa fa-google-plus"></i></a>
			<?php } ?>
			
			<?php if( isset( $restorator_options['single_post_sharebox_pinterest'] ) && $restorator_options['single_post_sharebox_pinterest'] != '0') { ?>
				<?php $restorator_featured_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID())); ?>
				<a class="cswoo_social_link pinterest" href="http://pinterest.com/pin/create/button/?url=<?php echo get_permalink(); ?>&media=<?php echo (strlen($restorator_featured_image_url[0]) > 0) ? $restorator_featured_image_url[0] : get_option( 'restorator_logo' ); ?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><i class="fa fa-pinterest-p"></i></a>
			<?php } ?>
		</div>
	</div>
	
<?php
}


/* Related Products on Single page */
function woo_related_products_limit() {
	global $product;
  
	$args = array(
		'post_type'             => 'product',
		'no_found_rows'         => 1,
		'posts_per_page'        => 4
	);
	return $args;
}
add_filter( 'woocommerce_related_products_args', 'woo_related_products_limit' );


//	Single Meta
function restorator_woocommerce_template_single_meta(){
	global $post, $product;
	
	?>
	<div class="product_meta">

		<?php do_action( 'woocommerce_product_meta_start' ); ?>

		<?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?>

			<span class="sku_wrapper"><b><?php echo esc_attr__( 'SKU', 'restorator' ); ?></b><span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : esc_attr__( 'N/A', 'restorator' ); ?></span></span>

		<?php endif; ?>

		<span class="posted_in"><b><?php echo esc_attr__( 'Categories', 'restorator' ); ?></b><?php echo $product->get_categories(); ?></span>

		<span class="tagged_as"><b><?php echo esc_attr__( 'Tags', 'restorator' ); ?></b><?php echo $product->get_tags(); ?></span>

		<?php do_action( 'woocommerce_product_meta_end' ); ?>

	</div>
<?php }


//	Nav Cart Icon
if(!function_exists('restorator_woo_nav_cart')) {
	function restorator_woo_nav_cart() {
		global $woocommerce;
	
		$inactive = '';
		$cart_count = $woocommerce->cart->get_cart_contents_count();
		?>
	
		<div id="woo-nav-cart">
			<div class="nav-cart-content">
				<i class="icon Evatheme-Icon-Fonts-thin-0443_shopping_cart_basket_store"></i>
				<?php if( $cart_count > 0 ) { ?>
					<span class="woo-cart-count"><?php echo esc_attr( $cart_count ); ?></span>
				<?php } ?>
			</div>
			<div class="nav-cart-products <?php if( $cart_count < 1 ) { echo 'cart_empty';} ?>">
				<div class="widget_shopping_cart"><div class="widget_shopping_cart_content"></div></div>				
			</div>
		</div>
	
	<?php 
	}
}
?>