<?php

// Get count item in cart
function get_count_items() {
	global $woocommerce;
    return (count(WC()->cart->get_cart()) > 0) ? count(WC()->cart->get_cart()) : 0;
}


// Chang text shipping in cart form 
add_filter( 'gettext', 'customize_no_shipping_text', 20, 3 );
function customize_no_shipping_text( $translated_text, $text, $domain ) {
    if ( $translated_text === 'No shipping options were found for %s.' ) {
        $translated_text = esc_html__( 'Enter your adress to view shipping options.', 'solarva' );
    }
    return $translated_text;
}

add_filter( 'gettext', 'customize_button_shipping_text', 25, 3 );
function customize_button_shipping_text( $translated_text, $text, $domain ) {
    if ( $translated_text === 'Enter a different address' ) {
        $translated_text = esc_html__( 'Calculate Shipping', 'solarva' );
    }
    return $translated_text;
}


//Custom products layout on archive page
add_filter( 'loop_shop_columns', 'solarva_loop_shop_columns', 20 ); 
function solarva_loop_shop_columns() {
	$columns = isset($_GET['col']) ? sanitize_text_field($_GET['col']) : solarva()->get_theme_opt('products_columns', 4);
	return $columns;
}
 

// Change number of products that are displayed per page (shop page)
add_filter( 'loop_shop_per_page', 'solarva_loop_shop_per_page', 20 );
function solarva_loop_shop_per_page( $limit ) {
	$limit = solarva()->get_theme_opt('product_per_page', 9);
	return $limit;
}


/* Remove result count & product ordering & item product category..... */
function solarva_cwoocommerce_remove_function() {
	remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10, 0 );
	remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5, 0 );
	remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10, 0 );
	remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10, 0 );
	remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10, 0 );
	remove_action( 'woocommerce_before_shop_loop' , 'woocommerce_catalog_ordering', 30 );
	remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );

	remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 1);
	remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_sale_flash', 15);

	remove_action( 'woocommerce_single_product_summary' , 'woocommerce_template_single_title', 5 );
	remove_action( 'woocommerce_single_product_summary' , 'woocommerce_template_single_rating', 10 );
	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_sharing', 50 );



}
add_action( 'init', 'solarva_cwoocommerce_remove_function' );

/* Product Category */
add_action( 'woocommerce_before_shop_loop', 'solarva_woocommerce_nav_top', 2 );
function solarva_woocommerce_nav_top() { 
	$grid_col = solarva()->get_theme_opt('products_columns1', 3);
	$list_col = solarva()->get_theme_opt('products_columns2', 2);
	?>
	<div class="woocommerce-topbar">
		<div class="woocommerce-topbar-group">
			<div class="woocommerce-layout-types">
				<button class="woocommerce-layout-grid btn-change-layout active" data-layout = "grid <?php echo esc_attr('columns-'.$grid_col); ?>">
					<svg xmlns="http://www.w3.org/2000/svg" width="16" height="13" viewBox="0 0 16 13" fill="none">
						<path fill-rule="evenodd" clip-rule="evenodd" d="M0 -0.00012207H4V2.99988H0V-0.00012207ZM6 -0.00012207H10V2.99988H6V-0.00012207ZM16 -0.00012207H11V2.99988H16V-0.00012207ZM0 4.99988H4V7.99988H0V4.99988ZM10 4.99988H6V7.99988H10V4.99988ZM11 4.99988H16V7.99988H11V4.99988ZM4 9.99988H0V12.9999H4V9.99988ZM6 9.99988H10V12.9999H6V9.99988ZM16 9.99988H11V12.9999H16V9.99988Z" fill="#B0B6BC"/>
					</svg>
				</button>
				<button class="woocommerce-layout-list btn-change-layout" data-layout = "list <?php echo esc_attr('columns-'.$list_col); ?>">
					<svg xmlns="http://www.w3.org/2000/svg" width="15" height="13" viewBox="0 0 15 13" fill="none">
						<path fill-rule="evenodd" clip-rule="evenodd" d="M1.5 2.99988C2.32843 2.99988 3 2.32831 3 1.49988C3 0.671451 2.32843 -0.00012207 1.5 -0.00012207C0.671573 -0.00012207 0 0.671451 0 1.49988C0 2.32831 0.671573 2.99988 1.5 2.99988ZM1.5 7.99988C2.32843 7.99988 3 7.32831 3 6.49988C3 5.67145 2.32843 4.99988 1.5 4.99988C0.671573 4.99988 0 5.67145 0 6.49988C0 7.32831 0.671573 7.99988 1.5 7.99988ZM3 11.4999C3 12.3283 2.32843 12.9999 1.5 12.9999C0.671573 12.9999 0 12.3283 0 11.4999C0 10.6715 0.671573 9.99988 1.5 9.99988C2.32843 9.99988 3 10.6715 3 11.4999ZM15 -0.00012207H4V2.99988H15V-0.00012207ZM4 4.99988H15V7.99988H4V4.99988ZM15 9.99988H4V12.9999H15V9.99988Z" fill="#B0B6BC"/>
					</svg>
				</button>
			</div>
			<?php woocommerce_result_count(); ?>
		</div>
		<div class="woocommerce-topbar-ordering">
			<?php woocommerce_catalog_ordering(); ?>
		</div>
	</div>
<?php }

