2013-08-28 73 views
0

我試圖獲取WooCommerce中指定給BACS支付網關ID的特定product_id值的總和。我有一個產品ID的數組,我使用array_sum來獲得產品值的總和,但這不能正常工作。如果任何人都能帶領我走向更好的方向,那將是非常值得讚賞的。WooCommerce - product_id值的總和

add_action('woocommerce_before_cart_total', 'invoice_price'); 

<?php 
$available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways(); 
$product = new WC_Product(get_the_ID()); 
$invoiceItems = array(522, 550, 523); 
$formatted_total = woocommerce_price($this->order_total); 

function invoice_price($available_gateways) { 
    if ($available_gateways == $gateways['bacs']) { 
    foreach($product == in_array($values['product_id'])) { 
     echo array_sum($invoiceItems); 
    } 
    $invoiceTotal = $formatted_total - $invoiceItems; 
} 
echo $invoiceTotal(); 
} 
?> 
+0

嘗試'回聲$ invoiceTotal;'而不是'回聲$ invoiceTotal(); '因爲'invoiceTotal'不是一個函數。 (我打算把它作爲答案發布,但我沒有足夠的資源來測試你的代碼)。 –

+0

你也可以嘗試'$ invoiceTotal = $ formatted_total - array_sum($ invoiceItems);'然後做'echo $ invoiceTotal;'---這對我來說在一定程度上對我有用。 –

回答

0

考慮以下幾點:

具有100陣列,200和300可能會增加到600

$formatted_total = 5000;

我由$invoiceItems陣列,減去5000。減去$formatted_total 600等於4400.

<?php 

$invoiceItems = array(100, 200, 300); 

$formatted_total = 5000; 

$invoiceTotal = $formatted_total - array_sum($invoiceItems); 

echo $invoiceTotal; // will echo 4400 

?> 

echo $invoiceTotal();會呈現無效,因爲您試圖回顯無效的函數。


如果我使用以下命令:

$invoiceTotal = $formatted_total - $invoiceItems; 

將產生以下錯誤:

Fatal error: Unsupported operand types