Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Output Order Total Module Value?


Tsimi

Recommended Posts

How can I output the value of a single order total module?

I know how osC outputs all order total modules (code sample is inside the checkout_confirmation.php)

What do I need to (re-)code to be able to select a certain module and output its value (price) inside the shopping_cart.php page.

 

 

Link to comment
Share on other sites

@@Tsimi

 

Had a thought........

 

Why not extend the order_total class such as (new file named- includes/classes/order_total_cart.php):

<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2015 osCommerce

  Released under the GNU General Public License
*/

  class order_total_cart extends order_total {
    
    function cart_output() {
      $output_string = '';
      if (is_array($this->modules)) {
        reset($this->modules);
        while (list(, $value) = each($this->modules)) {
          $class = substr($value, 0, strrpos($value, '.'));
          if ($GLOBALS[$class]->enabled) {
            $size = sizeof($GLOBALS[$class]->output_cart);
            for ($i=0; $i<$size; $i++) {
              $output_string .= '              <tr>' . "\n" .
                                '                <td align="right" class="main">' . $GLOBALS[$class]->output_cart[$i]['title'] . '</td>' . "\n" .
                                '                <td align="right" class="main">' . $GLOBALS[$class]->output_cart[$i]['text'] . '</td>' . "\n" .
                                '              </tr>';
            }
          }
        }
      }

      return $output_string;
    }
    
  }

?>

Then create whatever new order_total module you need to output.  You could basically duplicate whatever order total module you need to output from.

I'll post the includes/modules/order_total/ot_total_cart.php file that I made which is just the duplicated includes/modules/order_total/ot_total.php module notice we build the $output_cart array instead of the normal $output:

<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  class ot_total_cart {
    var $title, $output_cart;
    
    function ot_total_cart() {
      $this->code = 'ot_total_cart';
      $this->title = MODULE_ORDER_TOTAL_TOTAL_CART_TITLE;
      $this->description = MODULE_ORDER_TOTAL_TOTAL_CART_DESCRIPTION;
      $this->enabled = ((MODULE_ORDER_TOTAL_TOTAL_CART_STATUS == 'true') ? true : false);
      $this->sort_order = MODULE_ORDER_TOTAL_TOTAL_CART_SORT_ORDER;

      $this->output_cart = array();
    }

    function process() {
      global $order, $currencies;

      $this->output_cart[] = array('title' => $this->title . ':',
                              'text' => '<strong>' . $currencies->format($order->info['total'], true, $order->info['currency'], $order->info['currency_value']) . '</strong>',
                              'value' => $order->info['total']);
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_TOTAL_CART_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }

      return $this->_check;
    }

    function keys() {
      return array('MODULE_ORDER_TOTAL_TOTAL_CART_STATUS', 'MODULE_ORDER_TOTAL_TOTAL_CART_SORT_ORDER');
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Total on Shopping Cart', 'MODULE_ORDER_TOTAL_TOTAL_CART_STATUS', 'true', 'Do you want to display the total order value on the shopping cart page?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_TOTAL_CART_SORT_ORDER', '4', 'Sort order of display.', '6', '2', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }
    
  }
?>

Then where you need to output the data in shopping_cart.php:

<?php
require_once(DIR_WS_CLASSES . 'order.php');
require_once(DIR_WS_CLASSES . 'order_total.php');
require_once(DIR_WS_CLASSES . 'order_total_cart.php');

$order = new order;

$order_total_cart_modules = new order_total_cart;
$order_total_cart_modules->process();

if (MODULE_ORDER_TOTAL_INSTALLED) {
    echo $order_total_cart_modules->cart_output();
}
?>

You would need to install your new ot module in your admin and create its language file of course. You can also have better control of the html that is output in the includes/classes/order_total_cart.php class file.

 

This does work but I'm sure there is a simpler way of doing it.

Matt

Link to comment
Share on other sites

@@mattjt83

 

Hi Matt and thanks.

 

After our talk in the chat the other day i was trying to figure out a way to get this to work.

