Tag Archives: joomla

Howto: Create “Cash on Delivery” payment Varied from Different Postcodes for Virtuemart

Actually Virtuemart has done a great job on cash on delivery support, but what if cash on delivery cost a fee not only the total amount of purchase but also the different postcode? Here comes a tutorial on how to calculate the different rates based on users’ postcode.

Of course, before doing that, you need to have a workable Virtuemart distribution on joomla. You can find more tutorials on joomla official website or virtuemart website.

There are only two steps of this simple tutorial:

  1. Create a new payment method on Cash on Delivery. Actually the orignal virtuemart comes with an option of “COD”, but at this time I don’t want to ruin its file structure. I prefer to create a new one. Actually creating new payment method helps you to know more about joomla.
  2. Revise “ps_checkout.php” to make it works.

Let’s dive to details.

Create a new payment method on Cash on Delivery.

Sooner or later will you find that creating payment method couldn’t be easier. What you need to do is to create a class, and of course, you write some methods which is recoginzed by joomla framework. You can find bunch of original payment classes on /administrator/components/com_virtuemart/classes/payment/ folder. If you see carefully, you can see each payment class comes with a config file. For instance, you can see a payment class called ps_paypal.php, should come with a config named ps_paypal.cfg.php. I won’t use config file, so I don’t need the cfg.php file. Now here’s my turn on creating such a class:  create a file name “ps_codonpc.php” (which means “Cash On Delivery On PostCodes”), never mind its name, but I think you better follow the naming convention of Virtuemart. Open this file with any kind of text-editor, I recommend to use notepad++. Create a skeleton below.

if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
 
class ps_codonpc{
 
var $classname = "ps_codonpc";
var $payment_code = "PU";
 
function show_configuration() {
return false;
}  
 
function has_configuration() {
return false;
}
 
function get_payment_rate($sum, $ship_to_info_id=null) {
}  
 
function configfile_writeable() {
return true;
}
 
function configfile_readable() {
return true;
}
 
function write_configuration( &$d ) {
return true;
}
 
function process_payment($order_number, $order_total, &$d) {
return true;
}
 
}

I know what you must be thinking.  You must be wondering if there is any necessity to create those functions within the class. Yes, you have to. That’s the weakness as well as fexibility of the characteristic  object-oriented of PHP. Well, it makes senses, because PHP is runtime dynamic language, not compile-time language. The payment classes on /payment folder are dynamically recognized by the framework, they use the same functions naming convention to make the polymorphism works.  I mean the ps_paypal.php and ps_paymate.php are of the same functions’ name, and created by the framework, however the framework doesn’t need to know the existance of payment classes and doesn’t know what they are doing. The framework only knows their functions’ name. Confused? Never mind.  Forget this paragraph. You can do the same thing on java using “reflection”.

You can see a function called “get_payment_rate” in this class. Well, not all the classes in /payment folder have this function. Why? That’s because virtuemart framework will only calculate the payment rate if the “get_payment_rate” function is defined. Actually, there is only one parameter is need: $sum, but since we need to calculate the fee by post code, I add an additional paramter(ship_to_info_id)  and give it a default value(null).

Now you need to fill the “get_payment_rate” logic to make it works. I add a simple logic to calculate the payment rate.

function get_payment_rate($sum, $ship_to_info_id=null) {
		//Get the ship_to_info_id if the parameter is null
		//Normally the item is passed from checkout module
		//We need to change ps_payment_method.php to pass the parameter to this object.
		if( empty( $ship_to_info_id )) {
 
		    $sdb = new ps_DB();
 
		    $sdb->setQuery( "SELECT user_info_id FROM #__{vm}_user_info WHERE user_id=".$auth['user_id']." AND address_type='BT'" );
 
		    $ship_to_info_id = $sdb->loadResult();
 
		}
 
		//Get the zip(post code) from user info table
 
if(!empty( $ship_to_info_id )){
 
			$sdb = new ps_DB();
 
			$qry = "SELECT `zip` FROM `#__{vm}_user_info` WHERE user_info_id='".$ship_to_info_id."'";
 
			$sdb->query($qry);
 
			$sdb->next_record();
 
			$postcode = $sdb->f('zip');
		}  
 
		if(!empty($postcode)){
			if($postcode >= '3000' && $postcode < '3050'){
				return 0;  //these areas are free
			}else if($postcode >= '3050' && $postcode < '3150'){
				return floatval( $GLOBALS['CURRENCY']->convert(-5));  //these areas cost $5
			}else{
				return floatval( $GLOBALS['CURRENCY']->convert(-10)); //the othes cost $10
			}
		}else{ //unknow post code, use default
			return floatval( $GLOBALS['CURRENCY']->convert(-10)); //the othes cost $10
		}
}

