<?php
/**
 * Custom functons for WooCommerce
 *
 *
 * @package somalite
 */


/*
* Add cart menu
*
**/

if ( ! function_exists( 'somalite_wcmenucart' ) ) :
function somalite_wcmenucart($menu, $args) {

	// Check if WooCommerce is active
	if ( !class_exists( 'woocommerce' ) || 'primary' !== $args->theme_location )
		return $menu;

	ob_start();
	global $woocommerce;
	$viewing_cart = esc_html__('View your shopping cart', 'somalite');
	$start_shopping = esc_html__('Start shopping', 'somalite');
	$cart_url = wc_get_cart_url();
	$shop_page_url = esc_url(get_permalink( wc_get_page_id ( 'shop' )));
	$cart_contents_count = $woocommerce->cart->cart_contents_count;
	//$cart_contents = sprintf(_n('%d item', '%d items', $cart_contents_count, 'somalite'), $cart_contents_count);
	$cart_total = $woocommerce->cart->get_cart_total();
	// Uncomment the line below to hide nav menu cart item when there are no items in the cart
	// if ( $cart_contents_count > 0 ) {
	if ($cart_contents_count == 0) {
		$menu_item = '<li class="right wc-cart-menu"><a class="wcmenucart-contents" href="'. $shop_page_url .'" title="'. $start_shopping .'">';
	} 
	else{
		$menu_item = '<li class="right wc-cart-menu"><a class="wcmenucart-contents" href="'. $cart_url .'" title="'. $viewing_cart .'">';
	}
	$menu_item .= '<i class="fas fa-shopping-cart"></i><span class="badge">';
	//$menu_item .= $cart_contents.' - '. $cart_total;
	$menu_item .= $cart_contents_count;
	$menu_item .= '</span></a></li>';
	// Uncomment the line below to hide nav menu cart item when there are no items in the cart
	// }
	echo $menu_item;
	$social = ob_get_clean();
	return $menu . $social;

}
endif;

if ( ! function_exists( 'somalite_showwcmenucart_data' ) ) :
function somalite_showwcmenucart_data($menu, $args) {

	// Check if WooCommerce is active
	if ( !class_exists( 'woocommerce' ) || 'primary' !== $args->theme_location )
		return $menu;

	ob_start();	
	$menu_item = '	<div class="menu-view-cart-products">
						<div class="block-subtitle">								
							<div class="menu-widget-cart">'.									
								the_widget( 'WC_Widget_Cart') .'
							</div>
						</div>
					</div>
					
				';	
	echo $menu_item;
	$social = ob_get_clean();
	return $menu . $social;
}
endif;


if ( ! function_exists( 'somalite_filter_menu_cart_hook' ) ) :
function somalite_filter_menu_cart_hook() {
	if(true===get_theme_mod('sm_menu_cart',false)) {
	 	add_filter('wp_nav_menu_items','somalite_wcmenucart', 10, 2);
	 	add_filter('wp_nav_menu_items','somalite_showwcmenucart_data', 10, 2);
	 }
}
endif;
add_action( 'wp', 'somalite_filter_menu_cart_hook' );

/*
* Ensure cart contents update when products are added to the cart via AJAX
*
**/
if ( ! function_exists( 'somalite_wc_header_add_to_cart_fragment' ) ) :
function somalite_wc_header_add_to_cart_fragment( $fragments ) {
	ob_start();
	global $woocommerce;
	?>
		<a class="wcmenucart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php esc_attr_e( 'View your shopping cart','somalite' ); ?>"><i class="fas fa-shopping-cart"></i><span class="badge"><?php echo $woocommerce->cart->get_cart_contents_count(); ?></span></a>
	<?php	
	$fragments['a.wcmenucart-contents'] = ob_get_clean();	
	return $fragments;
}
endif;
add_filter( 'woocommerce_add_to_cart_fragments', 'somalite_wc_header_add_to_cart_fragment' );


