پیشنهاد در همین قیمت؛ نمایش محصولات همقیمت در ووکامرس
این کد با استفاده از شورتکد زیر، تعدادی از محصولات با قیمت نزدیک به محصول فعلی را در صفحه محصول نمایش میدهد:
[same_price_products]
محصولات ابتدا بر اساس دستهبندی محصول فعلی بررسی میشوند و سپس محصولاتی که در محدوده قیمتی مشخص قرار دارند پیدا میشوند. کد از نزدیکترین قیمت شروع کرده و در صورت پیدا نشدن محصول، محدوده جستجو را افزایش میدهد.
بهصورت پیشفرض حداکثر ۴ محصول نمایش داده میشود و محدوده جستجو از ۲ میلیون تومان شروع شده و تا ۵ میلیون تومان افزایش پیدا میکند.
عنوان بخش نیز قابل تغییر است. برای مثال:
[same_price_products title=”محصولات همقیمت”]
همچنین میتوان تعداد محصولات، محدوده قیمت و شناسه محصول را با پارامترهای شورتکد تغییر داد.
<?php
add_shortcode('same_price_products', 'same_price_products_shortcode');
function same_price_products_shortcode($atts) {
$atts = shortcode_atts(array(
'count' => 4,
'product_id' => 0,
'title' => 'پیشنهاد در همین رنج قیمت',
'range' => 2000000,
'max_range' => 5000000
), $atts);
ob_start();
if (!is_product() && !$atts['product_id']) {
return ob_get_clean();
}
global $product;
$target_product_id = $atts['product_id'] ? intval($atts['product_id']) : $product->get_id();
$target_product = wc_get_product($target_product_id);
if (!$target_product) {
return ob_get_clean();
}
$product_price = floatval($target_product->get_price());
if (!$product_price) {
return ob_get_clean();
}
$category_ids = wp_get_post_terms($target_product_id, 'product_cat', array('fields' => 'ids'));
$range = intval($atts['range']);
$max_range = intval($atts['max_range']);
$found_products = array();
for ($current_range = $range; $current_range <= $max_range; $current_range += $range) {
$min_price = $product_price - $current_range;
$max_price = $product_price + $current_range;
if ($min_price < 0) $min_price = 0;
$args = array(
'post_type' => 'product',
'posts_per_page' => intval($atts['count']),
'post__not_in' => array($target_product_id),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category_ids,
'operator' => 'IN'
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_price',
'value' => $min_price,
'type' => 'DECIMAL',
'compare' => '>='
),
array(
'key' => '_price',
'value' => $max_price,
'type' => 'DECIMAL',
'compare' => '<='
)
),
'fields' => 'ids'
);
$query = new WP_Query($args);
$found_products = $query->posts;
if (!empty($found_products)) {
break;
}
}
if (empty($found_products)) {
return ob_get_clean();
}
$products_with_distance = array();
foreach ($found_products as $pid) {
$p = wc_get_product($pid);
$p_price = floatval($p->get_price());
$distance = abs($p_price - $product_price);
$products_with_distance[] = array(
'id' => $pid,
'price' => $p_price,
'distance' => $distance
);
}
usort($products_with_distance, function($a, $b) {
return $a['distance'] - $b['distance'];
});
$products_with_distance = array_slice($products_with_distance, 0, intval($atts['count']));
$shop_url = wc_get_page_permalink('shop');
$view_all_url = add_query_arg(array(
'min_price' => $min_price,
'max_price' => $max_price
), $shop_url);
?>
<style>
.sp-header { display: flex; align-items: center; justify-content: flex-start; gap: 5px; margin-bottom: 3px; direction: rtl; }
.sp-title { margin: 0; }
.sp-wrapper { margin-top: 3px; display: flex; flex-wrap: wrap; gap: 5px; }
.sp-box { flex: 0 0 calc(25% - 3.75px); box-sizing: border-box; border: 1px solid #ddd; border-radius: 10px; padding: 0; min-width: 0; background: #fcfcfc; }
.sp-box-inner { display: flex; align-items: center; gap: 5px; min-width: 0; }
.sp-image { display: block; width: 70px; height: 70px; overflow: hidden; flex-shrink: 0; }
.sp-image img { border-radius: 0 10px 10px 0 !important; background: #ddd; }
.sp-box-inner > div { min-width: 0; overflow: hidden; width: 100%; }
.sp-name { color: #333 !important; text-decoration: none !important; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; max-width: 100%; }
.sp-price { color: #777; font-size: 13px; display: flex; justify-content: space-between; align-items: center; width: 100%; margin-top: 4px; }
.sp-sale-text { color: #e74c3c; font-size: 13px; font-weight: bold; margin-left: 10px; }
@media (max-width: 767px) { .sp-box { flex: 0 0 100%; } }
</style>
<div class="sp-header">
<h4 class="sp-title"><?php echo esc_html($atts['title']); ?></h4>
<a href="<?php echo esc_url($view_all_url); ?>" target="_blank" rel="noopener">(مشاهده همه)</a>
</div>
<div class="sp-wrapper">
<?php foreach ($products_with_distance as $item) :
$sp_product = wc_get_product($item['id']);
$sp_permalink = get_permalink($item['id']);
$has_sale = $sp_product->is_on_sale();
$sale_percent = '';
if ($has_sale) {
$regular_price = (float) $sp_product->get_regular_price();
$sale_price = (float) $sp_product->get_sale_price();
if ($regular_price > 0 && $sale_price >= 0) {
$sale_percent = (($regular_price - $sale_price) / $regular_price) * 100;
$sale_percent = number_format($sale_percent, 2, '.', '');
$sale_percent = rtrim(rtrim($sale_percent, '0'), '.');
}
}
?>
<div class="sp-box">
<div class="sp-box-inner">
<a class="sp-image" href="<?php echo esc_url($sp_permalink); ?>" target="_blank" rel="noopener">
<?php echo $sp_product->get_image('woocommerce_thumbnail', ['style' => 'width:100%;height:100%;object-fit:contain;']); ?>
</a>
<div>
<a class="sp-name" href="<?php echo esc_url($sp_permalink); ?>" target="_blank" rel="noopener" title="<?php echo esc_attr($sp_product->get_name()); ?>">
<?php echo $sp_product->get_name(); ?>
</a>
<div class="sp-price">
<span class="sp-price-value"><?php echo wc_price($sp_product->get_price()); ?></span>
<?php if ($has_sale) : ?>
<span class="sp-sale-text">٪<?php echo $sale_percent; ?>-</span>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php
wp_reset_postdata();
return ob_get_clean();
}