<?php
/**
 * WooCommerce related functions
 *
 * @package Meridian One
 * @since 1.0
 */

/**
 * Table Of Contents
 *
 * meridian_one_woocommerce_scripts ( Enqueue scripts and styles )
 * meridian_one_woocommerce_after_setup_theme ( Declare support for WooCommerce features )
 * meridian_one_woocommerce_is_cart ( Check if the current page is WooCommerce cart )
 * meridian_one_woocommerce_is_checkout ( Check if the current page is WooCommerce checkout )
 * meridian_one_woocommerce_is_account ( Check if the current page is WooCommerce account )
 * meridian_one_woocommerce_before ( Wrap before content )
 * meridian_one_woocommerce_after ( Wrap after content )
 * meridian_one_woocommerce_posts_per_page ( Change amount of products per page in shop )
 * meridian_one_woocommerce_loop_columns ( Change amount of columns in shop )
 * meridian_one_woocommerce_related_loop_columns ( Change amount of columns in related posts )
 * meridian_one_woocommerce_remove_heading ( Remove heading )
 * meridian_one_woocommerce_wrap_product_before ( Wrap product start )
 * meridian_one_woocommerce_wrap_product_after ( Wrap product end )
 */

// remove woocommerce actions
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper',     10 );
remove_action( 'woocommerce_after_main_content',  'woocommerce_output_content_wrapper_end', 10 );

if ( ! function_exists( 'meridian_one_woocommerce_scripts' ) ) {

	/**
	 * Enqueue scripts and styles
	 *
	 * @since 1.0
	 */
	function meridian_one_woocommerce_scripts() {

		// no WooCommerce no need for CSS
		if ( ! class_exists( 'WooCommerce' ) ) return;

		// theme
		$theme = wp_get_theme();

		// styles
		wp_enqueue_style( 'meridian-one-woocommerce', get_parent_theme_file_uri() . '/css/woocommerce.css', array(), $theme->get( 'Version' ) );

	}

} add_action( 'wp_enqueue_scripts', 'meridian_one_woocommerce_scripts' );

if ( ! function_exists( 'meridian_one_woocommerce_after_setup_theme' ) ) {

	/**
	 * Declare support for WooCommerce features
	 *
	 * @since 1.0
	 */
	function meridian_one_woocommerce_after_setup_theme() {

		// declare woocommerce support
		add_theme_support( 'woocommerce' );

		// woocommerce 3.0 features
		add_theme_support( 'wc-product-gallery-zoom' );
		add_theme_support( 'wc-product-gallery-lightbox' );
		add_theme_support( 'wc-product-gallery-slider' );

	} 

} add_action( 'after_setup_theme', 'meridian_one_woocommerce_after_setup_theme' );

/**
 * Check if the current page is WooCommerce cart
 *
 * @since 1.0
 */
function meridian_one_woocommerce_is_cart() {
	if ( function_exists( 'is_cart' ) ) {
		return is_cart();
	} else{
		return false;
	}
}

/**
 * Check if the current page is WooCommerce checkout
 *
 * @since 1.0
 */
function meridian_one_woocommerce_is_checkout() {
	if ( function_exists( 'is_checkout' ) ) {
		return is_checkout();
	} else{
		return false;
	}
}

/**
 * Check if the current page is WooCommerce account
 *
 * @since 1.0
 */
function meridian_one_woocommerce_is_account() {
	if ( function_exists( 'is_account_page' ) ) {
		return is_account_page();
	} else{
		return false;
	}
}

/**
 * Wrap before content
 *
 * @since 1.0
 */
function meridian_one_woocommerce_before() {
	echo '<div id="content" class="col col-8">';
} add_action( 'woocommerce_before_main_content', 'meridian_one_woocommerce_before', 1 );

/**
 * Wrap after content
 *
 * @since 1.0
 */
function meridian_one_woocommerce_after() {
	echo '</div>';
} add_action( 'woocommerce_after_main_content', 'meridian_one_woocommerce_after', 99999 );

/**
 * Remove WooCommerce breadcrumbs
 *
 * @since 1.0
 */
if ( ! function_exists( 'woocommerce_breadcrumb' ) ) {
	function woocommerce_breadcrumb() {
		return '';
	}
}

/**
 * Change amount of products per page in shop
 *
 * @since 1.0
 */
function meridian_one_woocommerce_posts_per_page( $cols ) {
	$cols = 9;
	return $cols;
} add_filter( 'loop_shop_per_page', 'meridian_one_woocommerce_posts_per_page', 20 );

/**
 * Change amount of columns in shop
 *
 * @since 1.0
 */
function meridian_one_woocommerce_loop_columns() {
	return 3;
} add_filter( 'loop_shop_columns', 'meridian_one_woocommerce_loop_columns');

/**
 * Change amount of columns in related posts
 *
 * @since 1.0
 */
function meridian_one_woocommerce_related_loop_columns( $args ) {
	$args['posts_per_page'] = 9;
	$args['columns'] = 3;
	return $args;
} add_filter( 'woocommerce_output_related_products_args', 'meridian_one_woocommerce_related_loop_columns' );

/**
 * Remove heading
 *
 * @since 1.0
 */
function meridian_one_woocommerce_remove_heading() {
	return false;
} add_filter( 'woocommerce_show_page_title', 'meridian_one_woocommerce_remove_heading' );

/**
 * Wrap product start
 *
 * @since 1.0
 */
function meridian_one_woocommerce_wrap_product_before() {
	echo '<div class="meridian-one-product-wrap">';
} add_action( 'woocommerce_before_shop_loop_item', 'meridian_one_woocommerce_wrap_product_before', 1 );

/**
 * Wrap product end
 *
 * @since 1.0
 */
function meridian_one_woocommerce_wrap_product_after() {
	echo '</div>';
} add_action( 'woocommerce_after_shop_loop_item', 'meridian_one_woocommerce_wrap_product_after', 99999 );