<?php
/**
 * Single Product Up-Sells
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/up-sells.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @package     WooCommerce/Templates
 * @version     3.0.0
 */

if (!defined('ABSPATH')) {
    exit;
}

global $options, $labels, $user_id;
$qty_input = $options->get_option('products-slider-quantity-input');
$ht_slider_upsell = irk_get_head_tag('ht-slider-upsell', ['class' => 'title']);
$ht_slider_upsell_title = irk_get_head_tag('ht-slider-upsell-title', ['class' => 'title clearfix']);
$ht_upsell_title = irk_get_head_tag('ht-upsell-title');

if ($options->get_option('upsells-carousel')) {
    if ($upsells) : ?>
        <div class="upsells widget-products-slider widget">
            <header class="header">
                <?php echo $ht_slider_upsell_title['open'] . '<span>' . esc_html($labels->get_label('upsells-title', 'irk')) . '</span>' . $ht_slider_upsell_title['close']; ?>
            </header>
            <div class="slider-inner">
                <div class="owl-carousel">
                    <?php foreach ($upsells as $upsell) : ?>
                        <?php
                        $product_id = $upsell->get_id();
                        $product = wc_get_product($product_id);
                        $second_image = irk_second_image($product, 'products-slider');
                        $additional_class = $second_image ? 'has-secondary-image' : '';
                        if ($options->get_option('upsells-hide-outofstock') && !$product->is_in_stock()) {
                            continue;
                        }
                        ?>
                        <div <?php wc_product_class('item ' . $additional_class, $product_id); ?>>
                            <a href="<?php echo $product->get_permalink(); ?>" title="<?php echo $product->get_name(); ?>">
                                <div class="img-wrapper">
                                    <?php
                                    echo $product->get_image('woocommerce_thumbnail', ['class' => 'img-fluid']);
                                    if ($second_image) {
                                        echo wp_get_attachment_image($second_image, 'woocommerce_thumbnail', false, ['class' => 'secondary-image img-fluid']);
                                    }
                                    ?>
                                </div>
                                <?php echo $ht_slider_upsell['open'] . $product->get_name() . $ht_slider_upsell['close']; ?>
                                <?php echo $product->get_price_html(); ?>
                            </a>
                            <?php if ($options->get_option('products-slider-add2cart')): ?>
                                <?php if (irk_active_quantity_input($product)): ?>
                                    <?php echo $qty_input ? woocommerce_quantity_input(['min_value' => apply_filters('woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product), 'max_value' => apply_filters('woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product),], $product, false) : ''; ?>
                                    <a href="#" class="ajax-add-to-cart add-to-cart button"
                                       data-id="<?php echo $product_id; ?>">
                                        <?php if ($qty_input): ?>
                                            <span class="plus" data-icon="dk-plus"></span>
                                        <?php else: ?>
                                            <span class="button-text"><?php echo esc_html($product->add_to_cart_text()); ?></span>
                                        <?php endif; ?>
                                        <img src="<?php echo IRK_IMG_URI; ?>oval.svg" class="ajax-loader" width="24"
                                             height="24" alt="loader">
                                    </a>
                                <?php else: ?>
                                    <a class="add-to-cart button" href="<?php echo $product->get_permalink(); ?>"
                                       data-id="<?php echo $product_id; ?>"><?php echo esc_html($product->add_to_cart_text()); ?></a>
                                <?php endif; ?>
                            <?php endif; ?>
                            <div class="product-actions">
                                <?php if ($options->get_option('products-slider-quick-view')): ?>
                                    <a href="#" class="quick-view" data-modal="#quick-view-modal"
                                       data-id="<?php echo $product_id; ?>"
                                       title="<?php echo esc_attr($labels->get_label('quick-view')); ?>"></a>
                                <?php endif; ?>
                                <?php if ($options->get_option('products-slider-favorites') && $user_id): ?>
                                    <a href="#"
                                       class="add-to-favorite favorites <?php echo IRK_Favorites_Manager::is_favorite($user_id, $product_id) ? 'active' : ''; ?>"
                                       data-pid="<?php echo $product_id; ?>"
                                       title="<?php echo esc_attr($labels->get_label('add-favorite')); ?>"></a>
                                <?php endif; ?>
                                <?php if ($options->get_option('products-slider-compare')): ?>
                                    <a href="#"
                                       class="compare add-to-compare <?php echo in_array($product_id, irk_compare_list()) ? 'active' : ''; ?>"
                                       data-id="<?php echo $product_id; ?>"
                                       title="<?php echo esc_attr($labels->get_label('add-compare')); ?>"></a>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    <?php
    endif;
    wp_reset_postdata();

    return;
}

if ($upsells) : ?>
    <section class="up-sells upsells products">

        <?php echo $ht_upsell_title['open'] . esc_html($labels->get_label('upsells-title', 'irk')) . $ht_upsell_title['close']; ?>

        <?php woocommerce_product_loop_start(); ?>

        <?php foreach ($upsells as $upsell) : ?>

            <?php
            $post_object = get_post($upsell->get_id());

            setup_postdata($GLOBALS['post'] =& $post_object);

            wc_get_template_part('content', 'product');
            ?>

        <?php endforeach; ?>

        <?php woocommerce_product_loop_end(); ?>

    </section>
<?php
endif;

wp_reset_postdata();