add_filter( 'woocommerce_price_filter_widget_min_amount', 'solarva_custom_price_filter_min', 10, 1 );
function solarva_custom_price_filter_min( $min ) {
    return 10; 
}

add_filter( 'woocommerce_price_filter_widget_max_amount', 'solarva_custom_price_filter_max', 10, 1 );
function solarva_custom_price_filter_max( $max ) {
    return 220; 
}

function display_new_badge_on_products() {
	global $product;
	$newness_days = 30;
	$created = strtotime( $product->get_date_created() );
	$html = '';
	if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
	   $html = '<span class="new">' . esc_html__( 'New', 'solarva' ) . '</span>';
	}
	return $html;
 }

function display_rating($product) {
	$rating_count = $product->get_rating_count();
	$average_rating = $product->get_average_rating();
	$html = '<div class="woocommerce-rating">';
	if($rating_count == 0) {
		$html .= '<div class="rating-star">';
		for($i=0 ; $i<5; $i++) {
			$html .= '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="16" viewBox="0 0 18 16" fill="none">
						<path d="M16.4443 5.50833L12.1652 4.87668L10.2527 0.950354C9.90997 0.250496 8.91708 0.2416 8.57147 0.950354L6.65891 4.87668L2.3798 5.50833C1.61243 5.62102 1.3049 6.57887 1.86139 7.12749L4.95722 10.182L4.225 14.4968C4.0932 15.2767 4.9045 15.8609 5.584 15.4961L9.41206 13.4588L13.2401 15.4961C13.9196 15.8579 14.7309 15.2767 14.5991 14.4968L13.8669 10.182L16.9627 7.12749C17.5192 6.57887 17.2117 5.62102 16.4443 5.50833ZM12.3585 9.68375L13.0527 13.788L9.41206 11.8515L5.77145 13.788L6.4656 9.68375L3.51914 6.77756L7.59029 6.17853L9.41206 2.442L11.2338 6.17853L15.305 6.77756L12.3585 9.68375Z" fill="#FEC51B"/>
					</svg>';
		}
		$html .= '</div>';
	}else {
		$html .= wc_get_rating_html( $average_rating, $rating_count);
	}

	$html .= '<span class="rating-count">('.esc_html($rating_count).')</span>';
	$html .= '</div>';
	return pxl_print_html($html);
}

add_filter( 'woocommerce_after_shop_loop_item', 'solarva_woocommerce_product' );
function solarva_woocommerce_product() {
	global $product;
	$img_size = solarva()->get_theme_opt('shop_featured_img_size', '500x500');
	$num_words = solarva()->get_theme_opt('number_of_words_excerpt', 25);
	?>
	<div class="woocommerce-product-inner">
		<div class="woocommerce-product-header">
			<a class="woocommerce-product-details" href="<?php the_permalink(); ?>">
				<?php echo woocommerce_get_product_thumbnail($img_size); ?>
			</a>
			<div class="woocommerce-badges">
				<?php pxl_print_html(display_new_badge_on_products($product->get_id())); ?>
				<?php if ($product->is_on_sale()) : ?>
					<span class="onsale"><?php echo esc_html__('Sale', 'solarva'); ?></span>
				<?php endif; ?>
			</div>
		</div>
		<div class="woocommerce-product-content">
			<div class="pxl-content-top">				
				<?php display_rating($product); ?>
				<div class="woocommerce-content-main">
					<div class="woocommerce-content-group">
						<h5 class="woocommerce-product-title">
							<a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a>
						</h5>
						<div class="woocommerce-product--price">
							<?php woocommerce_template_loop_price(); ?>
						</div>
					</div>
					<div class="woocommerce-product-categories">
						<?php the_terms( $product->get_id(), 'product_cat', '', ', ' ); ?>
					</div>
				</div>
				<?php echo do_shortcode('[add_to_cart id="'.esc_attr($product->get_id() ).'"]'); ?>
			</div>
			<div class="woocommerce-short-description">
				<span class="short-descripion-text">
					<?php echo wp_trim_words( get_the_excerpt($product->get_id()), $num_words, $more = null); ?>
				</span>
			</div>
		</div>
	</div>
<?php }

