Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Creating Shopping Cart Mod


Recommended Posts

Posted

Hi 

I have recently installed modular shopping cart for  bs frozen 

before that time i was just using the standard shopping cart but I had made a few modifications to make it do what I wanted it to - essentially I had set up a minimum order quantity - that meant in the shopping cart the customer saw how much they still needed to spend and hid the checkout button if they had not reached their minimum...

What 

i am wanting to do is to create my own add-on mod to look after the minimum order message part of things but I have never created a modular add on so not sure what to do. Can anyone give me some pointers on what I need to do to get it working - what bits go in the execute function and what goes into the template file..

below is part of  my original (modified) shopping cart file  - the bit that I think needs to be added to the new module is the bit inside the two commented sections ...

 

  require('includes/languages/' . $language . '/shopping_cart.php');

  $breadcrumb->add(NAVBAR_TITLE, tep_href_link('shopping_cart.php'));

  require('includes/template_top.php');
 
// Added by Portman to calculate minimum order amount 
 	if ($cart->count_contents() > 0) {
	  $order_val = $cart->show_total();
	  
	} else {
		$order_val = 0;
	}
	  $min_order_val = constant('MODULE_STORE_WHOLESALE_MIN_ORDER_' . $wholesale_group . '_ENGLISH');
	  
	  $min_order = $min_order_val - $order_val;
// End Added by Portman	  

?>

<div class="page-header">
  <h1 class="h3"><?php echo HEADING_TITLE; ?></h1> 
</div> 
 
<?php // added by Portman to display Minimum order value
	  if ($min_order > 0) {
?>		 <div class="alert alert-danger"> 
		 <?php echo MIN_ORDER_TEXT_SC1 . '<strong> ' . $currencies->format($min_order) . '</strong> ' . MIN_ORDER_TEXT_SC2;
	  }
	  else {
?>		<div class="alert alert-success alert-dismissible">  
		  <?php echo MINIMUM_ORDER_REACHED_SC; 
	  } ?>
	  </div>
<?php 
		  
// End Added by Portman 	 

  if ($messageStack->size('product_action') > 0) {
    echo $messageStack->output('product_action');
  }
?>

 

Posted

Hello Pete @Portman,

First of all you should better post this question in the official support thread:
https://www.oscommerce.com/forums/topic/409351-modular-shopping-cart/

To create a new shopping cart module follow these steps:

