Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

How to pass country_id!


bnotions

Recommended Posts

Hello all:

 

I'm hoping that someone can help me out with a little problem I am having. I have installed the "Per item shipping" module. What I am trying to do is pass the "country_id" of the shipping address to "shopping_cart.php" within "/catalog/includes/classes/".

 

I want to charge a specific amount for shipping to specific countries, based on the per item amount. Here is my hack to the "Per item shipping":

 

// mod indvship

// Country hack for Ind. Shipping

if ($order->delivery['country_id'] == '223'){ // U.S. Shipping

$this->shiptotal += ($products_ship_price * $qty);

}

elseif ($order->delivery['country_id'] == '38'){// Canada Shipping

$this->shiptotal += ($products_ship_price * $qty) + (($products_price * $qty) * .047);

}

else { // All other countries

$this->shiptotal += ($products_ship_price * $qty) + (($products_price * $qty) * .075);

}

// end Country hack for Ind. Shipping

// end indvship

 

$order->delivery['country_id'] doesn't seem to be the right call. Can anyone assist with this? I would appreciate it greatly!

 

Thanks,

Briant

http://www.bnotions.com

Thanks,

Briant

Link to comment
Share on other sites

Around line 138 of includes/classes/order.php is

      $customer_address_query = tep_db_query("select c.customers_firstname, c.customers_lastname, c.customers_telephone, c.customers_email_address, ab.entry_company, ab.entry_street_address, ab.entry_suburb, ab.entry_postcode, ab.entry_city, ab.entry_zone_id, z.zone_name, co.countries_id, co.countries_name, co.countries_iso_code_2, co.countries_iso_code_3, co.address_format_id, ab.entry_state from " . TABLE_CUSTOMERS . " c, " . TABLE_ADDRESS_BOOK . " ab left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id) left join " . TABLE_COUNTRIES . " co on (ab.entry_country_id = co.countries_id) where c.customers_id = '" . (int)$customer_id . "' and ab.customers_id = '" . (int)$customer_id . "' and c.customers_default_address_id = ab.address_book_id");

I.e. countries based on the entry_country_id of address book.

 

Hth,

Matt

Link to comment
Share on other sites

That's what I thought. For some reason it is just defaulting to the else statement. The country id for the US is 223 on my database. So it should run the code for the US.

 

Do you have any suggestions as to what I may be doing wrong. Here is my code:

 

<?php

/*

$Id: shopping_cart.php,v 1.3.2.6 2003/05/12 23:11:20 wilt Exp $

orig : shopping_cart.php,v 1.32 2003/02/11 00:04:53 hpdl Exp $

 

osCommerce, Open Source E-Commerce Solutions

http://www.oscommerce.com

 

Copyright ? 2003 osCommerce

 

Released under the GNU General Public License

*/

 

class shoppingCart {

var $contents, $total, $weight, $cartID, $content_type;

// mod indvship

var $shiptotal;

// end indvship

function shoppingCart() {

$this->reset();

}

 

function restore_contents() {

global $customer_id, $gv_id, $REMOTE_ADDR;

 

if (!tep_session_is_registered('customer_id')) return false;

 

// insert current cart contents in database

if (is_array($this->contents)) {

reset($this->contents);

while (list($products_id, ) = each($this->contents)) {

$qty = $this->contents[$products_id]['qty'];

$product_query = tep_db_query("select products_id from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");

if (!tep_db_num_rows($product_query)) {

tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . $customer_id . "', '" . $products_id . "', '" . $qty . "', '" . date('Ymd') . "')");

if (isset($this->contents[$products_id]['attributes'])) {

reset($this->contents[$products_id]['attributes']);

while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {

tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . $customer_id . "', '" . $products_id . "', '" . $option . "', '" . $value . "')");

}

}

} else {

tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $qty . "' where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");

}

}

//ICW ADDDED FOR CREDIT CLASS GV - START