/* Removes the "shop" title on the main shop page */
add_filter('woocommerce_show_page_title', 'solarva_hide_page_title');
function solarva_hide_page_title()
{
    return false;
}

/**
 * Modify image width theme support.
 */

add_action( 'woocommerce_before_single_product_summary', 'solarva_woocommerce_single_summer_start', 0 );
function solarva_woocommerce_single_summer_start() { ?>
	<?php echo '<div class="woocommerce-summary-wrap row">'; ?>
<?php }
add_action( 'woocommerce_after_single_product_summary', 'solarva_woocommerce_single_summer_end', 5 );
function solarva_woocommerce_single_summer_end() { ?>
	<?php echo '</div></div>'; ?>
<?php }


add_action( 'woocommerce_single_product_summary', 'solarva_woocommerce_sg_product_title', 5 );
function solarva_woocommerce_sg_product_title() { 
	global $product; 
	$product_title = solarva()->get_theme_opt( 'product_title', false ); 
	if($product_title ) : ?>
		<div class="woocommerce-sg-product-title">
			<?php woocommerce_template_single_title(); ?>
		</div>
<?php endif; }

add_action( 'woocommerce_single_product_summary', 'solarva_woocommerce_sg_product_rating', 10 );
function solarva_woocommerce_sg_product_rating() { global $product; ?>
	<div class="woocommerce-sg-product-rating">
		<?php woocommerce_template_single_rating(); ?>
	</div>
<?php }

add_action( 'woocommerce_single_product_summary', 'solarva_woocommerce_sg_product_price', 15 );
function solarva_woocommerce_sg_product_price() { ?>
	<div class="woocommerce-sg-product-price">
		<?php woocommerce_template_single_price(); ?>
	</div>
<?php }

add_action( 'woocommerce_single_product_summary', 'solarva_woocommerce_sg_product_excerpt', 20 );
function solarva_woocommerce_sg_product_excerpt() { ?>
	<div class="woocommerce-sg-product-excerpt">
		<?php woocommerce_template_single_excerpt(); ?>
	</div>
<?php }

add_action('woocommerce_before_add_to_cart_quantity', 'add_label_quantity', 25);
function add_label_quantity() {
	?>
		<h6 class="woocomerce-label--quantity"><?php echo esc_html('Quantity', 'solarva'); ?></h6>
	<?php
}


add_action('woocommerce_after_add_to_cart_quantity', 'add_wishlist_compare_buttons', 30);

function add_wishlist_compare_buttons() {
	global $product;
	?>
		<?php if (class_exists('WPCleverWoosw')) : ?>
			<div class="single_add_to_whistlist_button">
				<div class="woocommerce-wishlist">
					<?php echo do_shortcode('[woosw id="'.esc_attr($product->get_id() ).'"]'); ?>
				</div>
			</div>
		<?php endif; ?>
	<?php if (class_exists('WPCleverWoosw')) : ?>
		<div class="single_add_to_compare_button">
			<div class="woocommerce-compare">
				<?php echo do_shortcode('[woosc id="'.esc_attr($product->get_id() ).'"]'); ?>
			</div>
		</div>
	<?php endif; 
}


add_action('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_button_text', 35); 
function custom_add_to_cart_button_text() {
    return __('Add to cart', 'solarva'); 
}



