Php 避免在Woocommerce中的update cart totals ajax事件中清除自定义字段值

Php 避免在Woocommerce中的update cart totals ajax事件中清除自定义字段值,php,ajax,wordpress,woocommerce,cart,Php,Ajax,Wordpress,Woocommerce,Cart,我在WooCommerce cart-totals.php模板中遇到一个自定义字段的输出问题 我已经在cart-totals.php中的标记中添加了自定义的标记,该标记是显示小计、税款和用于计算运费的字段的元素 我的函数只是检查存储在自定义字段中的一系列值并回显文本,我遇到的问题是,当我更改发货选项时,比如从空运到地面,购物车通过ajax更新,并将自定义字段值更改为空。在更新购物车数量、更新总计或页面加载时不会发生这种情况,只是在更改装运选项时才会发生这种情况 如果您有任何关于修复此问题的建议,

我在WooCommerce cart-totals.php模板中遇到一个自定义字段的输出问题

我已经在cart-totals.php中的
标记中添加了自定义的
标记,该标记是显示小计、税款和用于计算运费的字段的元素

我的函数只是检查存储在自定义字段中的一系列值并回显文本,我遇到的问题是,当我更改发货选项时,比如从空运到地面,购物车通过ajax更新,并将自定义字段值更改为空。在更新购物车数量、更新总计或页面加载时不会发生这种情况,只是在更改装运选项时才会发生这种情况

如果您有任何关于修复此问题的建议,我们将不胜感激

<table cellspacing="0" class="shop_table shop_table_responsive">

  <tr class="cart-tier-discount">

    <th>Rewards Discount</th>

    <td>

      <?php
        $progress = get_the_author_meta( 'tier_progress_value', $user->ID );

        if ( $progress > 0.01 && $progress < 150 ) {
          echo '0%';
        }
        if ( $progress >= 150 && $progress < 300 ) {
          echo '10%';
        }
        if ( $progress >= 300 && $progress < 500 ) {
          echo '15%';
        }
        if ( $progress >= 500 ) {
          echo '20%';

        } 
      ?>

    </td>

  </tr> 

</table>

奖励折扣
尝试以下操作(不编辑
cart\u totals.php
模板)。因此,您需要先从
cart\u totals.php
模板文件中临时删除更改

守则:

add_action('woocommerce_cart_totals_before_shipping', 'cart_totals_rewards_before_shipping'  );
function cart_totals_rewards_before_shipping() {
    // Only for logged in users
    if ( ! is_user_logged_in() ) return;

    $progress = (int) get_user_meta( get_current_user_id(), 'tier_progress_value', true );

    if ( $progress >= 0 && $progress < 150 )
        $percentage = '0%';
    elseif ( $progress >= 150 && $progress < 300 )
        $percentage = '10%';
    elseif ( $progress >= 300 && $progress < 500 )
        $percentage = '15%';
    else
        $percentage = '20%';

    echo  '<tr class="cart-tier-discount">
    <th>' . __("Rewards Discount", "") . '</th>
    <td>' . $percentage .'</td>
    </tr>';
}
add_action('woocommerce_cart_totals_前_发货','cart_totals_奖励_前_发货');
功能车\u总计\u奖励\u发货前(){
//仅适用于已登录的用户
如果(!is_user_logged_in())返回;
$progress=(int)get_user_meta(get_current_user_id(),'tier_progress_value',true);
如果($progress>=0&$progress<150)
$percentage='0%';
elseif($progress>=150&$progress<300)
$percentage='10%';
其他($progress>=300&$progress<500)
$percentage='15%';
其他的
$percentage='20%';
回声'
“奖励折扣”
“.$百分比。”
';
}
代码进入活动子主题(或活动主题)的function.php文件。它应该有效

然后,在更改cart totals update事件的装运方法时,该值仍然存在:


当然是完美的!非常有趣的是,通过add_操作而不是直接在模板中添加标记可以防止出现问题。