Externally add a function to a WordPress Plugin’s PHP file (Allowing for easy version update)

I’ve got a real brain tickler that I’ve been wrestling with for a couple of days.

I’m creating a WordPress website for someone (at http://www.lydiala.com/), and using the Simple WordPress Paypal Shopping Cart Plugin.

Read More

I would like to display the total number of items in the cart, in the top-right corner of my header.php file.

I have figured out a function that successfully return this figure, and have added it to the plugin’s main PHP file, at /wp-content/plugins/wordpress-simple-paypal-shopping-cart/wp_shopping_cart.php (alongside all of the other functions).

I took the following EXISTING function in wp_shopping_cart.php:

function simple_cart_total() {  
    $grand_total = 0;   
    foreach ((array)$_SESSION['simpleCart'] as $item){      
        $total += $item['price'] * $item['quantity'];       
        $item_total_shipping += $item['shipping'] * $item['quantity'];  
    }   
    $grand_total = $total + $item_total_shipping;
    return number_format($grand_total,2); 
}

I then copied it, renamed it and changed it as follows:

function total_items() {    
    $grand_total = 0;   
    foreach ((array)$_SESSION['simpleCart'] as $item){      
         $total += $item['quantity'];   
    }
$grand_total = $total;  
    return number_format($grand_total); 
}

Now, in my header.php I have:

echo total_items();

Which works brilliantly well!

BUT, when the plugin updates in the future, this newly-added function will be lost when wp_shopping_cart.php is overwritten with a new version.

Is there a way to append my custom function to this external .php file? I already tried a few methods, but most of them don’t work because they try to load wp_shopping_cart.php, which has already been loaded (“Can’t redeclare”, etc.).

If worse comes to worst, I can always tells the client to call me when updates of this plugin are available.

Related posts

Leave a Reply

2 comments

  1. I would recommend adding this in a different plugin.

    paypall-totals.php in your wp-content/plugins (don’t forget to activate in the wp-admin)

    /*
    plugin-name: paypall totals
    description: adds a function to show paypall totals
    pluginURI: http://stackoverflow.com/q/17066291/933065
    */
    
    function paypall_total_items() { 
      session_start();   
      $grand_total = 0;   
      foreach ((array)$_SESSION['simpleCart'] as $item){      
        $total += $item['quantity'];   
      }
      $grand_total = $total;  
      return number_format($grand_total); 
    }
    

    Since it only gets it’s data out $_SESSION it doesn’t matter where you put this function. It also would work in your theme functions.php

    Also adjust the code in the header.php: echo paypall_total_items();

    Just restore the paypall plugin to it’s original code.

    Let me know.

  2. sorry, I can not comment now is, should split into a temporary answer.
    I think you should not write a plugin, you should put this code in your theme’s function.php.
    and check if plugin “WordPress Simple Paypal Shopping Cart Plugin.” have been installed or not by the following code:

    if (function_exists ("simple_cart_total")) {...}