add_action( 'woocommerce_single_product_summary', 'solarva_woocommerce_sg_social_share', 40 );
function solarva_woocommerce_sg_social_share() { 
	$product_social_share = solarva()->get_theme_opt( 'product_social_share', false );

	if($product_social_share) : ?>
		<div class="woocommerce-social-share">
			<span><?php echo esc_html__('Share:', 'solarva'); ?></span>
			<a class="fb-social" title="<?php echo esc_attr__('Facebook', 'solarva'); ?>" target="_blank" href="http://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>">
				<svg xmlns="http://www.w3.org/2000/svg" width="9" height="17" viewBox="0 0 9 17" fill="none">
					<path d="M6.30943 7.68579L6.65544 5.28303H4.48762V3.72124C4.48762 3.06424 4.79126 2.42225 5.76218 2.42225H6.74723V0.376157C6.74723 0.376157 5.85045 0.214722 4.99604 0.214722C3.20953 0.214722 2.04089 1.36729 2.04089 3.45093V5.28303H0.0566711V7.68579H2.04089V13.4975H4.48409V7.68579H6.30943Z" fill="#4AAB3D"/>
				</svg>
			</a>
			<a class="tw-social" title="<?php echo esc_attr__('Twitter', 'solarva'); ?>" target="_blank" href="https://twitter.com/intent/tweet?url=<?php the_permalink(); ?>&text=<?php the_title(); ?>%20">
				<svg xmlns="http://www.w3.org/2000/svg" width="13" height="11" viewBox="0 0 13 11" fill="none">
					<path d="M12.6953 1.52508C12.3184 1.69467 11.9057 1.79588 11.4985 1.86426C11.6911 1.83143 11.9745 1.48679 12.0873 1.34455C12.2606 1.1312 12.4037 0.887763 12.4835 0.625176C12.4945 0.606029 12.5027 0.578676 12.4807 0.562265C12.4559 0.551324 12.4367 0.556794 12.4174 0.567735C11.9662 0.80844 11.4985 0.983498 11.0005 1.10659C10.9592 1.11753 10.929 1.10659 10.9014 1.08197C10.8602 1.0382 10.8217 0.994439 10.7776 0.956145C10.5685 0.778352 10.3429 0.641588 10.0898 0.537647C9.7569 0.403619 9.39372 0.346178 9.03605 0.36806C8.68663 0.389942 8.34546 0.488412 8.03732 0.649794C7.72642 0.811175 7.45128 1.03547 7.23118 1.30353C7.00282 1.58252 6.83499 1.91349 6.7497 2.26361C6.66716 2.60005 6.67266 2.93375 6.72493 3.27566C6.73319 3.3331 6.72768 3.33857 6.67541 3.3331C4.72747 3.0459 3.11519 2.35387 1.80281 0.868616C1.74503 0.802969 1.71477 0.802969 1.66799 0.874087C1.09572 1.72749 1.3736 3.1006 2.08895 3.77622C2.18524 3.86648 2.28154 3.95675 2.38884 4.03881C2.35032 4.04701 1.87434 3.99504 1.44789 3.77622C1.39011 3.74066 1.3626 3.75981 1.35709 3.82545C1.35159 3.91845 1.35985 4.00325 1.3736 4.10172C1.48366 4.9688 2.0862 5.77024 2.91435 6.08206C3.01339 6.12309 3.12069 6.15865 3.228 6.17779C3.04091 6.21882 2.84556 6.24891 2.3063 6.20515C2.23752 6.19147 2.21276 6.22703 2.23752 6.28994C2.64196 7.38952 3.51964 7.71776 4.17445 7.90375C4.2625 7.92017 4.35054 7.92017 4.43858 7.93931C4.43308 7.94752 4.42757 7.94752 4.42207 7.95573C4.20472 8.28669 3.45085 8.53013 3.09868 8.65322C2.46038 8.87478 1.76429 8.97598 1.09022 8.91034C0.982914 8.89393 0.958152 8.89666 0.930638 8.91034C0.900374 8.92948 0.927887 8.9541 0.960903 8.98145C1.09847 9.07172 1.23604 9.15104 1.3791 9.22763C1.80831 9.45466 2.25403 9.63245 2.72175 9.76101C5.13742 10.4229 7.85573 9.93607 9.66885 8.1472C11.0913 6.74126 11.592 4.80195 11.592 2.8599C11.592 2.78331 11.6828 2.74228 11.7351 2.70125C12.1065 2.42499 12.4037 2.09402 12.6816 1.72476C12.7448 1.6427 12.7448 1.56885 12.7448 1.53876C12.7448 1.53329 12.7448 1.52782 12.7448 1.52782C12.7448 1.495 12.7421 1.5032 12.6953 1.52508Z" fill="#4AAB3D"/>
				</svg>
			</a>
			<a class="ins-social" title="<?php echo esc_attr__('Instagram', 'solarva'); ?>" target="_blank" href="http://www.instagram.com/sharer/sharer.php?url=<?php the_permalink(); ?>">
				<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 15 15" fill="none">
					<path d="M10.828 2.54517C10.404 2.54517 10.0649 2.88811 10.0649 3.30849C10.0649 3.73256 10.4077 4.07182 10.828 4.07182C11.2519 4.07182 11.5911 3.72887 11.5911 3.30849C11.5911 2.88811 11.2483 2.54517 10.828 2.54517Z" fill="#4AAB3D"/>
					<path d="M7.42916 3.52612C5.65963 3.52612 4.2182 4.96796 4.2182 6.73799C4.2182 8.50802 5.65963 9.94985 7.42916 9.94985C9.19869 9.94985 10.6401 8.50802 10.6401 6.73799C10.6401 4.96796 9.19869 3.52612 7.42916 3.52612ZM7.42916 8.79565C6.29371 8.79565 5.37208 7.87376 5.37208 6.73799C5.37208 5.60222 6.29371 4.68033 7.42916 4.68033C8.56461 4.68033 9.48624 5.60222 9.48624 6.73799C9.48624 7.87376 8.56092 8.79565 7.42916 8.79565Z" fill="#4AAB3D"/>
					<path d="M9.97625 13.2612H4.77088C2.61058 13.2612 0.852112 11.5022 0.852112 9.34132V4.13448C0.852112 1.97357 2.61058 0.2146 4.77088 0.2146H9.97993C12.1402 0.2146 13.8987 1.97357 13.8987 4.13448V9.345C13.895 11.5022 12.1365 13.2612 9.97625 13.2612ZM4.77088 1.44256C3.2889 1.44256 2.07972 2.64839 2.07972 4.13448V9.345C2.07972 10.8274 3.28521 12.0369 4.77088 12.0369H9.97993C11.4619 12.0369 12.6711 10.8311 12.6711 9.345V4.13448C12.6711 2.65208 11.4656 1.44256 9.97993 1.44256H4.77088Z" fill="#4AAB3D"/>
				</svg>
			</a>
			<a class="youtube-social" title="<?php echo esc_attr__('Youtube', 'solarva'); ?>" target="_blank" href="#">
				<svg xmlns="http://www.w3.org/2000/svg" width="16" height="13" viewBox="0 0 16 13" fill="none">
					<path d="M15.4252 3.92241C15.4252 1.87355 13.932 0.214722 12.0876 0.214722H3.55542C1.71108 0.214722 0.217834 1.87355 0.217834 3.92241V8.33499C0.217834 10.3839 1.71108 12.0427 3.55542 12.0427H12.0876C13.932 12.0427 15.4252 10.3839 15.4252 8.33499V3.92241ZM10.4083 6.46L6.57997 8.56525C6.43192 8.65454 5.92007 8.53705 5.92007 8.34439V4.0258C5.92007 3.83313 6.43192 3.71565 6.5842 3.80963L10.2475 6.02297C10.3998 6.11695 10.5648 6.36601 10.4083 6.46Z" fill="#4AAB3D"/>
				</svg>
			</a>
			<a class="pin-social" title="<?php echo esc_attr__('Pinterest', 'solarva'); ?>" target="_blank" href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&description=<?php the_title(); ?>%20">
				<svg xmlns="http://www.w3.org/2000/svg" width="11" height="14" viewBox="0 0 11 14" fill="none">
					<path fill-rule="evenodd" clip-rule="evenodd" d="M4.11351 8.14468C4.10281 8.17871 4.0921 8.20934 4.08496 8.23656C3.6211 9.96877 3.57115 10.3533 3.09301 11.1599C2.86464 11.541 2.6113 11.9052 2.32585 12.2523C2.29373 12.2931 2.26519 12.3408 2.20096 12.3306C2.1296 12.3135 2.12603 12.2557 2.11532 12.2012C2.04039 11.6737 1.99757 11.1463 2.01542 10.6154C2.04039 9.92453 2.1296 9.68631 3.06446 5.93263C3.07874 5.87478 3.06446 5.82714 3.04305 5.77609C2.81826 5.20096 2.77544 4.61561 2.97169 4.02006C3.3963 2.74048 4.91635 2.64179 5.18397 3.69676C5.3481 4.35017 4.91278 5.20436 4.58094 6.47033C4.30619 7.51169 5.59074 8.25358 6.69331 7.49468C7.70668 6.79363 8.10274 5.10907 8.02781 3.91797C7.88152 1.53917 5.14828 1.02529 3.41414 1.791C1.42666 2.66901 0.973504 5.01719 1.87269 6.09258C1.98687 6.22871 2.07251 6.31379 2.03682 6.44991C1.97973 6.66431 1.92978 6.88211 1.86555 7.09651C1.81916 7.25646 1.68001 7.31431 1.5123 7.24965C1.18046 7.12373 0.905709 6.91955 0.680913 6.6575C-0.0862477 5.75567 -0.303907 3.97582 0.702322 2.46823C1.81916 0.797282 3.89942 0.120056 5.79413 0.324245C8.05993 0.569271 9.49077 2.04624 9.75838 3.72059C9.8797 4.48289 9.79407 6.36483 8.67009 7.69546C7.3784 9.22348 5.28388 9.32557 4.32046 8.3863C4.24553 8.31484 4.18487 8.22976 4.11351 8.14468Z" fill="#4AAB3D"/>
				</svg>
			</a>
		</div>
<?php endif; }