if (tep_session_is_registered('gv_id')) {

$gv_query = tep_db_query("insert into " . TABLE_COUPON_REDEEM_TRACK . " (coupon_id, customer_id, redeem_date, redeem_ip) values ('" . $gv_id . "', '" . $customer_id . "', now(),'" . $REMOTE_ADDR . "')");

$gv_update = tep_db_query("update " . TABLE_COUPONS . " set coupon_active = 'N' where coupon_id = '" . $gv_id . "'");

tep_gv_account_update($customer_id, $gv_id);

tep_session_unregister('gv_id');

}

//ICW ADDDED FOR CREDIT CLASS GV - END

}

 

// reset per-session cart contents, but not the database contents

$this->reset(false);

 

$products_query = tep_db_query("select products_id, customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "'");

while ($products = tep_db_fetch_array($products_query)) {

$this->contents[$products['products_id']] = array('qty' => $products['customers_basket_quantity']);

// attributes

$attributes_query = tep_db_query("select products_options_id, products_options_value_id from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $customer_id . "' and products_id = '" . $products['products_id'] . "'");

while ($attributes = tep_db_fetch_array($attributes_query)) {

$this->contents[$products['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id'];

}

}

 

$this->cleanup();

}

 

function reset($reset_database = false) {

global $customer_id;

 

$this->contents = array();

$this->total = 0;

$this->weight = 0;

$this->content_type = false;

// mod indvship

$this->shiptotal = 0;

// end indvship

 

if (tep_session_is_registered('customer_id') && ($reset_database == true)) {

tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "'");

tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $customer_id . "'");

}

 

unset($this->cartID);

if (tep_session_is_registered('cartID')) tep_session_unregister('cartID');

}

 

function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {

global $new_products_id_in_cart, $customer_id;

 

$products_id = tep_get_uprid($products_id, $attributes);

if ($notify == true) {

$new_products_id_in_cart = $products_id;

tep_session_register('new_products_id_in_cart');

}

 

if ($this->in_cart($products_id)) {

$this->update_quantity($products_id, $qty, $attributes);

} else {

$this->contents[] = array($products_id);

$this->contents[$products_id] = array('qty' => $qty);

// insert into database

if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . $customer_id . "', '" . $products_id . "', '" . $qty . "', '" . date('Ymd') . "')");

 

if (is_array($attributes)) {

reset($attributes);

while (list($option, $value) = each($attributes)) {

$this->contents[$products_id]['attributes'][$option] = $value;

// insert into database

if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . $customer_id . "', '" . $products_id . "', '" . $option . "', '" . $value . "')");

}

}

}

$this->cleanup();

 

// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure

$this->cartID = $this->generate_cart_id();

}

 

function update_quantity($products_id, $quantity = '', $attributes = '') {

global $customer_id;

 

if (empty($quantity)) return true; // nothing needs to be updated if theres no quantity, so we return true..

 

$this->contents[$products_id] = array('qty' => $quantity);

// update database

if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $quantity . "' where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");

 

if (is_array($attributes)) {

reset($attributes);

while (list($option, $value) = each($attributes)) {

$this->contents[$products_id]['attributes'][$option] = $value;

// update database

if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . $value . "' where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "' and products_options_id = '" . $option . "'");

}

}

}

 

function cleanup() {

global $customer_id;

 

reset($this->contents);

while (list($key,) = each($this->contents)) {

if ($this->contents[$key]['qty'] < 1) {

unset($this->contents[$key]);

// remove from database

if (tep_session_is_registered('customer_id')) {

tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "' and products_id = '" . $key . "'");

tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $customer_id . "' and products_id = '" . $key . "'");

}

}

}

}

 

function count_contents() { // get total number of items in cart

$total_items = 0;

if (is_array($this->contents)) {

reset($this->contents);

while (list($products_id, ) = each($this->contents)) {

$total_items += $this->get_quantity($products_id);

}

}

 

return $total_items;

}

 

function get_quantity($products_id) {

if (isset($this->contents[$products_id])) {

return $this->contents[$products_id]['qty'];

} else {

return 0;

}

}

 

function in_cart($products_id) {

if (isset($this->contents[$products_id])) {

return true;

} else {

return false;

}

}

 

function remove($products_id) {

global $customer_id;

 

unset($this->contents[$products_id]);

// remove from database

if (tep_session_is_registered('customer_id')) {

tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");

tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");

}

 

// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure

$this->cartID = $this->generate_cart_id();

}

 

