Simple Cart Order Tweak
5 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: April 4, 2014 (RSS)
By Perchpole - April 4, 2014
Hello, All -
I've been making some good progress with simpleCart - which is doing pretty much everything I hoped. There are just a couple of things I'd like to tweak.
The first concerns the sc-order-summary.php page. It includes the following code:
<?php foreach ($extraLineItems as $extraLineItem): ?>
<tr>
<td><?php echo htmlspecialchars($extraLineItem['name']); ?>: </td>
<td align="right"><?php echo sc_moneyFormat($extraLineItem['TOTAL']); ?></td>
</tr>
<?php endforeach; ?>
In my set-up, the $extraLineItems are Tax and Shipping.
I would like to add a third column to the output to show details of how the TOTAL for VAT and Shipping have been calculated. The output would become:
<?php foreach ($extraLineItems as $extraLineItem): ?>
<tr>
<td><?php echo htmlspecialchars($extraLineItem['name']); ?>: </td>
<td align="right"><?php echo sc_moneyFormat($extraLineItem['TOTAL']); ?></td>
<td>My text and calculation</td>
</tr>
<?php endforeach; ?>
How best should I go about this?
:0/
Perch
By Chris - April 4, 2014
Can you provide some specific and detailed examples of what should be displayed there and what calculations would be involved?
Chris
By Perchpole - April 4, 2014
Hi, Chris -
It would be fairly basic stuff. It's more an exercise in learning how to insert the data in the loop than the data itself.
In the case of the shipping, for an example, I want to echo the value of the cart $weight to show the customer why they are being charged. In the case of Tax I want print a message for customers from the European Union because they would have to pay VAT.
:0)
Perch
By Chris - April 4, 2014
Hi Perch,
You can add extra fields to "extra line items" by adding them to the arrays returned by sc_addTax() and sc_addShipping(). To see how things fit together, you may want to look at sc_getExtraLineItems(), which is what collects the extra line items from those two functions.
In sc-order-summary.php, you could add an optional 'desc' field:
<?php foreach ($extraLineItems as $extraLineItem): ?>
<tr>
<td><?php echo htmlspecialchars($extraLineItem['name']); ?>: </td>
<td align="right"><?php echo sc_moneyFormat($extraLineItem['TOTAL']); ?></td>
<td><?php echo @$extraLineItem['desc'] ?></td>
</tr>
<?php endforeach; ?>
Then add it to the array returned by sc_addTax():
function sc_addTax($cartItems) {
$taxRate = @$GLOBALS['SC_TAX_RATE'];
$desc = 'No VAT';
// custom tax rate for canadians
if (@$_REQUEST['billing_country'] == 'United Kingdom') {
$taxRate = 0.14;
$desc = 'VAT Included';
}
$taxTotal = 0;
foreach ($cartItems as $cartItem) {
$taxTotal += @$cartItem['TOTAL'] * $taxRate;
}
$taxTotal = round($taxTotal, 2);
$taxPercentage = $taxRate * 100;
return array( 'name' => "Tax ($taxPercentage%)", 'TOTAL' => $taxTotal, 'desc' => $desc );
}
And add it to the array returned by sc_addShipping():
function sc_addShipping($cartItems) {
$weightTotal = 0;
$shippingTotal = 0;
foreach ($cartItems as $cartItem) {
$shippingTotal += @$cartItem['shipping'] * @$cartItem['quantity'];
$weightTotal += @$cartItem['weight'] * @$cartItem['quantity']
foreach ($cartItem['OPTIONS'] as $option) {
$shippingTotal += @$option['shipping'] * @$cartItem['quantity'];
$weightTotal += @$option['weight'] * @$cartItem['quantity']
}
}
$desc = $weightTotal . "kg";
return array( 'name' => 'Shipping', 'TOTAL' => $shippingTotal, 'desc' => $desc );
}
Does that help?
Chris
By Perchpole - April 4, 2014
Stunning. Too good. You need to get out more!
Seriously, it's perfect. Thanks.
:0)
Perch