/* Checkout Page*/
add_action('woocommerce_before_order_notes', 'custom_content_before_order_notes', 10);
function custom_content_before_order_notes() {
    echo '<h3 class="wc-block-title">Additional Information</h3>';
}

// Change button text submit checkout
add_filter('woocommerce_order_button_text', 'custom_woocommerce_order_button_text');
function custom_woocommerce_order_button_text() {
    return 'Place Order Now'; 
}


add_filter('gettext', 'custom_coupon_message_text', 20, 3);
function custom_coupon_message_text($translated_text, $text, $domain) {
    if ($text === 'If you have a coupon code, please apply it below.' && $domain === 'woocommerce') {
        $translated_text = 'Have a coupon ?';
    }
    return $translated_text;
}



add_action( 'woocommerce_checkout_before_order_review_heading', 'solarva_checkout_before_order_review_heading_start', 5 );
function solarva_checkout_before_order_review_heading_start() { ?>
	<?php echo '<div class="pxl-order-review-right"><div class="pxl-order-review-inner">'; ?>
<?php }

add_action( 'woocommerce_checkout_after_order_review', 'solarva_checkout_after_order_review_end', 5 );
function solarva_checkout_after_order_review_end() { ?>
	<?php echo '</div></div>'; ?>
<?php }
/* End Checkout Page*/

