Create a google AdSense box for your osCommerce store
multimixer | learn | Tuesday July 26 2011I had a client who wanted to display google AdSense adds on the right column of his store. Now I need to say, that I’m not the greatest fan of such an idea: A store is there to make money by selling its products or services, not through advertising or anything else. Each visitor should be focused on what the store has to offer, any ads or similar displays are just a distraction in my opinion.
Honestly, if your store doesn’t make enough money, you should focus on that, or start advertising for the store and not on the store
Anyway, this was the request, so if you like something like this too, here you go
Step 1: Create the files
All boxes are in osCommerce in folder catalog/includes/modules/boxes/
The corresponding language files are in folder catalog/includes/languages/english/modules/boxes/ and have exactly the same filename as the box it self
What we do now is to take the most simple box that exist, that is bm_information.php, make a copy and rename it to be bm_adsense.php. Same we do with the corresponding language file. So we have 2 fies now called bm_adsense.php.
Keep them open in your editor, do not upload them and more important do not install and activate them in admin, you will get errors at that stage
Step 2: Basic setup
As we know, all boxes are so called “classes” and do write configuration values (constants) to the database. That mean that some parts of the content of that files must be unique.
Before doing anything else, we need to make the file contents “unique” in that meaning. We do this by replacing class function names and constants as follows:
1 Box file: class and function names – 3 replacements
Replace all instances of ”bm_information” with “bm_adnsense”, so for example, on top of the file, it was
class bm_information { var $code = 'bm_information';
and it will look like
class bm_adsense { var $code = 'bm_adsense';
2 Box file: Constants – 18 replacements
replace all instances of “MODULE_BOXES_INFORMATION” with “MODULE_BOXES_ADSENSE” Please note the capital letters.
For example it was
if ( defined('MODULE_BOXES_INFORMATION_STATUS') ) { $this->sort_order = MODULE_BOXES_INFORMATION_SORT_ORDER; $this->enabled = (MODULE_BOXES_INFORMATION_STATUS == 'True');
and now it look like
if ( defined('MODULE_BOXES_ADSENSE_STATUS') ) { $this->sort_order = MODULE_BOXES_ADSENSE_SORT_ORDER; $this->enabled = (MODULE_BOXES_ADSENSE_STATUS == 'True');
3 laguage file: Constants
Same thing you do in the language file, well, ok, you don’t need all that defines, so you can basically replace the contents of that file with this
define('MODULE_BOXES_ADSENSE_TITLE', 'adsense'); define('MODULE_BOXES_ADSENSE_DESCRIPTION', 'Show adsense box'); define('MODULE_BOXES_ADSENSE_BOX_TITLE', 'adsense');
Right now you can upload the files if you like and go and enable them in your admin panel under modules>boxes. What you have now is a clone of the information box. Next step will be to modify the file for our purpose
Step 3 :File modification
All we have to do now, is to create and add to that box a new text input field to where we will add our adsense code in the administration panel
We need one more configuration constant for the database, that we create as follows in function install()
tep_db_query( "insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ( 'adSense code', 'MODULE_BOXES_ADSENSE_CODE', '', 'Paste the adSense code into here', '6', '4', 'tep_draw_textarea_field(\'configuration[MODULE_BOXES_ADSENSE_CODE]\', false, 35, 20, ', now())" );
And we need of course to add that constant to the array of keys, as follows
function keys() { return array('MODULE_BOXES_ADSENSE_STATUS', 'MODULE_BOXES_ADSENSE_CONTENT_PLACEMENT', 'MODULE_BOXES_ADSENSE_SORT_ORDER', 'MODULE_BOXES_ADSENSE_CODE'); }
Last thing to do is, to set our box to display whatever we entered into the text box. This we do in function dataF() that will look after our changes, just like this
function dataF() { $data = '<div style="text-align:center;">'; $data .= MODULE_BOXES_ADSENSE_CODE; $data .='</div>'; return $data; }
Please note that if you did not do the changes as described here you will not find such a function in your box. You will need to do that changes to function execute() accordingly
Guess what?
We are done ! Really !
Lets go to the administration panel to see what we did. We will find our box under modules>boxes, we will need to install it, and by clicking on “edit” we have following
and a preview in the admin
And that is how the box look like on my clients site
If you like this idea too, then go ahead and do it, and if you ge rich because of your ads, you can buy me a beer :)
In the same way you can create of course any box. Even this bo here can be used for other purposes too, for example to display plain text (just add plain text into the textarea in admin instead of the adsense code) or images or anything like that
you saved my life :) thank you.
Hi George,
If I want to stop specific box (e.g. shopping cart) from appearing in certain pages, I could add these codes in function execute() in bm_shopping_cart.php:
global $PHP_SELF;
$current_page = basename($PHP_SELF);
if ($current_page == FILENAME_SHOPPING_CART) return;
if ($current_page == FILENAME_ACCOUNT_HISTORY_INFO) return;
if ($current_page == FILENAME_CHECKOUT_CONFIRMATION) return;
However, after MTS is activated, these codes stop working. How do I fix this?
Thanks in advance for your help.
Hi Eddy
This comes because Mini Template System “re-execute” the boxes depending on your template settings. That is the reason why “data” has been separated from “execution”
If you want to add any conditions, best is you do this in function “dataF()” You could by the way merge all of your conditions into one using “or” that look like this “||”.
Something like this: if (condition1 || condition2 || condition3); return;
Not sure also why you store $PHP_SELF into a variable and don’t use it as is?
Thank you very much.
Doesn’t it make the lines shorter and more readable if I don’t use the operator “||” and by storing $PHP_SELF into a variable? Is there any side-effect the way I did it?
Hi Eddy, I will make a separate post about this, as a side note, a mini template system module that will let you create a custom layout per page is under construction
MTS PageMaker?! Shoot!! Looking forward to it.
Hi,
I am trying to make a new box, I have added new files for that as mentioned but I cannot see my box in Admin Panel under Modules->Boxes.
What can be wrong?
Thanks
@Sanjivani
If you can’t see the box at all when you click on “install” on the top right corner in admin (under modules>boxes) then I would suspect that you missed some point at the replacements, in the example above from “information” to “adsense”
Most possible the issue is in the constants, whatever is in CAPITAL_LETTERS
Hello George,
I followed your instructions, but still not working on my website.can you help me, with rechecking my code?
title = MODULE_BOXES_ADSENSE_TITLE;
$this->description = MODULE_BOXES_ADSENSE_DESCRIPTION;
if ( defined(‘MODULE_BOXES_ADSENSE_STATUS’) ) {
$this->sort_order = MODULE_BOXES_ADSENSE_SORT_ORDER;
$this->enabled = (MODULE_BOXES_ADSENSE_STATUS == ‘True’);
$this->group = ((MODULE_BOXES_ADSENSE_CONTENT_PLACEMENT == ‘Left Column’) ? ‘boxes_column_left’ : ‘boxes_column_right’);
}
}
function dataF() {
$data = ”;
$data .= MODULE_BOXES_ADSENSE_CODE;
$data .=”;
return $data;
}
function execute() {
global $oscTemplate;
$oscTemplate->addBlock($this->dataF(), $this->group);
}
function isEnabled() {
return $this->enabled;
}
function check() {
return defined(‘MODULE_BOXES_ADSENSE_STATUS’);
}
function install() {
tep_db_query(“insert into ” . TABLE_CONFIGURATION . ” (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values (‘Enable adsense Module’, ‘MODULE_BOXES_ADSENSE_STATUS’, ‘True’, ‘Do you want to add the module to your shop?’, ’6′, ’1′, ‘tep_cfg_select_option(array(\’True\’, \’False\’), ‘, now())”);
tep_db_query(“insert into ” . TABLE_CONFIGURATION . ” (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values (‘Content Placement’, ‘MODULE_BOXES_ADSENSE_CONTENT_PLACEMENT’, ‘Left Column’, ‘Should the module be loaded in the left or right column?’, ’6′, ’1′, ‘tep_cfg_select_option(array(\’Left Column\’, \’Right Column\’), ‘, now())”);
tep_db_query(“insert into ” . TABLE_CONFIGURATION . ” (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values (‘Sort Order’, ‘MODULE_BOXES_ADSENSE_SORT_ORDER’, ’0′, ‘Sort order of display. Lowest is displayed first.’, ’6′, ’0′, now())”);
tep_db_query( “insert into ” . TABLE_CONFIGURATION . ” (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ( ‘adSense code’, ‘MODULE_BOXES_ADSENSE_CODE’, ”, ‘Paste the adSense code into here’, ’6′, ’4′, ‘tep_draw_textarea_field(\’configuration[MODULE_BOXES_ADSENSE_CODE]\’, false, 35, 20, ‘, now())” );
}
function remove() {
tep_db_query(“delete from ” . TABLE_CONFIGURATION . ” where configuration_key in (‘” . implode(“‘, ‘”, $this->keys()) . “‘)”);
}
function keys() {
return array(‘MODULE_BOXES_ADSENSE_STATUS’, ‘MODULE_BOXES_ADSENSE_CONTENT_PLACEMENT’, ‘MODULE_BOXES_ADSENSE_SORT_ORDER’, ‘MODULE_BOXES_ADSENSE_CODE’);
}
}
?>
Hello Hoffmann
The comments area of the blog is not good for posting any code, it get corrupted
Anyway, first question is: What is not working?
Hello George
First, I’m sorry if that code makes you dizzy.
Second, in the view of my websites, adsense code just become blank ads?instruction adsense you make,..now make me dizzy..
thirth
Google Adsense code and SSL issues?whats your opinion with that issues?