/*
* Replace the image filename/path with your own :)
*
**/
if ( ! function_exists( 'somalite_custom_fix_thumbnail' ) ) :
function somalite_custom_fix_thumbnail() {
  add_filter('woocommerce_placeholder_img_src', 'somalite_woocommerce_placeholder_img_src');   
	function somalite_woocommerce_placeholder_img_src( $src ) {		
		$uploads = get_template_directory_uri();
		$src = $uploads . '/img/woo-no-image.jpg';		 
		return $src;
	}
}
endif;
add_action( 'init', 'somalite_custom_fix_thumbnail' );


/**
 * Number of products per page
*/

if ( ! function_exists( 'somalite_number_items_page' ) ) :
function somalite_number_items_page($cols) {
  	$number_product_per_page = intval( get_theme_mod('sm_shop_ipp','9') );
  	if( $number_product_per_page ){
  		$cols = $number_product_per_page;
  	}
  	else{
  		$cols=9;
  	}
  	return $cols;	
}
endif;
add_filter( 'loop_shop_per_page', 'somalite_number_items_page',20 );


/**
 * Function to disable Woo Wizard
 */

function somalite_disable_woo_wizard() {
    return false;
}
add_filter('woocommerce_enable_setup_wizard', 'somalite_disable_woo_wizard');


/**
 * WooCommerce pages delete default
 */
function somalite_woocommerce_default_pages() {
    $page_slugs = ['shop', 'cart', 'checkout', 'my-account'];
    foreach ($page_slugs as $slug) {
        $page = get_page_by_path($slug);
        if ($page) {
            wp_delete_post($page->ID, true);
        }
    }
}
add_action('activate_woocommerce/woocommerce.php', 'somalite_woocommerce_default_pages', 20);


/**
 * WooCommerce pages
 */
function somalite_assign_woocommerce_pages() {
    // Check if WooCommerce is active
    if (class_exists('WooCommerce')) {
        // Get pages by slug
        $shop_page = get_page_by_path('shop');
        $cart_page = get_page_by_path('cart');
        $checkout_page = get_page_by_path('checkout');
        $myaccount_page = get_page_by_path('my-account');
        if ($shop_page) {
            update_option('woocommerce_shop_page_id', $shop_page->ID);
        }
        if ($cart_page) {
            update_option('woocommerce_cart_page_id', $cart_page->ID);
        }
        if ($checkout_page) {
            update_option('woocommerce_checkout_page_id', $checkout_page->ID);
        }
        if ($myaccount_page) {
            update_option('woocommerce_myaccount_page_id', $myaccount_page->ID);
        }
    }
}
add_action('after_setup_theme', 'somalite_assign_woocommerce_pages');


/**
 * Number of products per row
*/

if (!function_exists('somalite_loop_columns')) :
    function somalite_loop_columns() {
        $product_num_per_row =3;        
        if( $product_num_per_row ){
            $number = $product_num_per_row;
        } else {
            $number = 3;
        }
        return $number;
    }
endif;
add_filter('loop_shop_columns', 'somalite_loop_columns');


/**
 * Related Products
*/
if (!function_exists('somalite_filter_woocommerce_output_related_products_args')) :
function somalite_filter_woocommerce_output_related_products_args( $args ) {     
    $args=array(    
    'posts_per_page' => 3,
    'columns' => 3,
    );

    return $args; 
};
endif;
add_filter( 'woocommerce_output_related_products_args', 'somalite_filter_woocommerce_output_related_products_args', 10, 1 ); 


/**
 * Check woocommece is active
*/

if ( ! function_exists( 'somalite_is_woocommerce_activated' ) ) :
	function somalite_is_woocommerce_activated() {
		if ( class_exists( 'woocommerce' ) ) { return true; } else { return false; }
	}
endif;


/**
 * Check class and return
*/

if ( ! function_exists( 'somalite_check_class' ) ) :
	function somalite_check_class($class) {
		$body_classes = get_body_class();
		if(in_array($class, $body_classes)){
			return true; 
		} 
		else { 
			return false; 
		}
	}
endif;
