Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this in my functions.php that creates a shopping cart widget with the plugin Marketpress.

<?php function marketpress_get_cart_subtotal($format_currency=true) {
    global $mp;

    $selected_cart = $mp->get_cart_contents(true);
    if (!$selected_cart) return;

    foreach ($selected_cart as $bid => $cart) {

        if (is_multisite())
            switch_to_blog($bid);

        foreach ($cart as $product_id => $variations) {
            foreach ($variations as $variation => $data) {
                $totals[] = $mp->before_tax_price($data['price'], $product_id) * $data['quantity'];
            }
        }

        //go back to original blog
        if (is_multisite())
            switch_to_blog($current_blog_id);

    }
    $total = array_sum($totals);
    if ($total > 0)
        return $mp->format_currency('', $total);
    else
        return '$0.00';
}
?>

This works fine locally on MAMP, but when I upload it to my server I get "Warning: array_sum() expects parameter 1 to be array, null given in functions.php on line 255" instead of the cart total.

What is wrong with the code, and why is there a discrepency between the same file on my localhost and the online server?

share|improve this question

closed as off topic by Chip Bennett, Wyck, brasofilo, toscho Dec 12 '12 at 21:40

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
This appears to be a PHP question, not a WordPress question. The error message is telling you that $totals is null, meaning it is not defined. Since $totals gets set when $cart has data as $product_id => $variations, the problem is likely either that you have no $cart defined, or $cart has no $variations. The solution is to wrap $total in an if ( isset( $totals ) ) conditional. –  Chip Bennett Dec 12 '12 at 16:57

1 Answer 1

up vote 1 down vote accepted

It is probably because of either different versions of PHP or different configuration for the Error logging. Assuming you have same environment (cart status, blog status, logged in user status etc)

To overcome this, Please initialize $totals with empty array in the start of the function.

And, If you want error to show up for localhost set WP_DEBUG constant to true (in wp-config.php) on localhost. You may also need to call error_reporting(E_ALL) at the top of your script.

hope it will fix your problem.

share|improve this answer
    
Thanks a lot, that was simple enough. And my apologies for posting a non-related question. I wasn't sure it was WP causing the errors or not! –  marctain Dec 12 '12 at 17:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.