Hi Guys,
I had a major headache trying to get to this, so I truly hope it's of use to others.
The problem: there is no way to set basic shipping (postage) charges with the new 4.7 version of ecommerce, apart from the shipcalc.module add on for United Parcel Services. There are a few modules in development, such as the shipbasic.module but it didn't suit my needs.
I wanted to be able to set postal rates for a few zones..i.e.
Zone 1: Ireland
Zone 2: UK & Mainland Europe
Zone 3: USA & CANADA
Zone 4: Rest of the World.
The solution (which is based on a basic.inc add-on for shipcalc by annyer that covers custom postage rates within the USA) is pasted below. Called international.inc you simply follow these steps to get up and running;
- copy the snippet below or download the international.txt attachment to this post and upload it to your modules/ecommerce/contrib/shipcalc/partners folder using the file name international.inc
- go to admin -> settings -> content types and configure "shippable products" to make the International shipping method available to shippable products
- go to admin ->store->settings->Shipcalc->international and set your zone charges and countries. you an multiple select countries for each zone.
- go to a product node, click on EDIT and make sure that the international shipping method is available for products
<?php
// $Id: international.inc,v 1.1 2006/08/01 06:30:01 Dublin Drupaller Exp $
/**
* @file
* Functions to communicate with shipcalc.module
*
* Allows the site admin to set Zone 1 Zone 2, Zone 3 and rest of The World
* Shipping settings. Requires the ecommerce suite of modules
* and shipcalc.module installed.
*
* this works with drupal 4.7
*
* NOTES;
*
* Upload this international.inc file into your modules/ecommerce/contrib/shipcalc/partners folder.
*
* Adjust countries and shipping charges per zone at admin -> store -> settings -> shipcalc -> international
*
* Make this shipping method available to products by configuring at admin -> settings -> content types
*
* Once enabled for content types (products) a new form will appear when editing
* products that allows you to include this shipping method.
*
*/
/**
* Shipcalc _shipping_methods hook.
*
* Define the international shipping methods.
*/
function international_shipping_methods($type = 'international') {
// TODO: Add descriptions of various shipping methods.
$methods = array();
$methods['international'] = array(
'#title' => t('international'),
'#description' => t('international Shipping.'),
);
switch ($type) {
case 'domestic':
default:
$methods['international']['Flat Rate'] = array(
'#title' => t('Flat Rate'),
);
break;
case 'international':
$methods['international']['Flat Rate'] = array(
'#title' => t('Flat Rate International'),
);
}
return $methods;
}
function international_product_form(&$form) {
$form['shipping_data']['product_weight'] = array(
'#type' => 'textfield',
'#title' => t('Product weight'),
'#description' => t('The weight of the product (in %unit)', array('%unit'=> (variable_get('shipcalc_units', 'LBS')) ? t('pounds') : t('kilograms'))),
'#default_value' => $form['#node']->product_weight
);
}
/**
* Shipcalc _settings_form hook.
*
* Create a form for international-specific configuration.
*/
function international_settings_form(&$form) {
$form['international'] = array(
'#type' => 'fieldset',
'#title' => t('international settings')
);
$form['international']['international_region_1_charge'] = array(
'#type' => 'textfield',
'#title' => t('Zone 1 Charge'),
'#default_value' => variable_get('shipcalc_international_region_1_charge', ''),
'#required' => TRUE,
'#description' => t('Set the Zone 1 charge')
);
$form['international']['international_region_1_countries'] = array(
'#type' => 'select',
'#title' => t('Zone 1 Countries'),
'#default_value' => variable_get('shipcalc_international_region_1_countries',''),
'#options' => store_build_countries(),
'#multiple' => TRUE,
'#description' => t('Select the country or multiple select countries for the Zone 1 charge.'),
);
$form['international']['international_region_2_charge'] = array(
'#type' => 'textfield',
'#title' => t('Zone 2 Charge'),
'#default_value' => variable_get('shipcalc_international_region_2_charge', ''),
'#required' => TRUE,
'#description' => t('Set the Zone 2 charge')
);
$form['international']['international_region_2_countries'] = array(
'#type' => 'select',
'#title' => t('Select Zone 2 countries'),
'#default_value' => variable_get('shipcalc_international_region_2_countries',''),
'#options' => store_build_countries(),
'#multiple' => TRUE,
'#description' => t('Select the country or multiple select countries for the Zone 2 charge'),
);
$form['international']['international_region_3_charge'] = array(
'#type' => 'textfield',
'#title' => t('Zone 3 Charge'),
'#default_value' => variable_get('shipcalc_international_region_3_charge', ''),
'#required' => TRUE,
'#description' => t('Set the Zone 3 charge')
);
$form['international']['international_region_3_countries'] = array(
'#type' => 'select',
'#title' => t('Select Zone 3 countries'),
'#default_value' => variable_get('shipcalc_international_region_3_countries',''),
'#options' => store_build_countries(),
'#multiple' => TRUE,
'#description' => t('Select the country or multiple select countries for the Zone 3 charge'),
);
$form['international']['international_region_4_charge'] = array(
'#type' => 'textfield',
'#title' => t('Rest of The World Charge'),
'#default_value' => variable_get('shipcalc_international_region_4_charge', ''),
'#required' => TRUE,
'#description' => t('Set a Rest of the World charge for any countries not covered by Zone 1, 2 and 3')
);
return $form;
}
/**
* Shipcalc _settings_form_submit hook.
*
* Save data from our international-specific configuration form.
*/
function international_settings_form_submit(&$form) {
global $form_values;
if ($form_values['shipping_partner'] == 'international') {
variable_set('shipcalc_international_region_1_charge', $form_values['international_region_1_charge']);
variable_set('shipcalc_international_region_1_countries', $form_values['international_region_1_countries']);
variable_set('shipcalc_international_region_2_charge', $form_values['international_region_2_charge']);
variable_set('shipcalc_international_region_2_countries', $form_values['international_region_2_countries']);
variable_set('shipcalc_international_region_3_charge', $form_values['international_region_3_charge']);
variable_set('shipcalc_international_region_3_countries', $form_values['international_region_3_countries']);
variable_set('shipcalc_international_region_4_charge', $form_values['international_region_4_charge']);
}
}
/**
* Shipcalc _get_rates_form hook.
*
*
*/
function international_get_rates($txn) {
// check to see if a shipping address has been set of if the billing address shuold be used
if (!$txn->address['shipping']->city) {
$ship_country = $txn->address['billing']->country; // shipping form was left blacnk so use the billing country address
}
else {
$ship_country = $txn->address['shipping']->country; // use the shipping country address
}
$zone1_countries = variable_get('shipcalc_international_region_1_countries', '');
if (in_array($ship_country,$zone1_countries)) {
$rates[international] = array(
'#key' => international,
'#cost' => variable_get('shipcalc_international_region_1_charge', ''),
'#currency' => 'EUR',
'#method' => 'ground'
);
}
$zone2_countries = variable_get('shipcalc_international_region_2_countries', '');
if (in_array($ship_country,$zone2_countries)) {
$rates[international] = array(
'#key' => international,
'#cost' => variable_get('shipcalc_international_region_2_charge', ''),
'#currency' => 'EUR',
'#method' => 'ground'
);
}
$zone3_countries = variable_get('shipcalc_international_region_3_countries', '');
if (in_array($ship_country,$zone3_countries)) {
$rates[international] = array(
'#key' => international,
'#cost' => variable_get('shipcalc_international_region_3_charge', ''),
'#currency' => 'EUR',
'#method' => 'ground'
);
}
//if the country has not been picked up set the Rest of The World charge
if ((!in_array($ship_country,$zone3_countries)) && (!in_array($ship_country,$zone2_countries)) && (!in_array($ship_country,$zone1_countries))){
$rates[international] = array(
'#key' => international,
'#cost' => variable_get('shipcalc_international_region_4_charge', ''),
'#currency' => 'EUR',
'#method' => 'ground'
);
}
return $rates;
}
I know this can be improved upon...but it works. feel free to add in or tweak new features/improvements. It took me blinking ages to get this working...so I hope it's of use to others.
Dub
| Comment | File | Size | Author |
|---|---|---|---|
| international.inc.txt | 7.88 KB | dublin drupaller |
Comments
Comment #1
nedjoThanks Dub, this does look useful.
How does this compare to http://drupal.org/node/71718? Are both needed? Should we combine them?
And see my questions at http://drupal.org/node/71718#comment-132107. Is Shipcalc the appropriate/best way to implement this?
Comment #2
chueewowee commentedI think it's just what i sneeded by me, for one. Thank you. It works to a point in my installation: I removed basic.inc t avoid conflict, but whatever adress I use, with zone 1 or zone 'rest of world', I am getting a zone 'rest of world' charge.
DO I have to call some javascript as in basic.inc?
Comment #3
dublin drupaller commentedno javascript is required...quick questions:
did you rename the file to basic.inc ?
if you did, rename it back to international.inc otherwise it won't work.
have you setup your ZONE countries?
You need to do that first...i.e. specify which countries belong with each ZONE.
If I remember correctly...you need to go to
ADMINISTER -> STORE -> SETTINGS -> SHIPCALC -> INTERNATIONAL
you can multiple select countries for each zone.
Hope that helps
Dub
Comment #4
chueewowee commentedThanks Drupalier. I have the original name, and have set the zone one countries, yes, and set content types. I'l play around a bit more.
Comment #5
chueewowee commenteddo I need to keep the basic.inc file in the directory?
Comment #6
chueewowee commentedI de-selected basic in the content types, and selected flat rate, and deselected flat rate for international shipping options. All zones are set in the shipcalc/international settings but I still get just the zone 4 charge in checkout.
Comment #7
dublin drupaller commentedI just tried the international.inc again on a fresh 4.7 install. worked fine. you must be missing some of the steps.
Can I recommend you start again and follow each step in the international.inc snippet posted above?
Dub
Comment #8
kuahyeow commentedHi dub, this is a very useful and fantastic piece of code! However, I noticed that weight is not really used. (ref function international_product_form). Do you intend to add weight support sometime in the future?
Comment #9
hedac commentedinternational.inc is what I was looking for. thank you :)
I get the correct zone shipping charge in the checkout... but.. I'm using paypal payment method... and pressing the place your order button.. goes to the paypal payment windows... and only zone 1 charge is applied in the shipping costs. maybe it is a problem in the paypal module?
Comment #10
hedac commentedsorry... I renamed the title accidentally
Comment #11
kuahyeow commentedThat's strange, because my zone 2 was applied successfully in Paypal. Maybe check that the country you're testing is actually in the zone?
Comment #12
hedac commentedyes.. I followed all the steps... nad selected some countries for zone 2 with a different rate, and zone3 countries with another rate... tried with zone 2 and zone 3 countries and always zone1 rate is applied in the paypal page. Also if the country would not be in the list of any specified zone... I asume the "rest of the world" rate should be applied.
Everything works perfectly in the checkout review but not in the paypal payment window. :(
Comment #13
hedac commentedI'm sorry.. it is working perfectly...
there was a checkbox in paypal.com settings..... to override shipping costs by the application.... so.. there is no problem at all.
international.inc is working very well.
Comment #14
kuahyeow commentedHedac: good that you solved the problem.
Setting it back to active, as it may be useful for others.
Comment #15
alynner commentedI just noticed this posting and wanted to say it looks great. I also wanted to point out a new inc file that I have created and I think no one has tested: http://drupal.org/node/93891, I'm sure it could be integrated to this one it if someone has a need.
btw what does it take to get these inc files in the repository? I have noticed there seem to be lots of people lost when it comes to shipcalc partners but there are quite a few working files in the issues.
-alynner
Comment #16
simePlease see here for new flexicharge features (including simple shipping), now in dev version of 4.7v3 and 5v3.
http://drupal.org/node/122769
There are instruction there too for creating custom .inc files.
Comment #17
simeComment #18
(not verified) commented