<?php
// Remove the default wrapper by woocommerce and add our own wrapper like on single page and post.
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper' );
add_action( 'woocommerce_before_main_content', 'code_manas_before_main_content' );
function code_manas_before_main_content() {
	echo '
	<main id="main" class="site-main" role="main">
		<div id="content" class="site-content">
			<div class="container">
				<div id="primary" class="content-area">
					<div class="primary-inner-wrapper">
	';
}

// Close the last two div from above right after the loop ending for adding the sidebar after it only on shop page.
add_action( 'woocommerce_after_shop_loop', 'code_manas_after_shop_loop' );
function code_manas_after_shop_loop() {
	echo '
		</div>
			</div>
	';
}
// Hook the above funtion on sinlge differently as single prodcut does not have above hook.
add_action( 'woocommerce_after_single_product', 'code_manas_after_shop_loop' );

// Close the manually added remaining wrappers at the end of the page. 
add_action( 'woocommerce_after_main_content', 'code_manas_after_main_content' );
function code_manas_after_main_content() {
	echo '
			</div>
		</div>
	</main>
	';
}

// Remove the breadcrumbs based on theme option
add_action( 'customize_preview_init', 'code_manas_breadcrumb_enable' );
add_action( 'init', 'code_manas_breadcrumb_enable' );
function code_manas_breadcrumb_enable() {
	$woo_breadcrumb_enable = get_theme_mod( 'woo_breadcrumb_enable', 1 );
	if ( ! $woo_breadcrumb_enable ) {
	    remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
	}
};

// Remove extra icon added by WooCommerce since theme adds it by CSS
add_filter( 'woocommerce_pagination_args', 'code_manas_pagination_arg' );
function code_manas_pagination_arg( $args ) {
	$args['prev_text'] = '';
	$args['next_text'] = '';
	return $args;
}

// Move result count after ordering for style's sake.
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
add_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 30 );

remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
add_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 20 );

// Remove sidebar hooked before footer and hook new registerd sidebar(WooCommerce Sidebar) by theme it after the loop.  
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar' );
add_action( 'woocommerce_after_shop_loop', 'code_manas_get_woo_sidebar', 20 );
function code_manas_get_woo_sidebar() {
	get_sidebar();
}

// Hook the above function again as single product does not have above hook
add_action( 'woocommerce_after_single_product', 'code_manas_get_woo_sidebar', 20 );