<?php

/**
 * Settings | WooCommerce
 *
 * @package Dotspice
 * @version 1.3.0
 */


if (class_exists('woocommerce')) {

	/**
	 * Include Sripts & Styles
	 */
	function dotspice_woo_scripts()
	{
		// Dequeue Scripts
		// wp_dequeue_script('select2');
		// wp_deregister_script('select2');
		// wp_dequeue_script('selectWoo');
		// wp_deregister_script('selectWoo');

		// Enqueue Scripts
		wp_enqueue_script('woocommerce-script', get_template_directory_uri() . '/js/woocommerce-script.js', array(), filemtime(get_template_directory() . '/js/woocommerce-script.js'), true);
	}
	add_action('wp_enqueue_scripts', 'dotspice_woo_scripts');


	/**
	 * Provide an edit link for WooCommerce Archive.
	 */
	function dotspice_woo_admin_bar_edit_menu($wp_admin_bar)
	{
		if (!is_admin() && is_shop()) {
			$page_ID = wc_get_page_id('shop');

			if (current_user_can('edit_post', $page_ID)) {
				$wp_admin_bar->add_node(
					array(
						'id'    => 'edit',
						'title' => __('Edit Page'),
						'href'  => get_edit_post_link($page_ID),
					)
				);
			}
		}
	}
	add_action('admin_bar_menu', 'dotspice_woo_admin_bar_edit_menu', 80);


	// Disable styles
	add_filter('woocommerce_enqueue_styles', '__return_empty_array');

	// Add theme support
	function dotspice_woo_support()
	{
		add_theme_support('woocommerce', array(
			'thumbnail_image_width'         => 1500,
			'single_image_width'            => 800,
			'gallery_thumbnail_image_width' => 150,
			'cart_item_thumbnail' => 100,
			'product_grid'                  => array(
				'default_rows'    => 4,
				'min_rows'        => 2,
				'max_rows'        => 8,
				'default_columns' => 3,
				'min_columns'     => 1,
				'max_columns'     => 4,
			),
		));

		add_theme_support('wc-product-gallery-lightbox');
		add_theme_support('wc-product-gallery-slider');
	}
	add_action('after_setup_theme', 'dotspice_woo_support', 99);

	// Disable page title
	add_filter('woocommerce_show_page_title', '__return_false');

	// Disable result count
	// remove_action('woocommerce_before_shop_loop', 'woocommerce_result_count', 20);

	// Disable breadcrumb
	remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);

	// Unset ship to different address checked
	add_filter('woocommerce_ship_to_different_address_checked', '__return_false');

	// Update Cart Count on Fragments
	function dotspice_woo_cart_count_fragments($fragments)
	{
		$cart_count = WC()->cart->get_cart_contents_count();
		$fragments['.navbar-btn.mini-cart .count'] = '<span class="count" data-count="' . $cart_count . '">' . $cart_count . '</span>';

		return $fragments;
	}
	add_filter('woocommerce_add_to_cart_fragments', 'dotspice_woo_cart_count_fragments', 10, 1);

	// Plus & minus button
	function dotspice_woo_single_quantity()
	{
?>
		<div class="quantity-buttons">
			<button type="button" class="quantity-buttons__plus plus">+</button>
			<button type="button" class="quantity-buttons__minus minus">-</button>
		</div>
<?php
	}
	add_action('woocommerce_after_add_to_cart_quantity', 'dotspice_woo_single_quantity');

	// Plus & minus button on Cart
	function dotspice_woo_cart_item_quantity($product_quantity, $cart_item_key)
	{
		ob_start();
		dotspice_woo_single_quantity();
		$quantity_buttons = ob_get_clean();

		return $product_quantity . $quantity_buttons;
	}
	add_filter('woocommerce_cart_item_quantity', 'dotspice_woo_cart_item_quantity', 10, 2);

	// Products search form
	function dotspice_woo_product_search_form($form)
	{
		ob_start();
		echo do_shortcode('[fibosearch]');
		$form_html = ob_get_clean();

		return $form_html;
	}
	add_filter('get_product_search_form', 'dotspice_woo_product_search_form');

	//Exclude products from WordPress Search
	function searchFilter($query)
	{
		if ($query->is_search) {
			if (!isset($query->query_vars['post_type'])) {
				$query->set('post_type', array('post', 'page'));
			}
		}
		return $query;
	}
	add_filter('pre_get_posts', 'searchFilter');

	// Transfer product data tabs
	remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10);
	add_action('woocommerce_single_product_summary', 'woocommerce_output_product_data_tabs', 80);

	// Remove product tab headers
	add_filter('woocommerce_product_description_heading', '__return_empty_string');
	add_filter('woocommerce_product_additional_information_heading', '__return_empty_string');

	// Remove sidebar on single product
	remove_action('woocommerce_sidebar', 'woocommerce_get_sidebar', 10);

	// Remove content wrappers
	remove_action('woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
	remove_action('woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);

	// Remove loop product link close
	remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5);

	// Replace Mini cart buttons
	add_action('woocommerce_widget_shopping_cart_buttons', function () {
		// Removing Buttons
		remove_action('woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_button_view_cart', 10);
		remove_action('woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20);

		// Adding customized Buttons
		add_action('woocommerce_widget_shopping_cart_buttons', 'dotspice_woo_widget_shopping_cart_button_view_cart', 10);
		add_action('woocommerce_widget_shopping_cart_buttons', 'dotspice_woo_widget_shopping_cart_proceed_to_checkout', 20);
	}, 1);

	function dotspice_woo_widget_shopping_cart_button_view_cart()
	{
		echo '<a href="' . wc_get_cart_url() . '" class="button wc-forward">' . dotspice_get_svg_icon('shopping-cart-fa-solid', 'html', array('size' => false, 'style' => false)) . esc_html__('View cart', 'woocommerce') . '</a>';
	}
	function dotspice_woo_widget_shopping_cart_proceed_to_checkout()
	{
		echo '<a href="' . wc_get_checkout_url() . '" class="button checkout wc-forward">' . dotspice_get_svg_icon('check-square-fa-solid', 'html', array('size' => false, 'style' => false)) . esc_html__('Checkout', 'woocommerce') . '</a>';
	}

	/**
	 * Change number of related products output
	 */
	function dotspice_woo_related_products_args($args)
	{
		$args['posts_per_page'] = 3;
		$args['columns'] = 3;

		return $args;
	}
	add_filter('woocommerce_output_related_products_args', 'dotspice_woo_related_products_args', 20);

}