/* Product Single: Gallery */
add_action( 'woocommerce_before_single_product_summary', 'solarva_woocommerce_single_gallery_start', 0 );
function solarva_woocommerce_single_gallery_start() { 
	global $product;
	echo '<div class="woocommerce-gallery col-xl-6 col-lg-6 col-md-6"><div class="woocommerce-gallery-inner">';
}

add_action( 'woocommerce_before_single_product_summary', 'solarva_woocommerce_single_gallery_end', 30 );
function solarva_woocommerce_single_gallery_end() { 
	global $product;
	echo '</div></div><div class="woocommerce-summary-inner col-xl-6 col-lg-6 col-md-6">';
}


add_action( 'woocommerce_product_thumbnails', 'solarva_woocommerce_custom_badge', 30 );
function solarva_woocommerce_custom_badge() { 
	global $product;
	?>
		<div class="woocommerce-badges">
			<?php pxl_print_html(display_new_badge_on_products($product->get_id())); ?>
			<?php if ($product->is_on_sale()) : ?>
				<span class="onsale"><?php echo esc_html__('Sale', 'solarva'); ?></span>
			<?php endif; ?>
		</div>
	<?php
}

add_filter('woocommerce_review_before_comment_form', 'custom_change_review_title');

function custom_change_review_title() {
    if (is_product()) {
        $product_id = get_the_ID();
        $reviews_count = get_comments_number($product_id);
        $product_title = get_the_title($product_id);
        $new_title = sprintf('%d reviews for %s', $reviews_count, $product_title);

        // Thay thế tiêu đề hiện tại bằng tiêu đề mới
        echo '<h2 class="woocommerce-reviews-title">' . esc_html($new_title) . '</h2>';
    }
}



/* Ajax update cart item */
add_filter('woocommerce_add_to_cart_fragments', 'solarva_woo_mini_cart_item_fragment');
function solarva_woo_mini_cart_item_fragment( $fragments ) {
	global $woocommerce;
	$product_subtitle = solarva()->get_page_opt( 'product_subtitle' );
    ob_start();
    ?>
    <div class="widget_shopping_cart">
    	<div class="widget_shopping_head">
    		<div class="pxl-item--close pxl-close pxl-cursor--cta"></div>
	    	<div class="widget_shopping_title">
	    		<?php echo esc_html__( 'Cart', 'solarva' ); ?> <span class="widget_cart_counter">(<?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count, 'solarva' ), WC()->cart->cart_contents_count ); ?>)</span>
	    	</div>
	    </div>
        <div class="widget_shopping_cart_content">
            <?php
            	$cart_is_empty = sizeof( $woocommerce->cart->get_cart() ) <= 0;
            ?>
            <ul class="cart_list product_list_widget">

			<?php if ( ! WC()->cart->is_empty() ) : ?>

				<?php foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
						$_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
						$product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

						if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {

							$product_name  = apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key );
							$thumbnail     = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
							$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
							?>
							<li>
								<?php if(!empty($thumbnail)) : ?>
									<div class="cart-product-image">
										<a href="<?php echo esc_url( $_product->get_permalink( $cart_item ) ); ?>">
											<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ); ?>
										</a>
									</div>
								<?php endif; ?>
								<div class="cart-product-meta">
									<h3><a href="<?php echo esc_url( $_product->get_permalink( $cart_item ) ); ?>"><?php echo esc_html($product_name); ?></a></h3>
									<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
									<?php
										echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
											'<a href="%s" class="remove_from_cart_button pxl-close" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s"></a>',
											esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
											esc_attr__( 'Remove this item', 'solarva' ),
											esc_attr( $product_id ),
											esc_attr( $cart_item_key ),
											esc_attr( $_product->get_sku() )
										), $cart_item_key );
									?>
								</div>	
							</li>
							<?php
						}
					}
				?>

			<?php else : ?>

				<li class="empty">
					<i class="caseicon-shopping-cart-alt"></i>
					<span><?php esc_html_e( 'Your cart is empty', 'solarva' ); ?></span>
					<a class="btn btn-shop" href="<?php echo get_permalink( wc_get_page_id( 'shop' ) ); ?>"><?php echo esc_html__('Browse Shop', 'solarva'); ?></a>
				</li>

			<?php endif; ?>

			</ul><!-- end product list -->
        </div>
        <?php if ( ! WC()->cart->is_empty() ) : ?>
			<div class="widget_shopping_cart_footer">
				<p class="total"><strong><?php esc_html_e( 'Subtotal', 'solarva' ); ?>:</strong> <?php echo WC()->cart->get_cart_subtotal(); ?></p>

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

				<p class="buttons">
					<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="btn btn-shop wc-forward"><?php esc_html_e( 'View Cart', 'solarva' ); ?></a>
					<a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="btn checkout wc-forward"><?php esc_html_e( 'Checkout', 'solarva' ); ?></a>
				</p>
			</div>
		<?php endif; ?>
    </div>
    <?php
    $fragments['div.widget_shopping_cart'] = ob_get_clean();
    return $fragments;
}