function remove_all() {

$this->reset();

}

 

function get_product_id_list() {

$product_id_list = '';

if (is_array($this->contents)) {

reset($this->contents);

while (list($products_id, ) = each($this->contents)) {

$product_id_list .= ', ' . $products_id;

}

}

 

return substr($product_id_list, 2);

}

 

function calculate() {

$this->total_virtual = 0; // Gift Voucher System

$this->total = 0;

$this->weight = 0;

// mod indvship

$this->shiptotal = 0;

// end indvship

if (!is_array($this->contents)) return 0;

 

reset($this->contents);

while (list($products_id, ) = each($this->contents)) {

$qty = $this->contents[$products_id]['qty'];

 

// products price

$product_query = tep_db_query("select products_id, products_price, products_ship_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id='" . tep_get_prid($products_id) . "'");

if ($product = tep_db_fetch_array($product_query)) {

// ICW ORDER TOTAL CREDIT CLASS Start Amendment

$no_count = 1;

$gv_query = tep_db_query("select products_model from " . TABLE_PRODUCTS . " where products_id = '" . $products_id . "'");

$gv_result = tep_db_fetch_array($gv_query);

if (ereg('^GIFT', $gv_result['products_model'])) {

$no_count = 0;

}

// ICW ORDER TOTAL CREDIT CLASS End Amendment

$prid = $product['products_id'];

$products_tax = tep_get_tax_rate($product['products_tax_class_id']);

$products_price = $product['products_price'];

$products_weight = $product['products_weight'];

// mod indvship

$products_ship_price = $product['products_ship_price'];

// end indvship

 

$specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . $prid . "' and status = '1'");

if (tep_db_num_rows ($specials_query)) {

$specials = tep_db_fetch_array($specials_query);

$products_price = $specials['specials_new_products_price'];

}

 

$this->total_virtual += tep_add_tax($products_price, $products_tax) * $qty * $no_count;// CREDIT CLASS;

$this->weight_virtual += ($qty * $products_weight) * $no_count;// CREDIT CLASS;

$this->total += tep_add_tax($products_price, $products_tax) * $qty;

// mod indvship

// Country hack for Ind. Shipping

if (($order->delivery['country']['id'] == '223'){ // U.S. Shipping

$this->shiptotal += ($products_ship_price * $qty);

}

elseif ($order->delivery['country']['id'] == '38'){ // Canada Shipping

$this->shiptotal += ($products_ship_price * $qty) + (($products_price * $qty) * .047);

}

else { // All other countries

$this->shiptotal += ($products_ship_price * $qty) + (($products_price * $qty) * .075);

}

// end Country hack for Ind. Shipping

// end indvship

 

// mod indvship

// Country hack for Ind. Shipping

// if $country == 'United States'{

// $this->shiptotal += ($products_ship_price * $qty);

// }

// end Country hack for Ind. Shipping

// end indvship

$this->weight += ($qty * $products_weight);

}

 

// attributes price

if (isset($this->contents[$products_id]['attributes'])) {

reset($this->contents[$products_id]['attributes']);

while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {

$attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . $prid . "' and options_id = '" . $option . "' and options_values_id = '" . $value . "'");

$attribute_price = tep_db_fetch_array($attribute_price_query);

if ($attribute_price['price_prefix'] == '+') {

$this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);

} else {

$this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);

}

}

}

}

}

 

function attributes_price($products_id) {

if (isset($this->contents[$products_id]['attributes'])) {

reset($this->contents[$products_id]['attributes']);

while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {

$attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . $products_id . "' and options_id = '" . $option . "' and options_values_id = '" . $value . "'");

$attribute_price = tep_db_fetch_array($attribute_price_query);

if ($attribute_price['price_prefix'] == '+') {

$attributes_price += $attribute_price['options_values_price'];

} else {

$attributes_price -= $attribute_price['options_values_price'];

}

}

}

 

return $attributes_price;

}

 

