<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive.
 *
 * Add page thumbnail and custom sections.
 *
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     3.4.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

get_header( 'shop' );

global $shop_template, $background_url, $fields_installed;

// detect if it's a taxonomy page
$is_shop_taxonomy = is_product_category() || is_product_tag();

// is product cat or tag
if ( $is_shop_taxonomy ) {
	global $wp_query;
	$cat = $wp_query->get_queried_object();
	$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
	$background_url = wp_get_attachment_image_src( $thumbnail_id, 'fullslideshownc', false );  // URL of Featured Full Image
	$background_url = $background_url[0];

	if ( is_product_category() ) {
		$custom_layout 	= $fields_installed ? get_field('cat_custom_page', 'product_cat_' . $cat->term_id) : false;
	}

	if ( is_product_tag() ) {
		$custom_layout 	= $fields_installed ? get_field('cat_custom_page', 'product_tag_' . $cat->term_id) : false;
	}
}

// is shop page archive
if ( is_shop() ) {
	$shop_id = get_option( 'woocommerce_shop_page_id' );

	// Get background url
	$background_url = get_post_meta( $shop_id, '_thumbnail_id', false ); // Get Image ID
	if( $background_url ) {
		$alt 			= get_post_meta( $background_url, '_wp_attachment_image_alt', true ); // Alt text of image
		$background_url = wp_get_attachment_image_src( $background_url[0], 'fullslideshownc', false );  // URL of Featured Full Image
		$background_url = $background_url[0];
	}
}

// use template from shop page for all pages
$shop_template = get_page_template_slug( get_option( 'woocommerce_shop_page_id' ) ); ?>

<div class="pagewrapper <?php if ( $background_url ) { echo 'has-background';  } else { echo 'no-background'; }; ?>">

	<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>

		<?php get_template_part('functions/templates/title-full'); ?>

	<?php endif; ?>

	<?php if ( $is_shop_taxonomy && $custom_layout == 'yes' ) : ?>
		<?php get_template_part(TW_SECTIONS_DIR .'/sections'); // Display sections ?>
	<?php else: ?>

		<?php
			// Page sections
			if ( ! $is_shop_taxonomy ) :
				get_template_part( TW_SECTIONS_DIR . '/sections' );
			endif;
		?>

		<?php
			/**
			 * woocommerce_before_main_content hook
			 *
			 * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
			 * @hooked woocommerce_breadcrumb - 20
			 */
			do_action( 'woocommerce_before_main_content' );
		?>

		<?php if ( 'template-full.php' == $shop_template ) : ?>
			<div class="woocommerce-content">
		<?php else : ?>
			<div class="rightside">
				<div class="rightcontent">
					<div class="postcontent">
		<?php endif; ?>

		<?php if ( have_posts() ) : ?>

			<?php
				/**
				 * woocommerce_before_shop_loop hook
				 *
				 * @hooked woocommerce_result_count - 20
				 * @hooked woocommerce_catalog_ordering - 30
				 */
				do_action( 'woocommerce_before_shop_loop' );
			?>

			<?php woocommerce_product_loop_start(); ?>

				<?php woocommerce_product_subcategories(); ?>

				<?php while ( have_posts() ) : the_post(); ?>

					<?php wc_get_template_part( 'content', 'product' ); ?>

				<?php endwhile; // end of the loop. ?>

			<?php woocommerce_product_loop_end(); ?>

			<?php
				/**
				 * woocommerce_after_shop_loop hook
				 *
				 * @hooked woocommerce_pagination - 10
				 */
				do_action( 'woocommerce_after_shop_loop' );
			?>

		<?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>

			<?php wc_get_template( 'loop/no-products-found.php' ); ?>

		<?php endif; ?>

		<?php if ( 'template-full.php' == $shop_template ) : ?>
			</div>
		<?php else : ?>
			</div></div></div>
		<?php endif; ?>

		<?php if ( 'template-full.php' !== $shop_template ) : ?>

			<?php
				/**
				 * woocommerce_sidebar hook
				 *
				 * @hooked woocommerce_get_sidebar - 10
				 */
				do_action( 'woocommerce_sidebar' );
			?>

		<?php endif; ?>

		<?php
			/**
			 * woocommerce_after_main_content hook
			 *
			 * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
			 */
			do_action( 'woocommerce_after_main_content' );
		?>

	<?php endif; ?>

</div>

<?php get_footer( 'shop' ); ?>