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 file structure
eShop has the following file 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.
paypal.php
Functions to handle PayPal transactions.
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));
}
You can also modify the way your cart will be displayed to the final user. For example, if the minimum order total is, say, 30 US dollars, you
can prevent a user from going to the checkout page by simply editing the cart-functions.php
file:
// modifying the display_cart() function
function display_cart($shopcart, $change, $eshopcheckout,$pzone='',$shiparray='') {
//The cart display.
$minimum_total_order = 30;
// On line 353:
if($final_price > $minimum_total_order) {
// display the checkout link
} else {
// display a warning
}
}
PayPal
Eshop makes use of the PayPal merchant gateway in a simple but effective manner. There are a few requirements you should be aware of:
- Either you're working in test mode (PayPal's sandbox) or live mode, you need a Business account on PayPal.
- Your account's email must be correctly associated to your account.
- Your account must be active and verified.
- You should remove PayPal's limitations from the transactions of your account.
- Your landing page (the PayPal's payment page where users are redirected after the checkout process) should be properly configured by using the options provided in your PayPal's profile page.
- The return pages ("Thank You" page, "Cancelled Order" page) created by Eshop must be accessible.
eShop builds a simple HTTP POST query and sends the data collected from the shopping cart and the checkout page (user's information such as name, email, and so on) to PayPal. It doesn't make use of the PayPal's APIs, although it's possible to modify this behavior (possible only if you know exactly what you're doing) by editing two files, namely:
paypal/eshop-paypal.class.php
– The PayPal custom class used by eShop.paypal.php
– The transaction handler used by eShop.
The entire POST query is built within the latter file. If you want to modify this query, you should first read the PayPal's documentation on query parameters. Bear in mind that if you change, add or remove a parameter without knowing what you're doing, the final result could be an error returned by PayPal or a landing page with some elements missing.
Conclusion
To sum up: if you understand properly how eShop works, you have a fantastic opportunity in your hands for customizing this plugin.