- choose a similar existing module like "cm_sc_no_products.php"
- duplicate and rename all files (main file, template file, language file(s)
- important: keep the naming convention for the template file: same name as the main files + "tpl_" prefix.
- rename the class name in the main file to the same name as the new filename
- rename all constants "MODULE_CONTENT_SC_NO_PRODUCTS_..." to a new name like "MODULE_CONTENT_SC_MINIMUM_ORDER_..."
- change the configuration entry descriptions and the language definitions for your needs
- add your language definitions to the language files
- rename the template class "cm-sc-no-products" to your correspondant new module name
- place the first part of your cod (data checks) into the main module inside the execute function
- don't forget to remove the code of the original module
- add the required variables to the globals list (in your code I believe only $cart is needed, it's already there.
- replace the html output in the template file with your second part of code

Install your new module in Admin : modules : content[shopping_cart] (adjust the sort order for your needs)

Hope this helps
rgds
Rainer

 

Posted

It's really good to see you stopping to think about the best way to go about doing this instead of just doing a quick hack to the code.

To answer your question the split in a content module between what goes in the template and what in the execute function is mostly about trying to separate the layout from the content - the idea is that on the whole a shopowner can use a module and change its appearance a lot just by configuring options and using css, but some people might need to change the template a bit (eg by adding extra classes) for the layout of their site. Templates are typically mostly html with only a little bit of php.

However it's worth broadening things a bit. Content modules are only executed on a page with that content section (so header modules on all pages, main content modules on one specific page) and are about rendering specific content. There are also header tags modules which are more about changing the behaviour of the site and are executed earlier in the page.

Personally I think I'd approach your addon differently. Ok, there's a message to display on the cart, but shouldn't the buyer also get bumped out of any checkout page back to the cart if it's below minimum order value? And shouldn't the checkout button on the cart be disabled below MOV? That feels more like a header tags module than a content module to me - you can just use the standard message class and content module to actually display the message on the cart page.

Contact me for work on updating existing stores - whether to Phoenix or the new osC when it's released.

Looking for a payment or shipping module? Maybe I've already done it.

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

Posted
40 minutes ago, BrockleyJohn said:

Personally I think I'd approach your addon differently. Ok, there's a message to display on the cart, but shouldn't the buyer also get bumped out of any checkout page back to the cart if it's below minimum order value? And shouldn't the checkout button on the cart be disabled below MOV? That feels more like a header tags module than a content module to me - you can just use the standard message class and content module to actually display the message on the cart page.

@BrockleyJohn, you are right, the checkout button should be disabled which could be done with a javascript which the content module injects as a footer script. Or a mod could be added to the checkout button (and alt checkout if PayPal Express is used) module. Anyway if the checkout button gets disabled, there is no need to show the message on the other checkout pages, the customer will not forward and stay on the shopping cart. So I believe a shopping cart content module is fine.

Posted
24 minutes ago, raiwa said:

the customer will not forward and stay on the shopping cart. So I believe a shopping cart content module is fine.

Yep, missed here the checkout button in the shopping cart navbar module. So that one should be also disabled/hidden.

Posted
1 hour ago, raiwa said:

Yep, missed here the checkout button in the shopping cart navbar module. So that one should be also disabled/hidden.

and a header button and what about multiple tabs open on the site... (that's why I'd put a redirect in, it's less work)

But there were always lots of different ways to do the same thing in osc and now it's increased exponentially! Whatever makes most sense to the person writing it is the best way to go if they cover all the bases.

Contact me for work on updating existing stores - whether to Phoenix or the new osC when it's released.

Looking for a payment or shipping module? Maybe I've already done it.

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

Posted

I agree, most correct would be to implement checks everywhere like it is done for stock check.

Posted

Thanks for your feedback @raiwa &  @BrockleyJohn I will play around with what you have suggested...

In my original shopping cart file I had a basic if statement around the part that drew the checkout button...

  <div class="buttonSet">
<?php // Mod by Portman to hide checkout button in min order not reached 
	if ($min_order <= 0) { ?>
      <div class="text-right"><?php echo tep_draw_button(IMAGE_BUTTON_CHECKOUT, 'fa fa-angle-right', tep_href_link('checkout_shipping.php', '', 'SSL'), 'primary', NULL, 'btn-success'); ?></div>
	<?php } //End Mod ?> 
  </div>

I assumed I would just have to do a similar thing in one of the other shopping cart mods, but to be honest I have not looked that far ahead yet... if this is not the case could you please give me a few more "beginner" pointers on what I should do next.

 

Thanks 

 

Posted
2 minutes ago, Portman said:

Thanks for your feedback @raiwa &  @BrockleyJohn I will play around with what you have suggested...

In my original shopping cart file I had a basic if statement around the part that drew the checkout button...


  <div class="buttonSet">
<?php // Mod by Portman to hide checkout button in min order not reached 
	if ($min_order <= 0) { ?>
      <div class="text-right"><?php echo tep_draw_button(IMAGE_BUTTON_CHECKOUT, 'fa fa-angle-right', tep_href_link('checkout_shipping.php', '', 'SSL'), 'primary', NULL, 'btn-success'); ?></div>
	<?php } //End Mod ?> 
  </div>

I assumed I would just have to do a similar thing in one of the other shopping cart mods, but to be honest I have not looked that far ahead yet... if this is not the case could you please give me a few more "beginner" pointers on what I should do next.

 

Thanks 

 

Yes, you can apply the same mod in the shopping cart checkout module

Posted

@raiwa, I have done what you have suggested ...  and it seems to work except for the variable $wholesale_group is empty... in catalog/shopping_cart.php it has a value but not here -  any idea what I have missed?

<?php
/*
  $Id$

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

  Copyright (c) 2016 osCommerce

  Released under the GNU General Public License
*/

  class cm_sc_min_order {
    public $code;
    public $group;
    public $title;
    public $description;
    public $sort_order;
    public $enabled = false;

    function __construct() {
      $this->code = get_class($this);
      $this->group = basename(dirname(__FILE__));

      $this->title = MODULE_CONTENT_SC_MIN_ORDER_TITLE;
      $this->description = MODULE_CONTENT_SC_MIN_ORDER_DESCRIPTION;

      if ( defined('MODULE_CONTENT_SC_MIN_ORDER_STATUS') ) {
        $this->sort_order = MODULE_CONTENT_SC_MIN_ORDER_SORT_ORDER;
        $this->enabled = (MODULE_CONTENT_SC_MIN_ORDER_STATUS == 'True');
      }
    }

    function execute() {
      global $oscTemplate, $cart;
	  
	  $content_width = (int)MODULE_CONTENT_SC_MIN_ORDER_CONTENT_WIDTH;

 	if ($cart->count_contents() > 0) {
	  $order_val = $cart->show_total();
	  
	} else {
		$order_val = 0;
	}
	  $min_order_val = constant('MODULE_STORE_WHOLESALE_MIN_ORDER_' . $wholesale_group . '_ENGLISH');
	  
	  $min_order = $min_order_val - $order_val;	
	  
	        ob_start();
      	include('includes/modules/content/' . $this->group . '/templates/tpl_' . basename(__FILE__));
        $template = ob_get_clean();

        $oscTemplate->addContent($template, $this->group);  
    }

    function  isEnabled() {
      return $this->enabled;
    }

    function check() {
      return defined('MODULE_CONTENT_SC_MIN_ORDER_STATUS');
    }

    function install() {
      tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Shopping Cart Minimum Order Message', 'MODULE_CONTENT_SC_MIN_ORDER_STATUS', 'True', 'Do you want to add the module to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");	
      tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Width', 'MODULE_CONTENT_SC_MIN_ORDER_CONTENT_WIDTH', '12', 'What width container should the content be shown in?', '6', '2', 'tep_cfg_select_option(array(\'12\', \'11\', \'10\', \'9\', \'8\', \'7\', \'6\', \'5\', \'4\', \'3\', \'2\', \'1\'), ', now())");
      tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_CONTENT_SC_MIN_ORDER_SORT_ORDER', '1', 'Sort order of display. Lowest is displayed first.', '6', '3', now())");
    }

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

    function keys() {
      return array('MODULE_CONTENT_SC_MIN_ORDER_STATUS', 'MODULE_CONTENT_SC_MIN_ORDER_CONTENT_WIDTH', 'MODULE_CONTENT_SC_MIN_ORDER_SORT_ORDER');
    }
  }

 

Posted

You need to add the " $wholesale_group " variable to the global list of the execute funtion.

On 7/29/2019 at 8:45 AM, raiwa said:

- add the required variables to the globals list (in your code I believe only $cart is needed, it's already there.

Sorry didn't see that you are using wholesale for this.

Posted

Thanks @raiwa got it all working now and have learnt a thing or two about modular design.... though I think there is a lot more to learn still!!

Archived

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

×
×
  • Create New...