Jump to content
Latest News: (loading..)
sinopia

Adding Custom Fields to Order Details

Recommended Posts

Hello,

I've been trying to add some custom fields to order details, more specific NIF (VAT Number).

Created in database: customers_nif in customers and billing_nif in orders.

In the creat_ accout.php it works fine and insert to db correctly.

Then in the checkout_payment.php it will get the current billing_nif value and ask if I want to use that or other. But then I doesn't insert in the db.

<?php
  $nif_query = tep_db_query("select customers_nif from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
  $nif_number = tep_db_fetch_array($nif_query);

  if ($nif_number['customers_nif'] != NULL) { 
      echo "Your VAT is: <strong>";
      echo $nif_number['customers_nif'];
      echo "</strong><br>Do you want to use it in the billing? ";

      echo tep_draw_checkbox_field('billing_nif', $nif_number['customers_nif'], 'class="input"');
  } else {
      echo "If you want VAT in your billing insert it here: <span class='inputRequirement'>Not required</span>";
      echo tep_draw_input_field('billing_nif','','class="input" maxlength="9"');
  }
?>

 

I've added: $nif = tep_db_prepare_input($_POST['billing_nif']); and also in order.php:

 $this->billing = array('name' => $order['billing_name'],
                             'company' => $order['billing_company'],
                             'billing_nif' => $order['billing_nif'], ....

 

Can someone please tell me what I'm missing? var_dump  in checkout_confirmation.php is:

array(5) {
  ["formid"]=>
  string(32) "f99ab3a7f36f9086ec2f640bfe65103f"
  ["billing_nif"]=>
  string(9) "123456789"
  ["payment"]=>
  string(3) "cod"
  ["discount_code"]=>
  string(0) ""
  ["comments"]=>
  string(0) ""
}

 

Share this post


Link to post
Share on other sites

Untested, but in checkout payment after

if (!tep_session_is_registered('comments')) tep_session_register('comments');
  if (isset($_POST['comments']) && tep_not_null($_POST['comments'])) {
    $comments = tep_db_prepare_input($_POST['comments']);
  }

add

if (!tep_session_is_registered('nif')) tep_session_register('nif');
  if (isset($_POST['nif']) && tep_not_null($_POST['nif'])) {
    $nif = tep_db_prepare_input($_POST['nif']);
  }

in includes/classes/order.php, change

function cart() {
      global $HTTP_POST_VARS, $customer_id, $sendto, $billto, $cart, $languages_id, $currency, $currencies, $shipping, $payment, $comments, $customer_default_address_id;

to

function cart() {
      global $_POST, $customer_id, $sendto, $billto, $cart, $languages_id, $currency, $currencies, $shipping, $payment, $comments, $customer_default_address_id, $nif;

change

$this->info = array('order_status' => DEFAULT_ORDERS_STATUS_ID,
                          'currency' => $currency,
                          'currency_value' => $currencies->currencies[$currency]['value'],
                          'payment_method' => $payment,
                          'cc_type' => '',
                          'cc_owner' => '',
                          'cc_number' => '',
                          'cc_expires' => '',
                          'shipping_method' => $shipping['title'],
                          'shipping_cost' => $shipping['cost'],
                          'subtotal' => 0,
                          'tax' => 0,
                          'tax_groups' => array(),
                          'comments' => (tep_session_is_registered('comments') && !empty($comments) ? $comments : ''));

to

$this->info = array('order_status' => DEFAULT_ORDERS_STATUS_ID,
                          'currency' => $currency,
                          'currency_value' => $currencies->currencies[$currency]['value'],
                          'payment_method' => $payment,
                          'cc_type' => '',
                          'cc_owner' => '',
                          'cc_number' => '',
                          'cc_expires' => '',
                          'shipping_method' => $shipping['title'],
                          'shipping_cost' => $shipping['cost'],
                          'subtotal' => 0,
                          'tax' => 0,
                          'tax_groups' => array(),
                          'comments' => (tep_session_is_registered('comments') && !empty($comments) ? $comments : ''),
                          'nif' => $nif);

 


Let's make things easier for new osCommerce users http://forums.oscommerce.com/topic/402638-discussion-about-hard-coded-database-tables/?p=1718900  Getting there with osCommerce 2.4! :thumbsup:

Share this post


Link to post
Share on other sites

Hi @frankl thanks for you answer.

I've tested but it's not working. I can't figure out what's missing. It basically get's the VAT in the registation (if user inserted one), and ask if it want to use in that order.

If yes it insert the current customer_nif from customers if not inserts NULL.. If in the registation the user didn't insert a VAT number in checkout_payment.php it asks if he want to use one for this order.

Structure of column: billing_nig VARCHAR 11 

Share this post


Link to post
Share on other sites

Hi,

Well @frankl I don't know why but this suddenly stopped to working. I've added a time ago a function to associate the vat number to customers db. It was working.. Only the orders aren't working. It insert a empty value to billing_nif.

Basically it's the same code all tep_db_prepare_input and orders.php are the same.

 

This is in checkout_payment.php

<?php 
//...
if (!tep_session_is_registered('billing_nif')) tep_session_register('billing_nif');
  if (isset($_POST['billing_nif']) && tep_not_null($_POST['billing_nif'])) {
    $nif = tep_db_prepare_input($_POST['billing_nif']); // NIF
  }
//...
?>
<div class="contentText" style="text-align: center;">
<?php
  // NIF
  $nif_query = tep_db_query("select customers_nif from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
  $nif_number = tep_db_fetch_array($nif_query);

  if (($nif_number['customers_nif'] != NULL) && ($nif_number['customers_nif'] != "")) { // check if it's on customers when registered/added in account
    echo "O NIF que tem registado na sua conta é: <strong>";
    echo $nif_number['customers_nif']; // works fine
    echo "</strong><br>Prentede o NIF na fatura? ";
    echo tep_draw_checkbox_field('billing_nif', $nif_number['customers_nif'], '', 'class="input"'); // here for orders <- don't work
  } else {
    echo "Insira abaixo o seu Número de Identificação Fiscal caso pretenda NIF na fatura: <span class='inputRequirement'>Facultativo</span><br>";
    echo tep_draw_input_field('billing_nif','','class="input" maxlength="9" style="width:30%"'); // here for orders <- don't work
    echo "<p id='associar_nif' style='display:none;'>Deseja associar o NIF na sua conta para próximas encomendas? ";
  echo tep_draw_checkbox_field('customer_nif', '', '', 'class="input" style="display:none"'); // associate into customers table <- this actually works
  echo "</p><p id='errmsg'></p>";
  }

?>
</div>

<script type="text/javascript">
$(document).ready(function () {
  $("input[name=billing_nif]").keypress(function (e) {
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        $("#errmsg").html("Apenas são permitidos dígitos").show().fadeOut("slow");
        return false;
    } else {
      $("p#associar_nif").show();
      $("input[name=customer_nif]").show();
    }
   });
    if ($(window).width() < 760 || $(window).height() < 760) {
    $("div#address_book").css({"float":"none"});
  }
});
</script>

This is on checkout_confirmation.php:

<?php
// NIF
  if (!tep_session_is_registered('billing_nif')) tep_session_register('billing_nif');
  if (isset($_POST['billing_nif']) && tep_not_null($_POST['billing_nif'])) {
    $nif = tep_db_prepare_input($_POST['billing_nif']); // NIF
  }

  if(isset($_POST["customer_nif"]) && !empty($_POST["billing_nif"])) {
      $associar_nif = tep_db_prepare_input($_POST["billing_nif"]);
      tep_db_query("UPDATE customers SET customers_nif = '" . tep_db_input($associar_nif) . "' WHERE customers_id = '" . (int)$customer_id . "' ");
    }
// END NIF
?>

checkout_process.php:

  $sql_data_array = array('customers_id' => $customer_id,
						  //.....
                          'billing_nif' => $order->info['billing_nif'], // NIF
						  //....

includes/classes/orders.php:

<?php
// Added billing_nif to sql query
      $order_query = tep_db_query("select customers_id, customers_name, customers_company, customers_street_address, customers_suburb, customers_city, customers_postcode, customers_state, customers_country, customers_telephone, customers_email_address, customers_address_format_id, delivery_name, delivery_company, delivery_street_address, delivery_suburb, delivery_city, delivery_postcode, delivery_state, delivery_country, delivery_address_format_id, billing_name, billing_company, billing_nif, billing_street_address, billing_suburb, billing_city, billing_postcode, billing_state, billing_country, billing_address_format_id, payment_method, cc_type, cc_owner, cc_number, cc_expires, currency, currency_value, date_purchased, orders_status, last_modified from " . TABLE_ORDERS . " where orders_id = '" . (int)$order_id . "'");
      $order = tep_db_fetch_array($order_query);

//....

      $this->billing = array('name' => $order['billing_name'],
                             'company' => $order['billing_company'],
                             'nif' => $order['billing_nif'],  // added here to use as order->billing['nif']
							  //.....
                             
//....
    function cart() {
      //global $HTTP_POST_VARS, $customer_id, $sendto, $billto, $cart, $languages_id, $currency, $currencies, $shipping, $payment, $comments, $customer_default_address_id;
      global $_POST, $customer_id, $sendto, $billto, $cart, $languages_id, $currency, $currencies, $shipping, $payment, $comments, $customer_default_address_id, $nif;
      
//....
      
      $this->info = array('order_status' => DEFAULT_ORDERS_STATUS_ID,
                          'currency' => $currency,
                          'billing_nif' => $nif, // NIF

//...
      $this->customer = array('firstname' => $customer_address['customers_firstname'],
                              'lastname' => $customer_address['customers_lastname'],
                              'company' => $customer_address['entry_company'],
                              'billing_nif' => $customer_address['billing_nif'], // NIF
                              //.....

?>
?>

The only thing it's not working in inserting it into orders, if anyone could help that would be awesome.

Share this post


Link to post
Share on other sites

Hi @wHiTeHaT thank for you answer. It initially worked.. When the customer register/alter account it can add the VAT number but when it's on the checkout_payment it shows if it's already on account and if customer want to use it in this order, else it asks if want to add a new one and associated it to the account. (it adds correctly but doesn't insert into orders).

I've tested it now but doesn't work, in checkout_confirmation.php if I var_dump the POST vars it shows the billing_nif so I think the problem is missing in checkout_process or classes/order.php

Share this post


Link to post
Share on other sites
    function query($order_id) {
      global $languages_id;

      $order_id = tep_db_prepare_input($order_id);

      $order_query = tep_db_query("select customers_id, customers_name, customers_company, customers_street_address, customers_suburb, customers_city, customers_postcode, customers_state, customers_country, customers_telephone, customers_email_address, customers_address_format_id, delivery_name, delivery_company, delivery_street_address, delivery_suburb, delivery_city, delivery_postcode, delivery_state, delivery_country, delivery_address_format_id, billing_name, billing_company, billing_nif, billing_street_address, billing_suburb, billing_city, billing_postcode, billing_state, billing_country, billing_address_format_id, payment_method, cc_type, cc_owner, cc_number, cc_expires, currency, currency_value, date_purchased, orders_status, last_modified from " . TABLE_ORDERS . " where orders_id = '" . (int)$order_id . "'");
      $order = tep_db_fetch_array($order_query);

This is on includes/classes/order.php -> In the query() I've added billing_nif (as it's on table orders)

      $this->billing = array('name' => $order['billing_name'],
                             'company' => $order['billing_company'],
                             'nif' => $order['billing_nif'],  // NIF
                             'street_address' => $order['billing_street_address'],
                             'suburb' => $order['billing_suburb'],
                             'city' => $order['billing_city'],
                             'postcode' => $order['billing_postcode'],
                             'state' => $order['billing_state'],
                             'country' => array('title' => $order['billing_country']),
                             'format_id' => $order['billing_address_format_id']);

I've added here to use order->billing['nif']

 

This is on checkout_process:

// load the before_process function from the payment modules
  $payment_modules->before_process();

  $sql_data_array = array('customers_id' => $customer_id,
// GUEST CHECKOUT START
                          'customers_guest' => (tep_session_is_registered('guest') ? 1 : 0),
// GUEST CHECKOUT END
                          'customers_name' => $order->customer['firstname'] . ' ' . $order->customer['lastname'],
                          'customers_company' => $order->customer['company'],
			  'billing_nif' => $order->billing['nif'], // NIF

I just can't figure it out the website is live and this is a bit needed otherwise clientes has to comment saying they want VAT in this order

Share this post


Link to post
Share on other sites

Another (and potentially simpler) way to do it;

checkout_success module
&
hook file to add the data into the admin/orders.php page  

You are done with no core code changes at all. 


This is a signature that appears on all my posts.  
IF YOU MAKE A POST REQUESTING HELP...please state the exact version
of osCommerce that you are using. THANKS

 
Get the latest current code (community-supported responsive 2.3.4.1BS Edge) here

 

Share this post


Link to post
Share on other sites

Well, I really don't know what is happening - this stopped again working.

@burt The idea is to see admin/orders.php and it showing if the client choice VAT in this order.

When a new client create a account it ask if it want to add VAT to his account.

In the checkout_payment (if it has inserted) will show the VAT number and asks if he want this order with VAT, otherwise it will be a input to write and checkbox if he want to associate it to the account.

The code for associate the account works well as it insert into table customers but in the orders every "billing_nif" is a empty space. Neither the VAT or NULL.

If someone could really take a lot into this I would be really appreciate as this site is going live soon. Just tell the files needed.

Share this post


Link to post
Share on other sites

In the checkout_confirmation.php is where it insert to customer table (which works fine):

  if (isset($HTTP_POST_VARS['customer_nif']) && !empty($HTTP_POST_VARS['billing_nif'])) {
      $associar_nif = tep_db_prepare_input($HTTP_POST_VARS['billing_nif']);
      tep_db_query("UPDATE customers SET customers_nif = '" . tep_db_input($associar_nif) . "' WHERE customers_id = '" . (int)$customer_id . "'");
  }

If I var_dump the $HTTP_POST_VAR I'm still getting the billing_nif in the array but in checkout_process.php and includes/classes/order.php doesn't insert it into orders and it's more easily right?

Share this post


Link to post
Share on other sites

Depending on your payment modules, orders may not be inserted in checkout_process but in the payment module.

If you already have it on customer, why do you need it on order anyway? If you really do, follow burt's suggestion and put it in checkout_success as an update to the order.


For a new install or if your store isn't mobile-friendly, get the community-supported responsive osCommerce (2.3.4.1 CE) here: https://github.com/gburton/Responsive-osCommerce/archive/2341-Frozen.zip

Working on generalising bespoke solutions for Quickbooks integration, Easify integration and pay4later (DEKO) integration at 2.3.x

Share this post


Link to post
Share on other sites

@BrockleyJohn the variable gets destroy on checkout_process.php (I think).

Created a new table to insert vat number and orders_id only. I can't insert it before because I can't get the orders_id and also can't get the variable.

Share this post


Link to post
Share on other sites

Have a look at the checkout success content module cm_cs_product_notifications

It accesses both customer_id which is still set for logged in customers and order_id which is already set to that customer's latest order. Then if they pay with paypal, worldpay etc you'll still get the info.


For a new install or if your store isn't mobile-friendly, get the community-supported responsive osCommerce (2.3.4.1 CE) here: https://github.com/gburton/Responsive-osCommerce/archive/2341-Frozen.zip

Working on generalising bespoke solutions for Quickbooks integration, Easify integration and pay4later (DEKO) integration at 2.3.x

Share this post


Link to post
Share on other sites

@BrockleyJohn I've take a look but I only can't get the input:

tep_draw_input_field('billing_nif','','class="input" maxlength="9" style="width:30%"');

On checkout_confirmation if I var_dump the HTTP_POST_VARS I still get it but then it get destroyed even if in checkout_process.php I comment this -> tep_session_unregister('billing_nif');

 

I used this:  

if (!tep_session_is_registered('billing_nif')) tep_session_register('billing_nif');
  if (isset($HTTP_POST_VARS['billing_nif']) && tep_not_null($HTTP_POST_VARS['billing_nif'])) {
    $nif = tep_db_prepare_input($HTTP_POST_VARS['billing_nif']); // NIF
  }

And now to force to insert (even in a new table) I used this: 

  if (isset($nif)) {
    tep_db_query("INSERT INTO nif (orders_id, billing_nif) VALUES ('" . $insert_id . "', '" . tep_db_input($nif) . "');");
  }

But the $nif isn't set in the checkout_process/checkout_success so doesn't work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×