Untitled

 avatar
unknown
plain_text
a month ago
6.4 kB
6
Indexable

if (! defined('ABSPATH')) {
	exit;
}
global $product;
global $post;
$current_product_title = get_the_title($post->ID);

$terms = wp_get_post_terms($post->ID, 'pa_brend');
$current_product_brand = isset($terms[0]) ? $terms[0]->term_id : '';

$product_categories = wp_get_post_terms($post->ID, 'product_cat');
$current_category = isset($product_categories[0]) ? $product_categories[0] : '';

if ($current_category) {
	$parent_category = $current_category->parent ? get_term($current_category->parent, 'product_cat') : $current_category;

	$main_category_slug = $parent_category->slug;

	if ($main_category_slug === 'zlato') {

		$args = array(
			'post_type' => 'product',
			'posts_per_page' => -1,
			'post__not_in' => array($post->ID),
			'tax_query' => array(
				array(
					'taxonomy' => 'product_cat',
					'field' => 'slug',
					'terms' => $main_category_slug,
					'operator' => 'IN',
				),
			),
		);
	} else {
		$args = array(
			'post_type' => 'product',
			'posts_per_page' => -1,
			'post__not_in' => array($post->ID),
			'tax_query' => array(
				array(
					'taxonomy' => 'pa_brend',
					'field' => 'id',
					'terms' => $current_product_brand,
					'operator' => 'IN',
				),
			),
		);
	}
}

$query = new WP_Query($args);


function get_title_similarity($title1, $title2)
{
	$length = min(strlen($title1), strlen($title2));
	$similar_chars = 0;

	// Poređenje karakter po karakter
	for ($i = 0; $i < $length; $i++) {
		if ($title1[$i] === $title2[$i]) {
			$similar_chars++;
		} else {
			break;
		}
	}

	return $similar_chars;
}

$similar_products = [];

if ($query->have_posts()) {
	while ($query->have_posts()) {
		$query->the_post();
		$product_title = get_the_title();
		$permalink = get_permalink();
		$price = get_post_meta(get_the_ID(), '_price', true);
		$thumbnail = get_the_post_thumbnail(null, 'thumbnail');
		$product = wc_get_product(get_the_ID());
		$attribute = '';

		$terms = wp_get_post_terms($post->ID, 'pa_brend');
		if (!empty($terms)) {
			$attribute = $terms[0]->name;
		}

		$similarity = get_title_similarity($current_product_title, $product_title);

		$similar_products[] = [
			'title' => $product_title,
			'similarity' => $similarity,
			'permalink' => $permalink,
			'price' => $price,
			'thumbnail' => $thumbnail,
			'attribute' => $attribute,
			'post_id' => get_the_ID(),
		];
	}
	wp_reset_postdata();
}

usort($similar_products, function ($a, $b) {
	return $b['similarity'] - $a['similarity'];
});

$top_similar_products = array_slice($similar_products, 0, 7);


if ($related_products) : ?>

	<section class="related products relative">

		<?php
		$heading = apply_filters('woocommerce_product_related_products_heading', __('Slični proizvodi', 'woocommerce'));

		if ($heading) :
		?>
			<h2 class="related-title--underlined"><?php echo esc_html($heading); ?></h2>
		<?php endif; ?>

		<?php //  woocommerce_product_loop_start(); 
		?>

		<?php // foreach ($related_products as $related_product) : 
		?>

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

		//	setup_postdata($GLOBALS['post'] = &$post_object); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found

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

		<?php //  endforeach; 

		?>

		<?php // woocommerce_product_loop_end(); 
		?>
		<?php woocommerce_product_loop_start(); ?>

		<?php
		if (!empty($top_similar_products)) {

			foreach ($top_similar_products as $product) {
				$post_id = $product['post_id'];

				$product_obj = wc_get_product($post_id);

				if (has_term('satovi', 'product_cat', $post_id)) {
					$attribute = $product_obj ? $product_obj->get_attribute('pa_brend') : '';
				} elseif (has_term('nakit', 'product_cat', $post_id)) {
					$attribute = $product_obj ? $product_obj->get_attribute('pa_brend') . '' : '';
				} elseif (has_term('zlato', 'product_cat', $post_id)) {
					$post_title = get_the_title($post_id);
					$attribute = preg_replace('/^\S+\s/', '', $post_title);
				} else {
					$attribute = '';
				}

		?>
				<li class="bordered-item product-item">
					<a href="<?php echo esc_url($product['permalink']); ?>" class="product-link">
						<?php echo $product['thumbnail']; ?>
					</a>
					<div class="product-item-content product-item-content-shop p-[10px] lg:p-[17px]">
						<div class="holder-product-height">
							<?php if ($attribute): ?>
								<a href="<?php echo esc_url($product['permalink']); ?>">
									<span class="product-brand block mt-[7px] mb-[10px] fz-20 font-bold color-dark-main uppercase"><?php echo esc_html($attribute); ?></span>
								</a>
							<?php else: ?>
								<span class="product-brand block mt-[7px] mb-[10px] fz-20 font-bold color-dark-main uppercase">No Brand</span>
							<?php endif; ?>

							<a href="<?php echo esc_url($product['permalink']); ?>">
								<h2 class="product-title fz-15 color-gray-main mb-[16px]"><?php echo esc_html($product['title']); ?></h2>
							</a>

							<?php if ($product['price'] > 0): ?>
								<span class="product-price fz-18 font-bold block mb-[17px]"><?php echo wc_price($product['price']); ?></span>
							<?php else: ?>
								<span class="product-price fz-18 font-bold block mb-[17px]">
									<?php echo $product['price']; // Display the alternative price or attribute 
									?>
								</span>
							<?php endif; ?>
						</div>
						<?php
						if ($product['price'] > 0) {
							$add_to_cart_url = wc_get_cart_url() . '?add-to-cart=' . $product['post_id'];
							echo '<a value="' . $product['post_id'] . '" href="' . esc_url($add_to_cart_url) . '" class="add-to-cart button block filled-btn single_add_to_cart_button">Dodaj u korpu</a>';
						} else {
							echo do_shortcode('[gmwqp_enquiry_single_product id="' . $product['post_id'] . '"]');
						}
						?>
					</div>
				</li>
		<?php
			}
		}
		?>


		<?php woocommerce_product_loop_end(); ?>

		<div class="cstm-arrows  lg:hidden left-[20px] right-[20px] absolute top-[40%] translate-y-[-50%] w-[full] flex items-center justify-between">
			<div class="arrow-left">
				<img class="w-[24px]" src="<?php echo get_template_directory_uri(); ?>/assets/icons/icons8-back-24.png" alt="">
			</div>
			<div class="arrow-right">
				<img class="w-[24px]" src="<?php echo get_template_directory_uri(); ?>/assets/icons/icons8-forward-24.png" alt="">
			</div>
		</div>
	</section>
<?php
endif;

wp_reset_postdata();

?>
<?php
Leave a Comment