<?php
global $smof_data;
/**
 * social share list
 */
function cms_woo_share() {
    ?>
    <ul class="pull-right">
        <li class="prd-wishlist">
            <?php wp_haswell_add_to_wishlist_custom_btn(); ?>
        </li>
        <li class="entry-share">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false" >
                <i aria-hidden="true" class="social_share"></i>
            </a>
            <ul class="social-menu dropdown-menu dropdown-menu-right" role="menu">
                <li><a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink();?>"><i aria-hidden="true" class="social_facebook"></i></a></li>
                <li><a target="_blank" href="https://twitter.com/home?status=<?php echo esc_url('Check out this article', 'haswell');?>:%20<?php echo esc_url(get_the_title());?>%20-%20<?php the_permalink();?>"><i aria-hidden="true" class="social_twitter"></i></a></li>
                <li><a target="_blank" href="https://plus.google.com/share?url=<?php the_permalink();?>"><i aria-hidden="true" class="social_googleplus"></i></a></li>
            </ul>
        </li>
    </ul>
    <?php
}
add_action('cms_product_social', 'cms_woo_share');


function filter_loop_shop_columns() { 
    // make filter magic happen here... 
    global $smof_data;
    $woo_columns = (isset($_GET['woo-columns'])) ? intval($_GET['woo-columns']) : $smof_data['woo_columns_layout'];
    return $woo_columns; 
}; 
         
// add the filter 
add_filter( 'loop_shop_columns', 'filter_loop_shop_columns', 10, 1 ); 

/**
 * show button Add to wishlist
 * @author Duong Tung
 */
function wp_haswell_add_to_wishlist_custom_btn() {
    if (class_exists( 'YITH_WCWL' )) {
        echo do_shortcode('[yith_wcwl_add_to_wishlist]');
    }
}

/**
 * For product Tab
 */
add_filter( 'woocommerce_product_tabs', 'cms_woo_rename_tabs', 98 );
function cms_woo_rename_tabs( $tabs ) {
    $tabs['reviews']['title'] = esc_html__( 'Ratings', 'haswell' );
    if (!empty($tabs['additional_information'] )) {
        $tabs['additional_information']['title'] = esc_html__( 'Parameters', 'haswell' );
    }

    return $tabs;
}

/**
 * Action show Shop info in single product
 */
add_action('cms_woo_show_shop_info', 'cms_woo_show_shop_info');
function cms_woo_show_shop_info() {
    ?>  
        <div class="cms-shop-support-info grey-dark-bg font-white pt-110-b-80-cont">
            <div class="container">
                <div class="row">
                    <div class="col-md-3 col-sm-6 text-center"><span class="icon icon-basic-globe"></span><h6 class="white-text "><?php esc_html_e('Free Shipping', 'haswell') ?></h6></div>
                    <div class="col-md-3 col-sm-6 text-center"><span class="icon icon-basic-life-buoy"></span><h6 class="white-text "><?php esc_html_e('24/7 Customer Service', 'haswell') ?></h6></div>
                    <div class="col-md-3 col-sm-6 text-center"><span class="icon icon-ecommerce-creditcard"></span><h6 class="white-text "><?php esc_html_e('Payment Options', 'haswell') ?></h6></div>
                    <div class="col-md-3 col-sm-6 text-center"><span class="icon icon-basic-clockwise"></span><h6 class="white-text "><?php esc_html_e('30 Days Returns', 'haswell') ?></h6></div>
                </div>
            </div>
        </div>
    <?php
}

/**
 * Action show clients on single product
 */
add_action('cms_woo_show_clients', 'cms_woo_show_clients');
function cms_woo_show_clients() {
    ?>
    <div class="cms-show-clients-info p-110-cont">
        <div class="container">
            
            <?php echo apply_filters('the_content', '[cms_grid col_xs="1" col_sm="2" col_md="6" col_lg="6" source="size:12|order_by:date|post_type:client" cms_template="cms_grid--clients.php"]'); ?>
            
        </div>
    </div>
    <?php
}

/**
 * Custom Product Filter Price - Default Widget
 */
