Pagseguro – ‘ship in cart’
Ever been to an online store to purchase yourself the latest Gizmo only to find, after entering all your details, that they want to charge what you consider an extortionate amount for delivery? You leave the store, they lose a sale, nobody is happy.
This is a pretty standard drawback in most online stores but some offer ‘shipping quotes’ on either the product page or in the basket. A bog standard osCommerce store does not offer this functionality but, back in 2004, Justin Baldwin came up with the ‘Ship in Cart’ addon which, after a few refinements, is still going strong today.
Ship in cart works by interrogating all the installed and activated shipping modules and returning a value for the shipping to either the customer’s address or indicated address. It will not work with Pagseguro as this payment module can be run with no shipping module installed. So, in order to display a shipping quote for Pagseguro here is some code that I have written.
n.b. This code was designed to work in a store that ONLY uses Pagseguro as a payment processor and only the Pagseguro Sedex shipping option
It’s a bit rough and may need fettling for your store – I have not enough time to make it into an osC contribution so things like includes/filenames.php have not been used.
It needs a couple of lines added to work with the STS Template system
Visit the Ship In Cart addon page here
Need help with it? Ask me.
Need the CEP javascript? Here
(CEP is the Brazilian Postcode and this script was obtained from mydesigntool.com)
Make sure you have filled in the Shipping Origin in your admin (Admin > Configuration > Shipping/Packaging > Postal Code (if you haven’t then the code will not run)
Okay – here’s the code and steps to install
Backup!!
1. Download the Pagseguro developer’s files from here.
2. Grab the file ‘frete.php’ from the download and upload it to your osCommerce installation. I placed mine in the main catalog folder but you could, if you wanted to be tidier, save it into one of the /includes/ folders.
3. Now open your checkout_shipping.php file in your editor and find the following:
} else {
?>
<tr>
<td align="center" class="main"><?php new infoBox(array(array('text' => TEXT_CART_EMPTY))); ?></td>
</tr>
4. Immediately above these paste in the following code
?>
<tr>
<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
</tr>
</form>
<tr>
<td align="right">
<?php
//Graeme Tyson
//Pageseguro freight estimator
//Validate Brazilian Postcode javascript from mydesigntool.com
?>
<script language="JavaScript">
function postcode_validate(postcode)
{
//01001-000
var obj;
var regRange= /^(([0][0-1][0]{3}[-][0-9]{3})|([0]{4}[1][-][0-9]{3})|([9]{4}[1-9][-][0-9]{3})|([9]{4}[0][-][9][7][1-9])|([9]{4}[0][-][9][8-9][0-9]))$/;
var regPostcode = /^([0-9]){5}([-])([0-9]){3}$/;
obj = document.getElementById("status");
if(regRange.test(postcode) == false)
{
if(regPostcode.test(postcode) == false)
{
obj.innerHTML = "<?php echo CEP_INVALID;?>";
}
else
{
obj.innerHTML = "<a class=\"prod_price\" href=\"javascript: submitformcep()\"><?php echo CEP_GO;?></a>";
}
}
}
function submitformcep(){ document.no_cep.submit();}
</script>
<?php
if (MODULE_PAYMENT_PAGSEGURO_SHIPPING=='True' && MODULE_PAYMENT_PAGSEGURO_STATUS=='True' && (tep_not_null($cart->weight))){//only include if we have pagesguro active, the freight to be calculated by Pagseguro and weight in cart - no weight = error at pagseguro server
if (isset($_POST['no_cep']) && !$_SESSION['cep']) {$my_cep=$_POST['no_cep']; $_SESSION['cep']=$my_cep;$_POST['no_cep']=''; } else {$my_cep=$_SESSION['cep'];}
if(!$my_cep){//no session registered
if(!$customer_default_address_id){//not logged in therefore this value is null
echo tep_draw_form('no_cep', tep_href_link(FILENAME_SHOPPING_CART, tep_get_all_get_params(array('action')) . 'action=no_cep'),'post','onSubmit="return false"');
echo'<span >'.QUOTE_OBTAIN.'</span>
<input onkeyup="postcode_validate(this.value);" type="text" title="'.CEP_ENTER.'" maxlength="" size="" name="no_cep" id="no_cep" value="">
</form>
<br>
<span id="status"> </span>';
}else{
$check_address_query = tep_db_query("select entry_postcode from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$customer_default_address_id . "'");
$check_address = tep_db_fetch_array($check_address_query);
$my_cep = $check_address['entry_postcode'];
}
}
require_once('frete.php');//remember to change the path to where you uploaded frete.php - would also be tidier if we used includes/filenames.php
$frete = new PgsFrete;
if($my_cep!='' & SHIPPING_ORIGIN_ZIP !='NONE'){$valorFrete = $frete->gerar(SHIPPING_ORIGIN_ZIP, $cart->weight, '', $my_cep);}
$sedex= $valorFrete['Sedex'];//gets the Sedex value - ignores PAC
$pac= $valorFrete['PAC'];
if($sedex!=''){
echo '<span>'.SHIPPING_SEDEX.' '.$my_cep.': R$'.$sedex.'<br></span>';
}
if($pac!=''){
echo '<span>'.SHIPPING_PAC.' '.$my_cep.': R$'.$pac.'</span>';
}
}
?>
</td>
</tr>
<tr>
<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
</tr>
<?php
5. Further down the page there will be a </form> tag. To avoid a nested <form> problem I’ve already moved this to inside to the top of the above code. You now need to find this lower </form> tag, cut it, and paste it back in a few lines above where it was located after this code:
<td align="right" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE) . '</a>'; ?></td>
So you get something like this:
<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
<td align="right" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE) . '</a>'; ?></form></td>
<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
6. Now go to the file includes/languages/YOUR LANGUAGE/shopping_cart.php and paste the following in just before the closing ?> Change the wording to suit your language.
//Pagseguro shipping
define('QUOTE_OBTAIN', 'To obtain a shipping quote please insert your CEP here in the format 12345-678');
define('CEP_ENTER', 'Enter your postcode');
define('SHIPPING_SEDEX', 'Shipping on this cart by Sedex to');
define('SHIPPING_PAC', 'Shipping on this cart by PAC to');
define('CEP_INVALID', 'Postcode not yet valid');
define('CEP_VALID', 'Postcode valid');
define('CEP_GO', 'Click to request quote');
That should be it – you’ll need to add style and layout to the output text though.
Need help with it? Ask me.
