<?php
/**
 * Related Products
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.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.9.0
 */

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

global $user_id, $labels, $options, $post;
$qty_input = $options->get_option('products-slider-quantity-input');
$ht_slider_related_products = irk_get_head_tag('ht-slider-related-products', ['class' => 'title']);
$ht_slider_related_products_title = irk_get_head_tag('ht-slider-related-products-title', ['class' => 'title clearfix']);
$ht_related_products_title = irk_get_head_tag('ht-related-products-title');

$p_ids = wc_get_product_term_ids($post->ID, 'product_cat');
$p_subcat_id = isset($p_ids[1]) ? absint($p_ids[1]) : absint($p_ids[0]);
foreach ($p_ids as $p_id) {
    $children = get_term_children(absint($p_id), 'product_cat');
    if (empty($children)) {
        $p_subcat_id = $p_id;
    }
}

$upsells_count = $options->get_option('upsells-count');
$args = array(
    'post_type' => 'product',
    'post__not_in' => [$post->ID],
    'posts_per_page' => $upsells_count ? $upsells_count : 10,
    'orderby' => 'rand'
);

$post_in = array_filter(array_map('absint', (array)get_post_meta($post->ID, 'irk_related_ids', true)));
if (is_array($post_in) && count($post_in)) {
    $args['post__in'] = $post_in;
} else {
    $args['tax_query'][0] = [
        'taxonomy' => 'product_cat',
        'field' => 'term_id',
        'terms' => $p_subcat_id,
        'operator' => 'IN'
    ];
}

if ($options->get_option('upsells-hide-outofstock')) {
    $args['meta_key'] = '_stock_status';
    $args['meta_value'] = 'instock';
}

$related_products = new WP_Query($args);

if (!$related_products->found_posts) {
    return false;
}
?>
<?php if ($options->get_option('upsells-carousel')): ?>
    <div class="cross-sells widget-products-slider widget">
        <header class="header">
            <?php echo $ht_slider_related_products_title['open'] . '<span>' . esc_html($labels->get_label('related-title', 'irk')) . '</span>' . $ht_slider_related_products_title['close']; ?>
        </header>
        <div class="slider-inner">
            <div class="owl-carousel">
                <?php while ($related_products->have_posts()) : ?>
                    <?php
                    $related_products->the_post();
                    $product_id = $related_products->post->ID;
                    $product = wc_get_product($product_id);
                    $second_image = irk_second_image($product, 'products-slider');
                    $additional_class = $second_image ? 'has-secondary-image' : '';
                    ?>
                    <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_related_products['open'] . $product->get_name() . $ht_slider_related_products['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 endwhile; ?>
                <?php wp_reset_postdata(); ?>
            </div>
        </div>
    </div>
<?php else: ?>
    <section class="related products">
        <?php echo $ht_related_products_title['open'] . esc_html($labels->get_label('related-title', 'irk')) . $ht_related_products_title['close']; ?>
        <?php woocommerce_product_loop_start(); ?>
        <?php while ($related_products->have_posts()) : ?>
            <?php
            $related_products->the_post();
            $product_id = $related_products->post->ID;
            $post_object = get_post($product_id);
            setup_postdata($GLOBALS['post'] =& $post_object);
            wc_get_template_part('content', 'product');
            ?>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
        <?php woocommerce_product_loop_end(); ?>
    </section>
<?php endif;