【问题标题】:Detect custom user meta change in Woocommerce?检测 Woocommerce 中的自定义用户元变化?
【发布时间】: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">&times;</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


    【解决方案1】:

    正如您在标题中所说,一种可能性是将层级数据保存在用户配置文件中。 为了不依赖 cookie,您可以向服务器发送一个 ajax request 以表示模式已被解除(为简洁起见,我删除了一些代码,请查看 cmets):

    function amount_until_tier() {
        $current_user = wp_get_current_user();
    
        $count = count_orders();
        $tier_name = tier_status();
    
        // REMOVED ($count >= 50) check
    
        $the_digits = ['5','15','30','50'];
    
        $showModal = false;
    
        if (in_array($count, $the_digits)) {
            $dismissed_tier = $current_user->get('last_dismissed_tier');
        
            if ($dismissed_tier != $tier_name) {
                $showModal = true;
            }
        }
    
        if ($showModal) {
        ?>
        <script>
            jQuery(window).on("load",function(){
                jQuery("#tier_advance").modal("show");
            });
            
            jQuery(document).on("click", "#tier_confirm", function(){    
                <?php $ajax_url = admin_url('admin-ajax.php'); ?>        
                jQuery.ajax("<?= $ajax_url; ?>", {action: 'dismiss_tier_modal'})
                .then(() => jQuery("#tier_advance").modal("hide"));
            });
        </script>
        <!-- MODAL HTML HERE -->
        <?php
        }        
    }
    add_action('wp_head', 'amount_until_tier');
    
    // Create the ajax action to dismiss the modal
    function ajax_dismiss_tier_modal_action()
    {
        $current_user = wp_get_current_user();
        // Save current tier as last shown
        update_user_meta($current_user->ID, 'last_dismissed_tier', tier_status());
    } 
    // Register the ajax action for logged-in users only
    add_action('wp_ajax_dismiss_tier_modal', 'ajax_dismiss_tier_modal_action');
    

    您还可以存储当前等级并在订单完成后对其进行更新,但这需要您更新现有用户。

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多