Can you see the return value? it’s negative, that’s because the get_payment_rate function return the payment discount (according to the definition of this function), the name should be changed to get_payment_discount. Otherwise would be a little bit confused. If you return a positive, the users will get a discount from this payment; a negative, the users will pay an extra fee.

OK. That’s fine.

Revise “ps_checkout.php” to make it works.

And now go to ps_checkout.php, and find a line 1673, change “return $_PAYMENT->get_payment_rate($subtotal);” to

$ship_to_info_id = $_REQUEST['ship_to_info_id'];
return $_PAYMENT->get_payment_rate($subtotal, $ship_to_info_id);
“.

Okey dokey, Now add the Cash On Delivery paymethod on your administration page, and then go through the check out process and find out more excitement. Of course, the logic provided here is very simple, you can judge the rate from a post code data table and apply new features.

Howto: Import data from Excel to joomla table using Kimport v1.5.0

After proper installation of Kimport component for joomla 1.5.x, you can try to manage the data using this component.

There is something you need to do before uploading the excel file to the component. The most important step to do is mapping the data columns to the excel columns. For instance, you probably have a table like this:

example data table

You can read the data structure by typing “describe [your-table-name]” on the command line or GUI-based mysql admin tools. Then you may now construct your excel file. Create a Excel file in Office 2003 (note that Kimport currently recognizes Excel2003), the paradigm of the excel file should look like: (Click the picture to enlarge the picture below)

excel file with a proper paradigm

You can see that the first column of the Excel is reserved to further use,  then the first row have to be IDENTICAL with the columns’ name of your data table. The second row is the data type property of the column. For instance, product_sku should be string, so the data type is written “string”.

Okay, now you know that the excel’s paradigm is to map the data meta information of the actual data table. The yellow high-lined part of this excel means the reserved information. Please also note the “sheet name” below. The sheet name should be the EXACTLY same as the table’s name. After the excel file is prepared, you can proceed to upload the file to the component.

Two steps are need to perform upload:

  1. Select “Kimport” from the top menu “Components”. (click the image below to enlarge)
    step 1
  2. Select the Excel file from the local computer. Then click “Save” on the top-right of the page. (click the image below to enlarge)
    step 2
  3. If upload is succeed, you can see a information notification on the page. (click the image below to enlarge)
    step 3

Easy, huh? But Still need to keep in mind when you arrange the Excel data:

  • The first column of the excel file is reserved.
  • The first row without the first column is mapped to the columns’ name of the data table.
  • The second row without the first column is the data type of the column
  • The sheet name of the excel file should rename to the EXACT name of the data table.

Kimport v1.5.0 installation guide

Just like the other components, you need to install Kimport before using it.

Pre-requisition:

  • Have joomla 1.5.x installed.
  • Login to joomla adminitration page as Super Administrator role user.

Installation: (3 Steps)

  1. Select install/uninstall from the main menu. If you can’t see the sub-menu on top, please confirm if you have an Super Admin role privilege.
    install step 1step 1
  2. Select the com_kimport.zip on your local folder. Press “Upload File & Install” button.  
    step 2step 2
  3. After a short while (com_kimport is small size), you can see the successful post-screen.     
    Step 3step 3

Now you can see “Kimport” sub-menu on the “Components” menu on top.

Kimport v1.5.0 for joomla 1.5.x Released

If you have ever been a web master, you would probably know joomla project or even use it for managing your web contents. It is easy to use and deploy. The only thing to do before using joomla should you have Apache and Php 5+ installed on your own server or hosting server. For SEF convenience, better have mod_rewrite on Apache server.

During the mainteinance of the joomla website, I was getting tired for adding new articles and other contents, that is, you have click on the “Add” button, type all the contents(or copy them from your notepad) to the relevant page, then press “Save” or “Apply” so that the new article is post. Then if you have another article to post, you have to go back to ground-zero and repeat the steps. 5 articles in all in one time gonna kill me, how about you get 20 more to post in one time? So that is the original idea for me to design a joomla component called KIMPORT. That component save my time adding the new contents. KIMPORT now becomes an administrator-oriented utility for manipulating the data table. Before adding new contents, you just fill in a specific excel file (2003) in a certain paradigm and then upload the excel with the component, then you got your data filled in the data table. The setup and the instruction guides please refer to the new post of my blog.

The initial version of KIMPORT is 1.5.0, which means it’s native for joomla 1.5.x. Since joomla 1.5.x is GPLv3.0, the license for KIMPORT is GPLv3.0 as well.

kimport

You can download the project source code with any SVN client:

svn checkout http://kimport.googlecode.com/svn/trunk/ kimport-read-only

Or just only download the package for installation:

http://kimport.googlecode.com/files/com_kimport.zip