Session Variables in osCommerce
Introduction
Session variables can be very helpful if you need to get data from one of osCommerce’s subsystems and use it in another. In this post, I will use an unusual example to show how to create a session variable in checkout_shipping.php then use it for another purpose in a payment module or two.
Background
My client has osCommerce version 2.3.3.4 with a FedEx shipping module, a LinkPointCentral (First Data) payment module using the offsite functionality and the Free Shipping Per Product add-on.
He has problems with international shipping because the FedEx module does not include all the costs associated with shipping things outside the U.S. Each shipping option can be adjusted upward either by a dollar amount or a % of the base shipping cost, but for one reason or another he has been unable to supply the information that is needed to set that up. Thus, he decided that he would contact all international customers and provide them with the shipping charges by phone or email. I created a special shipping module for international orders that simply advises the customer that they will be contacted with the total cost of the order. They could then make their payment via a special page, tied to the First Data system. The special page is revealed to the customer when they are contacted with the total cost. To keep customers from paying for the item’s cost without shipping, I disabled the First Data module for international orders and presented them with a special international payment module, that only advises them, like the shipping module.
That works ok, but when international customers saw the $35 shipping cost for a $2 sample package, they got upset. They have no problem paying $37 for the item as long as there is no shipping associated with it. To fix that, I installed the Free Shipping add-on and configured two versions of the sample package item. One for U.S. and one for International customers. They have different prices to reflect the different shipping costs, but do not use the FedEx module at all. The Free Shipping add-on handles the shipping cost issue nicely.
This is all wonderful, except there is no longer a need for international customers be contacted with shipping information for this item – if they order it alone. They should be able to go ahead and make their payment using the First Data module. We want the First Data module to be active if the only things they have in their cart are tagged for free shipping.
The List of Payment Options
To get the list of payment modules that are applicable to the customer’s order, catalog/checkout_payment.php calls catalog/includes/classes/payment.php. The class brings in each of the payment models (catalog/includes/modules/payment) that have been activated on the system and each payment module determines whether or not it should be listed. Thus, I needed to modify catalog/includes/modules/payment/linkpointconnectoffsite.php to turn on the module when nothing in the cart requires shipping costs to be added. Different payment modules have different configuration options. First Data, for example, allows the store manager to set it up so that the module is only applicable in certain geographic zone. I turned it off by making it only applicable to the U.S.
Now, we want to turn it on based upon the status of shipping for the products in the cart. You might think that knowing if total shipping is equal to zero would be all we would need. The way this store is set up, international orders never have shipping calculated, so that value is always zero. There are a few ways that this might work, but I chose to use information created by the Free Shipping add-on.
Free Shipping Variable Becomes a Session Variable
The Free Shipping add-on modifies catalog/checkout_shipping.php (among several other files). In the process it creates and initializes four variables. One of those, $products_ship_free, is initially false, but becomes true if all the products in the cart have free shipping.
Unfortunately this is just a local variable used in this process and discarded. We want to use it in a different process, so it must be saved so that it can be available for the other process. After the final value of the variable is set, I added the following line:
$_SESSION[‘products_ship_free’] = $products_ship_free;
We now have a session variable that can be used anywhere we need it.
Using the Session Variable
I need to use the new session variable in linkpointconnectoffsite.php, my First Data payment module. By tracing through the file, I found the spot to add my code, after all the other decisions about whether or not the module would be active are completed. At that point, I added this code:
if ($_SESSION[‘products_ship_free’]==true) { $this->enabled = true; }
Thus, anytime an order has free shipping, the customer can use the First Data module to make their payment.
Unfortunately, customers are still also given the option to choose the manual payment process. Besides being confusing, we don’t want them to do that. Thus, I needed to edit catalog/includes/modules/payment/international.php almost exactly the same way as linkpointconnectoffsite.php. In this case, I inserted the following code at the appropriate location:
if ($_SESSION[‘products_ship_free’]==true) { $this->enabled = false; }
In this case we want to disable the module so enabled = false instead of true.
Ultimately I needed to modify three files to achieve the desired result:
- catalog/checkout_shipping.php to capture the variable needed in the other files
- catalog/includes/modules/payment/linkpointconnectoffsite.php to turn ON the First Data payment module if there is no shipping cost associated with the order
- catalog/includes/modules/payment/international.php to turn OFF the International payment module if there is no shipping cost associated with the order
Conclusion
This is an unusual example, given the way the site has been modified and what I wanted to accomplish. Nonetheless, it demonstrates how information from a seemingly unrelated part of osCommerce can be used in any other part with session variables.
Recent Comments