无法从php的全局变量中获取值


unable to get value from global variable in php

这是我在我的一个php插件文件中的代码。

add_filter('woocommerce_cart_totals_order_total_html','test_func');
function test_func() {
    global $woocommerce, $totalship;
    $cart_subtotal = (float)$woocommerce->cart->subtotal;
    if( $cart_subtotal < 1000 ) {
    $cart_subtotal01 = $woocommerce->cart->get_cart_subtotal();
    $cart_subtotal11 = explode('</span>', $cart_subtotal01);
    $text_tax ='';
    if($cart_subtotal11[1]) {
        $text_tax = $cart_subtotal11[1];
    }
    $allcarttotal = $cart_subtotal+$totalship;  
    $value = '<strong><span class="amount">Rs.&nbsp;' . $allcarttotal . '</span>'.$text_tax.'</strong>';
    $citrus_total_val = $value;
    return $citrus_total_val;
        //return $value;
    }
    else {
    $docart_total = $cart_subtotal - $totalship;
    $citrus_total_val = $docart_total;
    return $citrus_total_val;
        //return $docart_total;
    }
}
global $citrus_total_val;

我试图将$citrus_total_val的值传递给另一个用于支付网关的插件。

这是代码:

  global $citrus_total_val; 
  //Setup URL and signatue etc.
  $currencycode = get_woocommerce_currency();       
  $merchantTxnId = $order_id;
  $orderAmount = $citrus_total_val;

但是这里没有传递值。我做错了什么?

试试这个:

$citrus_total_val = '';
add_filter('woocommerce_cart_totals_order_total_html','test_func');
function test_func() {
    global $woocommerce, $totalship;
    global $citrus_total_val;
    $cart_subtotal = (float)$woocommerce->cart->subtotal;
    if( $cart_subtotal < 1000 ) {
    $cart_subtotal01 = $woocommerce->cart->get_cart_subtotal();
    $cart_subtotal11 = explode('</span>', $cart_subtotal01);
    $text_tax ='';
    if($cart_subtotal11[1]) {
        $text_tax = $cart_subtotal11[1];
    }
    $allcarttotal = $cart_subtotal+$totalship;  
    $value = '<strong><span class="amount">Rs.&nbsp;' . $allcarttotal . '</span>'.$text_tax.'</strong>';
    $citrus_total_val = $value;
    return $citrus_total_val;
        //return $value;
    }
    else {
    $docart_total = $cart_subtotal - $totalship;
    $citrus_total_val = $docart_total;
    return $citrus_total_val;
        //return $docart_total;
    }
}

尝试将其放入函数中,就像您对global $woocommerce, $totalship;所做的那样

add_filter('woocommerce_cart_totals_order_total_html','test_func');
function test_func() {
    global $woocommerce, $totalship;
    global $citrus_total_val;
    $cart_subtotal = (float)$woocommerce->cart->subtotal;
    if( $cart_subtotal < 1000 ) {
    $cart_subtotal01 = $woocommerce->cart->get_cart_subtotal();
    $cart_subtotal11 = explode('</span>', $cart_subtotal01);
    $text_tax ='';
    if($cart_subtotal11[1]) {
        $text_tax = $cart_subtotal11[1];
    }
    $allcarttotal = $cart_subtotal+$totalship;  
    $value = '<strong><span class="amount">Rs.&nbsp;' . $allcarttotal . '</span>'.$text_tax.'</strong>';
    $citrus_total_val = $value;
    return $citrus_total_val;
        //return $value;
    }
    else {
    $docart_total = $cart_subtotal - $totalship;
    $citrus_total_val = $docart_total;
    return $citrus_total_val;
        //return $docart_total;
    }
}