function get_products() {

global $languages_id;

 

if (!is_array($this->contents)) return false;

 

$products_array = array();

reset($this->contents);

while (list($products_id, ) = each($this->contents)) {

$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_price, p.products_weight, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id='" . tep_get_prid($products_id) . "' and pd.products_id = p.products_id and pd.language_id = '" . $languages_id . "'");

if ($products = tep_db_fetch_array($products_query)) {

$prid = $products['products_id'];

$products_price = $products['products_price'];

 

$specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . $prid . "' and status = '1'");

if (tep_db_num_rows($specials_query)) {

$specials = tep_db_fetch_array($specials_query);

$products_price = $specials['specials_new_products_price'];

}

 

$products_array[] = array('id' => $products_id,

'name' => $products['products_name'],

'model' => $products['products_model'],

'price' => $products_price,

'quantity' => $this->contents[$products_id]['qty'],

'weight' => $products['products_weight'],

'final_price' => ($products_price + $this->attributes_price($products_id)),

'tax_class_id' => $products['products_tax_class_id'],

'attributes' => $this->contents[$products_id]['attributes']);

}

}

 

return $products_array;

}

 

function show_total() {

$this->calculate();

 

return $this->total;

}

// mod indvship

function get_shiptotal() {

$this->calculate();

 

return $this->shiptotal;

}

// end indvship

 

function show_weight() {

$this->calculate();

 

return $this->weight;

}

// CREDIT CLASS Start Amendment

function show_total_virtual() {

$this->calculate();

 

return $this->total_virtual;

}

 

function show_weight_virtual() {

$this->calculate();

 

return $this->weight_virtual;

}

// CREDIT CLASS End Amendment

 

function generate_cart_id($length = 5) {

return tep_create_random_value($length, 'digits');

}

 

function get_content_type() {

$this->content_type = false;

 

// if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) {

if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) || ($this->show_weight() == 0 )&& ($this->count_contents() > 0) ) {

reset($this->contents);

while (list($products_id, ) = each($this->contents)) {

if (isset($this->contents[$products_id]['attributes'])) {

reset($this->contents[$products_id]['attributes']);

while (list(, $value) = each($this->contents[$products_id]['attributes'])) {

$virtual_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad where pa.products_id = '" . $products_id . "' and pa.options_values_id = '" . $value . "' and pa.products_attributes_id = pad.products_attributes_id");

$virtual_check = tep_db_fetch_array($virtual_check_query);

 

if ($virtual_check['total'] > 0) {

switch ($this->content_type) {

case 'physical':

$this->content_type = 'mixed';

 

return $this->content_type;

break;

default:

$this->content_type = 'virtual';

break;

}

} else {

switch ($this->content_type) {

case 'virtual':

$this->content_type = 'mixed';

 

return $this->content_type;

break;

default:

$this->content_type = 'physical';

break;

}

}

}

// ICW ADDED CREDIT CLASS - Begin

} elseif ($this->show_weight() == 0) {

reset($this->contents);

while (list($products_id, ) = each($this->contents)) {

$virtual_check_query = tep_db_query("select products_weight from " . TABLE_PRODUCTS . " where products_id = '" . $products_id . "'");

$virtual_check = tep_db_fetch_array($virtual_check_query);

if ($virtual_check['products_weight'] == 0) {

switch ($this->content_type) {

case 'physical':

$this->content_type = 'mixed';

 

return $this->content_type;

break;

default:

$this->content_type = 'virtual_weight';

break;

}

} else {

switch ($this->content_type) {

case 'virtual':

$this->content_type = 'mixed';

 

return $this->content_type;

break;

default:

$this->content_type = 'physical';

break;

}

}

}

// ICW ADDED CREDIT CLASS - End

} else {

switch ($this->content_type) {

case 'virtual':

$this->content_type = 'mixed';

 

return $this->content_type;

break;

default:

$this->content_type = 'physical';

break;

}

}

}

} else {

$this->content_type = 'physical';

}

//echo $this->content_type . '<br>';

 

return $this->content_type;

}

 

function unserialize($broken) {

for(reset($broken);$kv=each($broken);) {

$key=$kv['key'];

if (gettype($this->$key)!="user function")

$this->$key=$kv['value'];

}

}

 