I copied the whole order_totals.php class file, renamed it and changed the $modules->output function to $modules->output2 the same thing I did inside the module that I want to show. The reason why I did that is the order_totals class file checks if a module is installed -> if yes -> it checks for the process and output function of each module. If a module has those functions it will show them. That is why I changed it to $modules->output2 and therefore let the copied/new orders_total file only check for $modules->output2 and that seems to work. It is more of a sly workaround but it does the trick. Now I am able to show only that single module's value in the shopping cart page.

 

I haven't tried out your idea but I will give it a go. Thank you.

 

The other problem I have is that I would like the value from that new module to be calculated to the sub-total of the cart but for that I will need to amend the shopping_cart class (calculate function), ...i guess.

Link to comment
Share on other sites

  • 4 weeks later...

@@Tsimi

 

I was playing around with this a bit more because I wanted to add the ability to apply a coupon on the shopping_cart page and then have the order total modules show the appropriate values.  Basically what I do is show the default osC output for the cart sub-total and then use a hook to replace that data if a coupon has been applied. I like it better than creating a duplicate orders total class and new ot modules etc....

 

The general idea...

 

Create a new class file to extend the order_total class.  Save as includes/classes/order_total_cart.php

<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2015 osCommerce

  Released under the GNU General Public License
*/

  class order_total_cart extends order_total {
    
    function cart_output( $include_array = array() ) {
      if ( sizeof( $include_array ) == 0 ) return false;
      $output_string = '';
      if (is_array($this->modules)) {
        reset($this->modules);
        while (list(, $value) = each($this->modules)) {
          $class = substr($value, 0, strrpos($value, '.'));
          if ( $GLOBALS[$class]->enabled && in_array( $class, $include_array ) ) {
            $size = sizeof($GLOBALS[$class]->output);
            for ($i=0; $i<$size; $i++) {
              $output_string .= '              <p class="text-right">' .
                                '                ' . $GLOBALS[$class]->output[$i]['title'] . '' .
                                '                ' . $GLOBALS[$class]->output[$i]['text'] . '' .
                                '              </p>';
            }
          }
        }
      }

      return $output_string;
    }
    
  }

?>

Now to call this new class do something like the following.  I used a hook to implement this but you could add the code as you see fit.

require_once DIR_WS_CLASSES . 'order.php';
$order = new order;

require_once DIR_WS_CLASSES . 'order_total.php';
require_once DIR_WS_CLASSES . 'order_total_cart.php';
                
$ot_cart_output = new order_total_cart;
                
$ot_cart_output->process();
                
$module_output = $ot_cart_output->cart_output(array('ot_subtotal', 'ot_discount_coupon', 'ot_total'));

$output = <<<EOD
<script>
$(function(){
    var output = '{$module_output}';
    if ( output != '' ){
        $(".extra-totals").html(output);
    }
});
</script>
EOD;

echo $output;//or return $output; if this is inside a function...

You can see I replace the .extra-totals html with my new output.  You give the that class to whatever you want replaced on your shopping cart page.

 

Of course in my hook I am determining if there is a valid coupon applied before even bothering to run through the rest of the code.

Matt

Link to comment
Share on other sites

@@mattjt83

 

 

I was playing around with this a bit more because I wanted to add the ability to apply a coupon on the shopping_cart

 

lol, that was "the" same reason I had. I wanted to output the discount value and recalculate the subtotal accordingly. I got it half way working but finally abandon the idea since my coding skill is near to 0 and therefore I would have probably messed up some other important function in the checkout.

Right now I am playing around with raiwa's Ship in Cart addon which shows the discount stuff and shipping stuff on the shopping cart page.

 

Thanks again for getting back to me, I will look at your codes and see if I can use this to get it finally working.

Link to comment
Share on other sites

Also note that this part of the code allows you to output whatever modules you would like.

$module_output = $ot_cart_output->cart_output(array('ot_subtotal', 'ot_discount_coupon', 'ot_total'));

Matt

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...