/* Ajax update cart total number */

add_filter( 'woocommerce_add_to_cart_fragments', 'solarva_woocommerce_sidebar_cart_count_number' );
function solarva_woocommerce_sidebar_cart_count_number( $fragments ) {
	ob_start();
	?>
	<span class="widget_cart_counter">(<?php echo sprintf (_n( '%d', '%d', WC()->cart->cart_contents_count, 'solarva' ), WC()->cart->cart_contents_count ); ?>)</span>
	<?php
	
	$fragments['span.widget_cart_counter'] = ob_get_clean();
	
	return $fragments;
}

add_filter( 'woocommerce_add_to_cart_fragments', 'solarva_woocommerce_sidebar_cart_count_number_header' );
function solarva_woocommerce_sidebar_cart_count_number_header( $fragments ) {
	ob_start();
	?>
	<span class="widget_cart_counter_header"><?php echo sprintf (_n( '%d', '%d', WC()->cart->cart_contents_count, 'solarva' ), WC()->cart->cart_contents_count ); ?></span>
	<?php
	
	$fragments['span.widget_cart_counter_header'] = ob_get_clean();
	
	return $fragments;
}

add_filter( 'woocommerce_add_to_cart_fragments', 'solarva_woocommerce_sidebar_cart_count_number_sidebar' );
function solarva_woocommerce_sidebar_cart_count_number_sidebar( $fragments ) {
	ob_start();
	?>
	<span class="ct-cart-count-sidebar"><?php echo sprintf (_n( '%d', '%d', WC()->cart->cart_contents_count, 'solarva' ), WC()->cart->cart_contents_count ); ?></span>
	<?php
	
	$fragments['span.ct-cart-count-sidebar'] = ob_get_clean();
	
	return $fragments;
}

add_filter( 'woocommerce_output_related_products_args', 'solarva_related_products_args', 20 );
  function solarva_related_products_args( $args ) {
	$args['posts_per_page'] = 4;
	$args['columns'] = 4;
	return $args;
}

/* Pagination Args */
function solarva_filter_woocommerce_pagination_args( $array ) { 
	$array['end_size'] = 1;
	$array['mid_size'] = 1;
    return $array; 
}; 
add_filter( 'woocommerce_pagination_args', 'solarva_filter_woocommerce_pagination_args', 10, 1 ); 

/* Flex Slider Arrow */
add_filter( 'woocommerce_single_product_carousel_options', 'solarva_update_woo_flexslider_options' );
function solarva_update_woo_flexslider_options( $options ) {
$options['directionNav'] = true;
	return $options;
}

/* Single Thumbnail Size */
$single_img_size = solarva()->get_theme_opt('single_img_size');
if(!empty($single_img_size['width']) && !empty($single_img_size['height'])) {
	add_filter('woocommerce_get_image_size_single', function ($size) {
		$single_img_size = solarva()->get_theme_opt('single_img_size');
		$single_img_size_width = preg_replace('/[^0-9]/', '', $single_img_size['width'] ?? '');
		$single_img_size_height = preg_replace('/[^0-9]/', '', $single_img_size['height'] ?? '');
		$size['width'] = $single_img_size_width;
	    $size['height'] = $single_img_size_height;
	    $size['crop'] = 1;
	    return $size;
	});
}
add_filter('woocommerce_get_image_size_gallery_thumbnail', function ($size) {
    $size['width'] = 300;
    $size['height'] = 300;
    $size['crop'] = 1;
    return $size;
});

