{#
* Plugin Name : CustomerRank
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
#}
{% block stylesheet %}
<style>
.next-level { color: #ff0000; font-weight: bold; }
.progress-container {
position: relative;
background: #eee;
border-radius: 10px;
height: 20px;
max-width: 500px;
width:80%;
margin: 10px auto;
overflow: hidden;
border: 1px #000 solid;
}
.progress-bar-fill {
background: linear-gradient(to right, #ee0000, #ff7f7f);
height: 100%;
width: 0;
border-radius: 10px;
transition: width 1.5s ease;
}
.progress-text {
position: absolute;
left: 50%; top: 0;
transform: translateX(-50%);
font-size: 12px;
color: #000;
}
.rank {
position: relative;
cursor: help;
}
.rank span.tooltip {
position: absolute;
bottom: 125%; left: 50%;
transform: translateX(-50%);
background: rgba(0,0,0,0.8);
color: #fff;
padding: 4px 8px;
border-radius: 4px;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity .3s ease;
z-index: 10;
font-weight:normal !important;
}
.rank:hover .tooltip,
.rank:focus .tooltip { opacity: 1; }
.tooltip-percent {
color: #e60000;
font-weight: bold;
}
.next_info .note {
font-size: 10px;
color: #555;
margin-top: 5px;
}
</style>
{% endblock stylesheet %}
{% if NextCustomerRank %}
{# Pre-compute once #}
{% set userTotal = CurrentCondition.total_amount|default(0) %}
{% set cartTotal = get_carts_total_price()|default(0) %}
{% set combinedTotal = userTotal + cartTotal %}
{% set currentStart = CurrentCustomerRank.condAmount|default(0) %}
{# Safer: default nextEnd to 0 if not set #}
{% set nextEnd = NextCustomerRank.condAmount|default(0) %}
{% set nextAmount = nextEnd - combinedTotal %}
{% if nextAmount < 0 %}{% set nextAmount = 0 %}{% endif %}
{# --- SAFE progress calculation (avoid division by zero) --- #}
{% set denom = nextEnd - currentStart %}
{% if denom > 0 %}
{# Normal case: can compute progress safely #}
{% set rawPercent = (((combinedTotal - currentStart) / denom) * 100)|round(0, 'floor') %}
{% else %}
{# No valid "next" threshold (top rank, or misconfigured thresholds) #}
{# Treat as 100% so we never divide by zero and never crash mypage/cart. #}
{% set rawPercent = 100 %}
{% endif %}
{# Clamp progress to 0–100 #}
{% if rawPercent < 0 %}
{% set progressPercent = 0 %}
{% elseif rawPercent > 100 %}
{% set progressPercent = 100 %}
{% else %}
{% set progressPercent = rawPercent %}
{% endif %}
{# total point rate #}
{% set totalPointRate = BaseInfo.basic_point_rate|default(0) + NextCustomerRank.pointRate|default(0) %}
<div id="customer_rank">
<div class="container-fluid">
<div class="col-md-10 col-md-offset-1">
<div class="next_info">
<p>
{% if nextAmount == 0 %}
おめでとうございます!
「<span class="rank {{ NextCustomerRank.name }}" tabindex="0">
{{ NextCustomerRank.name }}
<span class="tooltip">
次回のご注文からポイント率が
<span class="tooltip-percent">{{ totalPointRate }}%</span>に!
</span>
</span>」にランクアップします!
{% else %}
あと <span class="next-level">{{ nextAmount|number_format }}円</span> で
「<span class="rank {{ NextCustomerRank.name }}" tabindex="0">
{{ NextCustomerRank.name }}
<span class="tooltip">
次回のご注文からポイント率が
<span class="tooltip-percent">{{ totalPointRate }}%</span>に!
</span>
</span>」にランクアップ!
{% endif %}
</p>
<div class="progress-container">
<div class="progress-bar-fill"></div>
<span class="progress-text">0%</span>
</div>
<p class="note">※クーポン・ポイント適用前の目安です。ご注文発送後にランクが更新されます。</p>
</div>
</div>
</div>
</div>
{% endif %}
{% block javascript %}
<script>
$(function(){
$("#customer_rank").appendTo("#customer-rank-info");
});
document.addEventListener("DOMContentLoaded", function(){
{% if NextCustomerRank %}
var bar = document.querySelector(".progress-bar-fill"),
text = document.querySelector(".progress-text"),
percent = {{ progressPercent }},
duration = 1500,
stepTime = percent ? duration / percent : duration;
bar.style.transition = 'width ' + duration + 'ms ease';
setTimeout(function(){
bar.style.width = percent + "%";
var current = 0,
timer = setInterval(function(){
text.textContent = (++current) + "%";
if (current >= percent) clearInterval(timer);
}, stepTime);
}, 200);
{% endif %}
});
</script>
{% endblock javascript %}