【发布时间】:2020-10-06 22:39:34
【问题描述】:
所以我下面的脚本运行良好。它正确计算所有内容,正确通知用户,并且基本上正是我想要发生的事情:
这是一个等级奖励系统,一旦客户进行 XX 数量的购买,就会更新,一旦等级提升,就会弹出一个模式窗口,通过“恭喜!”通知他们他们的进步。
问题是 - 它的工作方式是,如果客户说 5 个订单(一个等级提升金额)并使用不同的浏览器,他们将再次收到此通知(因为解雇是在 Javascript 中完成并存储在曲奇饼)。如果 cookie 不存在或者他们是隐私浏览,他们会一遍又一遍地看到这条消息。
我想要一种更持久、更有效的方法来检测用户是否已经看到了这个进度消息,并且只将它们显示到模态时间。
// -----------------------------------------
// COUNT AMOUNT UNTIL CUSTOMER REACHES NEXT TIER
function amount_until_tier() {
$count = count_orders();
$tier_name = tier_status();
//$amount_until = abs($count - $next_tier);
if ( $count >= 50 ) {
echo 'You\'ve made it! You\'re VIP. Expect random rewards.';
} else {
//echo 'You need <strong>' . $amount_until . '</strong> more orders to reach <strong>' . $tier_name . '</strong>';
}
$the_digits = ['5','15','30','50'];
$showModal = false;
if (in_array($count, $the_digits)) {
if (!isset( $_COOKIE["tier_advance"] )) {
$showModal= true;
} else {
if ($_COOKIE["tier_advance"] != "$count") {
$showModal = true;
} else {
// echo "false because tier_advance !== count
}
}
} else {
// echo "false because not in the_digits";
}
if ($showModal) {
echo '<script>
jQuery(window).on("load",function(){
jQuery("#tier_advance").modal("show");
});
jQuery(document).on("click", "#tier_confirm", function(){
// Set a cookie
Cookies.set("tier_advance", "' . $count . '", { expires: 356 });
});
</script>
<!-- BEGIN Share Modal -->
<div class="modal fade" id="tier_advance" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h5>CONGRATULATIONS!' . $count . '</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body text-center">
<p>You are now a <strong class="all-caps"> ' . $tier_name . ' </strong>tier customer!</p>
<p>Expect some sweet rewards to start coming your way!</p>
</div>
<div class="modal-footer modal-footer-centered">
<button id="tier_confirm" type="button" class="btn btn-secondary" data-dismiss="modal">Awesome, thanks!</button>
</div>
</div>
</div>
</div>
<!-- END Share Modal -->';
}
}
add_action('wp_head', 'amount_until_tier');
【问题讨论】:
标签: javascript php wordpress woocommerce metadata