Simple Cart Order Tweak

5 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: April 4, 2014   (RSS)

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?

All the best,
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']); ?>:&nbsp;</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?

All the best,
Chris

By Perchpole - April 4, 2014

Stunning. Too good. You need to get out more!

Seriously, it's perfect. Thanks.

:0)

Perch