2013-08-28 31 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