News:

Looking for documentation? Take a look on our wiki

Main Menu

Bill To and Ship To Address layout on order emails.

Started by WebStuff, August 14, 2013, 13:40:45 PM

Previous topic - Next topic

WebStuff

I've made a template override for the components/com_virtuemart/invoice/mail_html_shopperaddresses.php and pdf invoice file components/com_virtuemart/invoice/invoice_order.php which allows for the re-arrangement of the fields sequence on the emails compared to the sequence that a shopper inputs them on the registration form or to not display certain fields.
This is similar to the old address layout options in VM1. At the moment this only works on the customer Bill To and Ship To addresses but I think it should be incorporated in the renderVendorAddress and parent functions.

I did this as I have a system that finds the customer address from the postcode so I wanted the postcode first in the registration form but obviously that looks strange when listed out in an email or invoice. So I came up with this. It may not be the most efficient way of doing it but it does work for me so I thought I'd share it.

Here's the mail_html_shopperaddresses.php code:<?php
/**
 *
 * Layout for the order email
 * shows the chosen adresses of the shopper
 * taken from the stored order
 *
 * @package VirtueMart
 * @subpackage Order
 * @author Max Milbers,   Valerie Isaksen
 *
 * @link http://www.virtuemart.net
 * @copyright Copyright (c) 2004 - 2010 VirtueMart Team. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * VirtueMart is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 *
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
$addressBTOrder = array('email{br}''company{br}''title''first_name''last_name{br}''address_1{br}''address_2{br}''city{br}''county{br}''zip{br}''virtuemart_country_id{br}''phone_1{br}''phone_2{br}');
$addressSTOrder = array('company{br}''title''first_name''last_name{br}''address_1{br}''address_2{br}''city{br}''county{br}''zip{br}''virtuemart_country_id{br}''phone_1{br}''phone_2{br}');
?>

<table class="html-email" cellspacing="0" cellpadding="0" border="0" width="100%">
  <tr  >
<th width="50%">
<?php echo JText::_('COM_VIRTUEMART_USER_FORM_BILLTO_LBL'); ?>
</th>
<th width="50%" >
<?php echo JText::_('COM_VIRTUEMART_USER_FORM_SHIPTO_LBL'); ?>
</th>
</tr>
<tr>
<td valign="top" width="50%">
<?php
foreach ($addressBTOrder as $fieldname) {
$fieldinfo explode('{',$fieldname);
if (!empty($this->userfields['fields'][$fieldinfo[0]]['value'])) { ?>

<span class="values vm2<?php echo '-' $this->userfields['fields'][$fieldinfo[0]]['name'?>" ><?php echo $this->escape($this->userfields['fields'][$fieldinfo[0]]['value']) ?></span> <?php
if (isset($fieldinfo[1]) && $fieldinfo[1] == 'br}') { ?>

<br class="clear" /> <?php
}
}
}
?>

</td>
<td valign="top" width="50%">
<?php
foreach ($addressSTOrder as $fieldname) {
$fieldinfo explode('{',$fieldname);
if (!empty($this->shipmentfields['fields'][$fieldinfo[0]]['value'])) { ?>

<span class="values vm2<?php echo '-' $this->shipmentfields['fields'][$fieldinfo[0]]['name'?>" ><?php echo $this->escape($this->shipmentfields['fields'][$fieldinfo[0]]['value']) ?></span> <?php
if (isset($fieldinfo[1]) && $fieldinfo[1] == 'br}') { ?>

<br class="clear" /> <?php
}
}
}
?>

</td>
</tr>
</table>

The two arrays at the top:
$addressBTOrder = array('email{br}', 'company{br}', 'title', 'first_name', 'last_name{br}', 'address_1{br}', 'address_2{br}', 'city{br}', 'county{br}', 'zip{br}', 'virtuemart_country_id{br}', 'phone_1{br}', 'phone_2{br}');
$addressSTOrder = array('company{br}', 'title', 'first_name', 'last_name{br}', 'address_1{br}', 'address_2{br}', 'city{br}', 'county{br}', 'zip{br}', 'virtuemart_country_id{br}', 'phone_1{br}', 'phone_2{br}');
hold the sequence information. Hopefully it's fairly self explanatory but basically you list the order you want the fields to appear plus if you want a break at the end of it, in which case put a  {br} at the end of that field name. If you want an extra break just put a {br} on it's own in single quotes without a field_name.
invoice_order.php adds "breaks" between fields anyway as each field is displayed in a row and cell of a table but you can add extra breaks if you wish to space out the information.

Here is the invoice_order.php code:<?php
/**
 *
 * Order detail view
 *
 * @package VirtueMart
 * @subpackage Orders
 * @author Oscar van Eijk, Valerie Isaksen
 * @link http://www.virtuemart.net
 * @copyright Copyright (c) 2004 - 2010 VirtueMart Team. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * VirtueMart is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * @version $Id: details_order.php 5341 2012-01-31 07:43:24Z alatak $
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

$addressBTOrder = array('email''company''title''first_name''last_name''address_1''address_2''city''county''zip''virtuemart_country_id''{br}''phone_1''phone_2{br}');
$addressSTOrder = array('company''title''first_name''last_name''address_1''address_2''city''county''zip''virtuemart_country_id''{br}''phone_1''phone_2{br}');
?>

<?php if ($this->invoiceNumber) { ?>
<h1><?php echo JText::_('COM_VIRTUEMART_INVOICE').' '.$this->invoiceNumber?> </h1>
<?php ?>

<table width="100%" cellspacing="0" cellpadding="0" border="0">
<?php if ($this->invoiceNumber) { ?>
    <tr>
<td class=""><?php echo JText::_('COM_VIRTUEMART_INVOICE_DATE'?></td>
<td align="left"><?php echo vmJsApi::date($this->invoiceDate'LC4'true); ?></td>
    </tr>
    <?php ?>
    <tr>
<td ><?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_PO_NUMBER'?></td>
<td align="left"><strong>
    <?php echo $this->orderDetails['details']['BT']->order_number?>
</strong>
</td>
    </tr>

    <tr>
<td class=""><?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_PO_DATE'?></td>
<td align="left"><?php echo vmJsApi::date($this->orderDetails['details']['BT']->created_on'LC4'true); ?></td>
    </tr>
    <tr>
<td class=""><?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_PO_STATUS'?></td>
<td align="left"><?php echo $this->orderstatuses[$this->orderDetails['details']['BT']->order_status]; ?></td>
    </tr>
  <tr>
<td class=""><?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_SHIPMENT_LBL'?></td>
<td align="left"><?php
    echo 
$this->orderDetails['shipmentName'];
    ?>
</td>
    </tr>
    <tr>
<td class=""><?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_PAYMENT_LBL'?></td>
<td align="left"><?php echo $this->orderDetails['paymentName']; ?>
</td>
    </tr>
<?php if ($this->orderDetails['details']['BT']->customer_note) { ?>
<tr>
    <td><?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_CUSTOMER_NOTE'?></td>
    <td valign="top" align="left" width="50%"><?php echo $this->orderDetails['details']['BT']->customer_note?></td>
</tr>
<?php ?>

<tr>
<td class="orders-key"><strong><?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_TOTAL'?></strong></td>
<td class="orders-key" align="left"><strong><?php echo $this->currency->priceDisplay($this->orderDetails['details']['BT']->order_total); ?></strong></td>
</tr>

<tr>
<td colspan="2"></td>
</tr>
<tr>
<td valign="top"><strong>
<?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_BILL_TO_LBL'?></strong> <br/>
<table border="0">
<?php
foreach ($addressBTOrder as $fieldname) {
$fieldinfo explode('{',$fieldname);
if (!empty($this->userfields['fields'][$fieldinfo[0]]['value'])) {
echo '<tr><td class="key">' $this->userfields['fields'][$fieldinfo[0]]['title'] . '</td>'
'<td>' $this->escape($this->userfields['fields'][$fieldinfo[0]]['value']) . '</td></tr>';
if (isset($fieldinfo[1]) && $fieldinfo[1] == 'br}') { ?>

<br class="clear" /> <?php
}
}
}
?>

</table>
</td>
<td valign="top" ><strong>
<?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_SHIP_TO_LBL'?></strong><br/>
<table border="0">
<?php
foreach ($addressBTOrder as $fieldname) {
$fieldinfo explode('{',$fieldname);
if (!empty($this->shipmentfields['fields'][$fieldinfo[0]]['value'])) {
echo '<tr><td class="key">' $this->shipmentfields['fields'][$fieldinfo[0]]['title'] . '</td>'
'<td>' $this->escape($this->shipmentfields['fields'][$fieldinfo[0]]['value']) . '</td></tr>';
if (isset($fieldinfo[1]) && $fieldinfo[1] == 'br}') { ?>

<br class="clear" /> <?php
}
}
}
?>

</table>
</td>
    </tr>
</table>


I'm sure this could be made into a plugin by somebody with a bit more time than me. It would also be nice to have this configurable in one of the Shop->Shop-> [Tabs] or Configuration->Configuration->Templates options.

Hope this is helpful to someone.

Robert_ITMan

Brilliant! Thanks a ton for sharing this! Your sweet code saved me hours doing this myself - I was about to add each field ... but your way is so much faster and results are exactly what I needed.

FYI - Only change I made was to add &nbsp; instead of blank space after the </span> so I have spaces now between values.
WEBSITES @ OURFINGERTIPS
manage > develop > market > repeat

Save a lot of time and money when focused on building a website that works with marketing efforts to get more leads and sales from visitors.

www.ourfingertips.com

AH

Nice solution to a problem that some people may come across.

:)
Regards
A

Joomla 3.10.11
php 8.0

bryanearl

I know this is old, but it is specifically relevant to what I am doing...

I modified the above code to re-order my address displays in the newest version of VM. It works!

However, I need to add a comma after "city" in the final output for the email. If I add a comma to the array like this:

'city,'

... it doesn't work and the city doesn't display at all. I also tried the hex value for comma (%2c) but it didn't work either.

What would I add in the array after city to add a comma?

GJC Web Design

{,} 

and then add in the loop

if (isset($fieldinfo[1]) && $fieldinfo[1] == ',}') { ?>
                  , <?php
               }

maybe - haven't tried it - but will be something like this
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

bryanearl

Quote from: GJC Web Design on June 15, 2018, 10:50:01 AM
maybe - haven't tried it - but will be something like this

Thanks, I ended up doing it a bit differently, but I was able to get the address output how I wanted it like this:

Firstname Lastname
P.O. Box 999999
Chino Valley, Arizona 86323
928-555-1212


Here is what I did to the email file (mail_html_shopperaddresses.php) as an example:

<?php
/**
 *
 * Layout for the order email
 * shows the chosen adresses of the shopper
 * taken from the stored order
 *
 * @package VirtueMart
 * @subpackage Order
 * @author Max Milbers,   Valerie Isaksen
 *
 * @link https://virtuemart.net
 * @copyright Copyright (c) 2004 - 2010 VirtueMart Team. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * VirtueMart is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 *
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

$br '<br class="clear" />';
$addressBTOrder = [[
    
'key' => 'email',
    
'suffix' => $br
], [
    
'key' => 'company',
    
'suffix' => $br
], [
    
'key' => 'title',
    
'suffix' => ''
], [
    
'key' => 'first_name',
    
'suffix' => ''
], [
    
'key' => 'last_name',
    
'suffix' => $br
], [
    
'key' => 'address_1',
    
'suffix' => $br
], [
    
'key' => 'address_2',
    
'suffix' => $br
], [
    
'key' => 'city',
    
'suffix' => ','
], [
    
'key' => 'virtuemart_state_id',
    
'suffix' => ''
], [
    
'key' => 'zip',
    
'suffix' => $br
], [
    
'key' => 'virtuemart_country_id',
    
'suffix' => $br
], [
    
'key' => 'phone_1',
    
'suffix' => $br
], [
    
'key' => 'phone_2',
    
'suffix' => $br
]];

$addressSTOrder = [[
    
'key' => 'company',
    
'suffix' => $br
], [
    
'key' => 'title',
    
'suffix' => ''
], [
    
'key' => 'first_name',
    
'suffix' => ''
], [
    
'key' => 'last_name',
    
'suffix' => $br
], [
    
'key' => 'address_1',
    
'suffix' => $br
], [
    
'key' => 'address_2',
    
'suffix' => $br
], [
    
'key' => 'city',
    
'suffix' => ','
], [
    
'key' => 'virtuemart_state_id',
    
'suffix' => ''
], [
    
'key' => 'zip',
    
'suffix' => $br
], [
    
'key' => 'virtuemart_country_id',
    
'suffix' => $br
], [
    
'key' => 'phone_1',
    
'suffix' => $br
], [
    
'key' => 'phone_2',
    
'suffix' => $br
]];


?>

<table class="html-email" cellspacing="0" cellpadding="5" border="0" width="100%" style="border-collapse: collapse; font-family: Arial, Helvetica, sans-serif; font-size: 12px; margin: 0 auto;">
<tr>
<th width="50%" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;">
    <?php echo vmText::_('COM_VIRTUEMART_USER_FORM_BILLTO_LBL'); ?>
</th>
<th width="50%" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;">
    <?php echo vmText::_('COM_VIRTUEMART_USER_FORM_SHIPTO_LBL'); ?>
</th>
    </tr>
    <tr>
<td valign="top" width="50%" style="border: 1px solid #CCCCCC;">

<?php foreach ($addressBTOrder as $field) :
$userField $this->userfields['fields'][$field['key']];
if (!empty($userField['value'])) : ?>

<span class="values vm2-<?= $userField['name'] ?>">
  <?= $this->escape($userField['value']) ?><?= $field['suffix'] ?>
</span>
<?php endif; endforeach ?>

</td>
<td valign="top" width="50%" style="border: 1px solid #CCCCCC;">
    <?php if(!empty($this->orderDetails['details']['has_ST'])){
foreach ($addressSTOrder as $field) :
$userField $this->shipmentfields['fields'][$field['key']];
if (!empty($userField['value'])) : ?>

<span class="values vm2-<?= $userField['name'] ?>">
  <?= $this->escape($userField['value']) ?><?= $field['suffix'] ?>
</span> <?php endif; endforeach ?>
<br class="clear" /> <?php
} else {
echo "<br/>".vmText::_('COM_VM_ST_SAME_AS_BT');
}
?>

</td>
    </tr>
<tr><td valign="top" width="50%" style="padding:5px"></td><td valign="top" width="50%" style="padding:5px"></td></tr>
</table>



stresskills

Thank you very much for sharing! That saved me some time :)
Can we use the abbreviated State instead? CA instead of California for example?

Quote from: bryanearl on June 15, 2018, 15:19:59 PM
Quote from: GJC Web Design on June 15, 2018, 10:50:01 AM
maybe - haven't tried it - but will be something like this

Thanks, I ended up doing it a bit differently, but I was able to get the address output how I wanted it like this:

Firstname Lastname
P.O. Box 999999
Chino Valley, Arizona 86323
928-555-1212


Here is what I did to the email file (mail_html_shopperaddresses.php) as an example:

<?php
/**
 *
 * Layout for the order email
 * shows the chosen adresses of the shopper
 * taken from the stored order
 *
 * @package VirtueMart
 * @subpackage Order
 * @author Max Milbers,   Valerie Isaksen
 *
 * @link https://virtuemart.net
 * @copyright Copyright (c) 2004 - 2010 VirtueMart Team. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * VirtueMart is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 *
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

$br '<br class="clear" />';
$addressBTOrder = [[
    
'key' => 'email',
    
'suffix' => $br
], [
    
'key' => 'company',
    
'suffix' => $br
], [
    
'key' => 'title',
    
'suffix' => ''
], [
    
'key' => 'first_name',
    
'suffix' => ''
], [
    
'key' => 'last_name',
    
'suffix' => $br
], [
    
'key' => 'address_1',
    
'suffix' => $br
], [
    
'key' => 'address_2',
    
'suffix' => $br
], [
    
'key' => 'city',
    
'suffix' => ','
], [
    
'key' => 'virtuemart_state_id',
    
'suffix' => ''
], [
    
'key' => 'zip',
    
'suffix' => $br
], [
    
'key' => 'virtuemart_country_id',
    
'suffix' => $br
], [
    
'key' => 'phone_1',
    
'suffix' => $br
], [
    
'key' => 'phone_2',
    
'suffix' => $br
]];

$addressSTOrder = [[
    
'key' => 'company',
    
'suffix' => $br
], [
    
'key' => 'title',
    
'suffix' => ''
], [
    
'key' => 'first_name',
    
'suffix' => ''
], [
    
'key' => 'last_name',
    
'suffix' => $br
], [
    
'key' => 'address_1',
    
'suffix' => $br
], [
    
'key' => 'address_2',
    
'suffix' => $br
], [
    
'key' => 'city',
    
'suffix' => ','
], [
    
'key' => 'virtuemart_state_id',
    
'suffix' => ''
], [
    
'key' => 'zip',
    
'suffix' => $br
], [
    
'key' => 'virtuemart_country_id',
    
'suffix' => $br
], [
    
'key' => 'phone_1',
    
'suffix' => $br
], [
    
'key' => 'phone_2',
    
'suffix' => $br
]];


?>

<table class="html-email" cellspacing="0" cellpadding="5" border="0" width="100%" style="border-collapse: collapse; font-family: Arial, Helvetica, sans-serif; font-size: 12px; margin: 0 auto;">
<tr>
<th width="50%" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;">
    <?php echo vmText::_('COM_VIRTUEMART_USER_FORM_BILLTO_LBL'); ?>
</th>
<th width="50%" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;">
    <?php echo vmText::_('COM_VIRTUEMART_USER_FORM_SHIPTO_LBL'); ?>
</th>
    </tr>
    <tr>
<td valign="top" width="50%" style="border: 1px solid #CCCCCC;">

<?php foreach ($addressBTOrder as $field) :
$userField $this->userfields['fields'][$field['key']];
if (!empty($userField['value'])) : ?>

<span class="values vm2-<?= $userField['name'] ?>">
  <?= $this->escape($userField['value']) ?><?= $field['suffix'] ?>
</span>
<?php endif; endforeach ?>

</td>
<td valign="top" width="50%" style="border: 1px solid #CCCCCC;">
    <?php if(!empty($this->orderDetails['details']['has_ST'])){
foreach ($addressSTOrder as $field) :
$userField $this->shipmentfields['fields'][$field['key']];
if (!empty($userField['value'])) : ?>

<span class="values vm2-<?= $userField['name'] ?>">
  <?= $this->escape($userField['value']) ?><?= $field['suffix'] ?>
</span> <?php endif; endforeach ?>
<br class="clear" /> <?php
} else {
echo "<br/>".vmText::_('COM_VM_ST_SAME_AS_BT');
}
?>

</td>
    </tr>
<tr><td valign="top" width="50%" style="padding:5px"></td><td valign="top" width="50%" style="padding:5px"></td></tr>
</table>


"If you think you can do a thing or think you can't do a thing, you're right." Henry Ford