Woocommerce自定义简码返回零值

我在wordpress中创建了一个自定义的“谢谢”页面,其中包含以下段落:

Thank you so much for your order!

After paying off [cart_total] via XYZ website, please fill out this form and inform us about your payment.

并尝试了这个自定义的简码:

// Total Price Shortcode 

function cart_wctotal(){

   global $woocommerce;
   $wctotal = $woocommerce->cart->get_cart_total();

   return "<span class='cart-total'> ".$wctotal."</span>";

}

add_shortcode( 'cart_total', 'cart_wctotal' );

但是,当我签出订单时,输出将返回零总价格:

非常感谢您的订购!      通过XYZ网站支付€0.00后,请填写此表单,并   通知我们您的付款。
评论
  • hquia
    hquia 回复

    See comment from @xhynk why your code is not going to work.

    通过用户ID获取最后的订单(和订单总数)

    function cart_wctotal(){
        // Get user id
        $user_id = get_current_user_id();
    
        // Get last order by user id
        $last_order = wc_get_customer_last_order( $user_id );
    
        // Order total
        $wctotal = $last_order->get_total();
    
        return "<span class='cart-total'> " . $wctotal . "</span>";
    }
    add_shortcode( 'cart_total', 'cart_wctotal' );