add_action( 'widgets_init', 'override_woocommerce_widgets', 15 );
function override_woocommerce_widgets() { 
    if ( class_exists( 'WC_Widget_Price_Filter' ) ) {
        if( function_exists('unregister_ef3_widget') ){
            unregister_ef3_widget( 'WC_Widget_Price_Filter' );
        }
        if( function_exists('register_ef3_widget') ){
            include_once( 'widgets/cms-price-filter.php' );
            register_ef3_widget( 'Custom_WC_Widget_Price_Filter' );
        }
    } 
}

 
/**
 * Change number product to show
 */

add_filter( 'loop_shop_per_page', 'haswell_number_shop_loop', 20 );

function haswell_number_shop_loop(){
    global $smof_data;
    $number_product = ( !empty($smof_data['woo_number_page']) ) ? $smof_data['woo_number_page'] : 9;
    return $number_product;
}

add_action( 'wp_ajax_product_remove', 'product_remove' );
add_action( 'wp_ajax_nopriv_product_remove', 'product_remove' );
function product_remove() {
    $cart = WC()->instance()->cart;
    $item_key = $_POST['product_key'];
    if($item_key){
       $result = $cart->remove_cart_item($item_key);
    }
    exit();
}

/* Ajax update cart total number */
add_filter('woocommerce_add_to_cart_fragments', 'haswell_woo_mini_total_cart_fragment');
function haswell_woo_mini_total_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>
    <span class="cart_total"><?php echo ''.$woocommerce->cart->cart_contents_count; ?></span>
    <?php
    $fragments['span.cart_total'] = ob_get_clean();
    return $fragments;
}

/* Ajax update cart item */
add_filter('woocommerce_add_to_cart_fragments', 'haswell_woo_mini_cart_item_fragment');
function haswell_woo_mini_cart_item_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>
    <div class="shopping_cart_dropdown">
        <div class="shopping_cart_dropdown_inner">
            <?php
                $cart_is_empty = sizeof( $woocommerce->cart->get_cart() ) <= 0;
            ?>
            <ul class="cart_list product_list_widget">
            <?php do_action( 'woocommerce_before_mini_cart_contents' ); ?>
            <?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_name(), $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 );
                            $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
                            ?>
                            <li class="<?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>">
                                <div class="pull-left">
                                    <?php
                                        echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
                                            '<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-item_key="%s" data-product_sku="%s">&times;</a>',
                                            esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
                                            esc_html__( 'Remove this item', 'haswell' ),
                                            esc_attr( $product_id ),
                                            esc_attr( $cart_item_key ),
                                            esc_attr( $_product->get_sku() )
                                        ), $cart_item_key );
                                        ?>
                                        <?php if ( ! $_product->is_visible() ) : ?>
                                            <?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . '&nbsp;'; ?>
                                        <?php else : ?>
                                            <a href="<?php echo esc_url( $product_permalink ); ?>">
                                                <?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . '&nbsp;'; ?>
                                            </a>
                                        <?php endif; ?>
                                </div>
                                <div class="product-desc">
                                    <h5><?php echo esc_html($product_name); ?></h5>
                                    <?php echo wc_get_formatted_cart_item_data( $cart_item ); ?>
                                    <?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 ); ?>
                                </div>
                            </li>
                            <?php
                        }
                    }
                ?>
                <?php do_action( 'woocommerce_mini_cart_contents' ); ?>
                <?php else : ?>
                    <li class="empty"><?php esc_html_e( 'No products in the cart.', 'haswell' ); ?></li>
                <?php endif; ?>

            </ul><!-- end product list -->
        </div>

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

            <p class="total"><strong><?php esc_html_e( 'Subtotal', 'haswell' ); ?>:</strong> <?php echo WC()->cart->get_cart_subtotal(); ?></p>

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

            <p class="buttons">
                <?php do_action( 'woocommerce_widget_shopping_cart_buttons' ); ?>
            </p>
        <?php endif; ?>
    </div>
    <?php
    $fragments['div.shopping_cart_dropdown'] = ob_get_clean();
    return $fragments;
}

