WordPress: how the eShop plugin works

The most interesting working experience had so far with WordPress is surely the radical changes I had to make to the eShop plugin. A client wanted that instead of processing orders the plugin should send an e-mail with a pre-order receipt. Further, the shopping cart must be modified so that it can accept a discounted value for each item. Said that, let's see how eShop works.

eShop structure

eShop has the following structure:

  1. archive-class.php

    Order download handler (as compressed or CSV files).

  2. authorizenet.php

    Payment handler with authorize.net.

  3. bank.php

    Bank payment handler.

  4. cart-functions.php

    Cart functions. It performs the following operations:

    • displays the shopping cart
    • calculates total and sub-total price
    • sums up all cart items
    • if present, it calculates the discount for each item
    • checks whether a product has delivery costs
    • applies the discount code
    • checks whether a discount code is valid
    • calculates the delivery costs for each country
    • data validation
    • inserts hidden fields to process cart data
    • saves the order in the database
    • fetches the product thumbnail
    • processes the shopping cart
    • notifies users via e-mail

  5. cash.php

    Payment registration.

  6. checkout.php

    Checkout handler.

  7. eShop-add-cart.php

    This file determines how a product should be added to the cart.

  8. eShop-admin-functions.php

    Back-end administration functions.

  9. eShop-all-data.php

    Database operations. It also provides a way to export data as CSV.

  10. eShop-base.php

    Basic administrative functions.

  11. eShop-dashboard.php

    Dashboard widgets.

  12. eShop-discount-codes.php

    Discount code handler.

  13. eShop-downloads.php

    Download handler for orders and products.

  14. eShop-email.php

    User's e-mail handler.

  15. eShop-user-orders.php

    Displays a summary of all orders for users and administrators.

  16. eShop.php

    Main file.

  17. public-functions.php

    AJAX code in jQuery for the cart.

Shopping cart

You can access all the items contained in the eShop shopping cart as follows:

$shopping_cart = $_SESSION['eShopcart'.$blog_id];

The shopping cart itself is a multidimensional associative array which you can navigate as follows:

foreach($shopping_cart as $product => $value) {

 $product_id = $value['postid'];
    $price = $value['price'];
    $quantity = $value['qty'];
    $product_name = $value['pname'];
    $product_desc = $value['item'];

}

Remember: you can see the contents of the cart by simply printing it to the page whenever you want to (e.g. for testing purpose):

print_r($_SESSION['eShopcart'.$blog_id]);

As you can see, the whole cart is stored in the current PHP session. So if you want to empty the cart, you can do the following:

unset($_SESSION['eShopcart'.$blog_id]);
$_SESSION['eShopcart'.$blog_id] = array();

You can even add custom items to the cart by modifying the associative array. For example, in the checkout.php file you can add the following:

$shopping_cart = $_SESSION['eShopcart'.$blog_id];

if(isset($_POST['custom-discount'])) {    
              $custom = $_POST['custom-discount'];
            
              $discount = -1;
              
            
           foreach($shopping_cart as $item => $value) {
                
               $discount++;
                    
                    if($custom[$discount] != '') {
                        
                        
                        $val = $custom[$discount];
                        
                            
                        $shopping_cart[$item]['custom-discount'] = $val;   
                            
                        
                    }
                    
                    

                
            } 
            
             
}

Now our cart contains another item for each product, that is, custom-discount. Bear in mind that if you have access to the shopping cart, you have also access to many interesting data related to each product. For example, you can get the thumbnail associated to each product (which is usually handled as a post):

foreach($shopping_cart as $product => $value) {

 $product_id = $value['postid'];
    $product_image = get_the_post_thumbnail($product_id, array(150,150));

}

To sum up: if you understand properly how the eShop's shopping cart works, you have a fantastic tool in your hands for customizing this plugin.

2 thoughts on “WordPress: how the eShop plugin works”

  1. thanks for this info, helped me with authorize.net and the clearing of the shopping cart after a successful transaction.

  2. Hi Gabriele

    Thank you for this great post! I'm pretty new to eShop, and I'm trying to display the addtocart box next to my product photo (instead of below the content of the post). For example, by adding a php function int the Wordpress template file; but there seems to be no way to displat the addtocart box where you want.

    Would you be kind enough to provide me with some pointers on how to do that?

    Thanks
    Rico

Leave a Reply

Note: Only a member of this blog may post a comment.