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:
archive-class.php
Order download handler (as compressed or CSV files).
authorizenet.php
Payment handler with authorize.net.
bank.php
Bank payment handler.
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
cash.php
Payment registration.
checkout.php
Checkout handler.
eShop-add-cart.php
This file determines how a product should be added to the cart.
eShop-admin-functions.php
Back-end administration functions.
eShop-all-data.php
Database operations. It also provides a way to export data as CSV.
eShop-base.php
Basic administrative functions.
eShop-dashboard.php
Dashboard widgets.
eShop-discount-codes.php
Discount code handler.
eShop-downloads.php
Download handler for orders and products.
eShop-email.php
User's e-mail handler.
eShop-user-orders.php
Displays a summary of all orders for users and administrators.
eShop.php
Main file.
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.
thanks for this info, helped me with authorize.net and the clearing of the shopping cart after a successful transaction.
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