add_filter('woocommerce_get_image_size_thumbnail', function ($size) {
    $size['width'] = 600;
    $size['height'] = 506;
    $size['crop'] = 1;
    return $size;
});

// paginate links
add_filter('woocommerce_pagination_args', 'solarva_woocommerce_pagination_args');
function solarva_woocommerce_pagination_args($default){
	$default = array_merge($default, [
		'prev_text' => '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="19" viewBox="0 0 18 19" fill="none">
							<path d="M14.9941 9.80566H3.00586C2.79492 9.79395 2.61621 9.71777 2.46973 9.57715C2.32324 9.43652 2.25 9.26074 2.25 9.0498C2.25 8.83887 2.32324 8.66309 2.46973 8.52246C2.61621 8.38184 2.79492 8.30566 3.00586 8.29395H14.9941C15.2051 8.30566 15.3838 8.38184 15.5303 8.52246C15.6768 8.66309 15.75 8.83887 15.75 9.0498C15.75 9.26074 15.6768 9.43652 15.5303 9.57715C15.3838 9.71777 15.2051 9.79395 14.9941 9.80566ZM8.0332 13.0225C8.17383 13.1748 8.24414 13.3506 8.24414 13.5498C8.24414 13.749 8.16797 13.9219 8.01562 14.0684C7.86328 14.2148 7.69043 14.2881 7.49707 14.2881C7.30371 14.2881 7.13086 14.2178 6.97852 14.0771L2.47852 9.57715C2.32617 9.4248 2.25 9.24902 2.25 9.0498C2.25 8.85059 2.32617 8.6748 2.47852 8.52246L6.97852 4.02246C7.13086 3.88184 7.30371 3.81152 7.49707 3.81152C7.69043 3.81152 7.86328 3.88477 8.01562 4.03125C8.16797 4.17773 8.24414 4.35059 8.24414 4.5498C8.24414 4.74902 8.17383 4.9248 8.0332 5.07715L4.06055 9.0498L8.0332 13.0225Z" fill="#5C6972"/>
						</svg>',
		'next_text' => '<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 19 19" fill="none">
							<path d="M3.50586 9.80566H15.4941C15.7051 9.79395 15.8838 9.71777 16.0303 9.57715C16.1768 9.43652 16.25 9.26074 16.25 9.0498C16.25 8.83887 16.1768 8.66309 16.0303 8.52246C15.8838 8.38184 15.7051 8.30566 15.4941 8.29395H3.50586C3.29492 8.30566 3.11621 8.38184 2.96973 8.52246C2.82324 8.66309 2.75 8.83887 2.75 9.0498C2.75 9.26074 2.82324 9.43652 2.96973 9.57715C3.11621 9.71777 3.29492 9.79395 3.50586 9.80566ZM10.4668 13.0225C10.3262 13.1748 10.2559 13.3506 10.2559 13.5498C10.2559 13.749 10.332 13.9219 10.4844 14.0684C10.6367 14.2148 10.8096 14.2881 11.0029 14.2881C11.1963 14.2881 11.3691 14.2178 11.5215 14.0771L16.0215 9.57715C16.1738 9.4248 16.25 9.24902 16.25 9.0498C16.25 8.85059 16.1738 8.6748 16.0215 8.52246L11.5215 4.02246C11.3691 3.88184 11.1963 3.81152 11.0029 3.81152C10.8096 3.81152 10.6367 3.88477 10.4844 4.03125C10.332 4.17773 10.2559 4.35059 10.2559 4.5498C10.2559 4.74902 10.3262 4.9248 10.4668 5.07715L14.4395 9.0498L10.4668 13.0225Z" fill="#5C6972"/>
						</svg>',
		'type'      => 'plain',
	]);
	return $default;
}

// cart link in archive product
add_filter('woocommerce_loop_add_to_cart_link', 'solarva_woocommerce_loop_add_to_cart_link', 10, 3);
function solarva_woocommerce_loop_add_to_cart_link($button, $product, $args){
	return sprintf(
		'<a href="%s" data-quantity="%s" class="%s" %s><span class="pxl-btn-text">%s</span></a>',
		esc_url( $product->add_to_cart_url() ),
		esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
		esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
		isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
		esc_html( $product->add_to_cart_text() ),
		esc_html( $product->add_to_cart_text() ),
		'<i class="flaticon-shopping-bag pxl-mr-10"></i><span class="btn-cart-overlay"></span><span class="pxl-cart-loader"></span>'
	);
}