// ------------------------ ICWILSON CREDIT CLASS Gift Voucher Addittion-------------------------------Start

// amend count_contents to show nil contents for shipping

// as we don't want to quote for 'virtual' item

// GLOBAL CONSTANTS if NO_COUNT_ZERO_WEIGHT is true then we don't count any product with a weight

// which is less than or equal to MINIMUM_WEIGHT

// otherwise we just don't count gift certificates

 

function count_contents_virtual() { // get total number of items in cart disregard gift vouchers

$total_items = 0;

if (is_array($this->contents)) {

reset($this->contents);

while (list($products_id, ) = each($this->contents)) {

$no_count = false;

$gv_query = tep_db_query("select products_model from " . TABLE_PRODUCTS . " where products_id = '" . $products_id . "'");

$gv_result = tep_db_fetch_array($gv_query);

if (ereg('^GIFT', $gv_result['products_model'])) {

$no_count=true;

}

if (NO_COUNT_ZERO_WEIGHT == 1) {

$gv_query = tep_db_query("select products_weight from " . TABLE_PRODUCTS . " where products_id = '" . tep_get_prid($products_id) . "'");

$gv_result=tep_db_fetch_array($gv_query);

if ($gv_result['products_weight']<=MINIMUM_WEIGHT) {

$no_count=true;

}

}

if (!$no_count) $total_items += $this->get_quantity($products_id);

}

}

return $total_items;

}

// ------------------------ ICWILSON CREDIT CLASS Gift Voucher Addittion-------------------------------End

}

?>

Thanks,

Briant

Link to comment
Share on other sites

Oh, if this is in the shopping_cart, it probably hasn't made the order yet (the order is made from the shopping cart). Sorry, I must have missed that previously. Try

if (tep_session_is_registered('customers_id')) {
 if (tep_session_is_registered('sendto')) {
   $country_query = tep_db_query("select entry_country_id as id from " . TABLE_ADDRESS_BOOK . " where address_book_id = '" . (int)$sendto . "'");
 } else {
   $country_query = tep_db_query("select ab.entry_country_id as id from " . TABLE_ADDRESS_BOOK . " ab, " . TABLE_COUNTRY . " c where ab.address_book_id = c.default_address_id and c.customers_id = '" . (int)$customers_id . "'");
 }
 $country_array = tep_db_fetch_array($country_query);
 if ($country_array['id'] == '223) {
/* so on and so forth */
} else { // continues if (tep_session_is_registered('customers_id')) {
 $this->shiptotal = "Log in to see shipping costs.";
}

Note: can't alter shipping by country until customer logs in so we know where it is going.

 

Hth,

Matt

Link to comment
Share on other sites

It's in shopping_cart, but it's in ""/catalog/includes/classes/shopping_cart.php", not "/catalog/shopping_cart.php".

 

So the code killed the script.

 

By the way, thanks for taking the time to help me.

Thanks,

Briant

Link to comment
Share on other sites

Oh, if this is in the shopping_cart, it probably hasn't made the order yet (the order is made from the shopping cart). Sorry, I must have missed that previously. Try
if (tep_session_is_registered('customers_id')) {
 if (tep_session_is_registered('sendto')) {
   $country_query = tep_db_query("select entry_country_id as id from " . TABLE_ADDRESS_BOOK . " where address_book_id = '" . (int)$sendto . "'");
 } else {
   $country_query = tep_db_query("select ab.entry_country_id as id from " . TABLE_ADDRESS_BOOK . " ab, " . TABLE_COUNTRY . " c where ab.address_book_id = c.default_address_id and c.customers_id = '" . (int)$customers_id . "'");
 }
 $country_array = tep_db_fetch_array($country_query);
 if ($country_array['id'] == '223) {
/* so on and so forth */
} else { // continues if (tep_session_is_registered('customers_id')) {
 $this->shiptotal = "Log in to see shipping costs.";
}

Note: can't alter shipping by country until customer logs in so we know where it is going.

 

Hth,

Matt

Matt,

I have read through the above, but could you point out exactly where to insert the piece of code you suggested? I put it (I thought) where it belonged, and got nothing but errors. Mucho gracias!

 

Yikes!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...