Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Help with Discount contribution


peeta

Recommended Posts

I need to allow variable percentage discounts based on total purchase quantity

 

example:

Combined total items in cart

1 - 49 = regular price

50 - 100 = 10% discount

101 - 175 = 15% discount

176 - 250 = 20% discount

251+ = 25% discount

 

I installed a contribution that works exactly as needed but only supports one discount range. Discounts per product quantity by Takitei

 

Can this be modified to do the variations I need or is there another contrib that does this? Thanks

Link to comment
Share on other sites

Can this be modified to do the variations I need
Probably yes. PHP challenged I guess? Otherwise you probably would have tried to change:

	  if(MODULE_ORDER_TOTAL_DISCOUNT_QUANT_STATUS == 'true'){
	if($this->count_contents() >= MODULE_ORDER_TOTAL_DISCOUNT_QUANT_ITEMS){
	  $discount_quant = $this->total * MODULE_ORDER_TOTAL_DISCOUNT_QUANT_PERCENT / 100;
	  $this->total -= $discount_quant;
}
 }

To something like:

	  if(MODULE_ORDER_TOTAL_DISCOUNT_QUANT_STATUS == 'true'){
	if($this->count_contents() >= 251 ){
	  $discount_quant = $this->total * 25 / 100;
	  $this->total -= $discount_quant;
 } elseif ($this->count_contents() >= 176 && $this->count_contents() <= 250 ) {
	  $discount_quant = $this->total * 20 / 100;
	  $this->total -= $discount_quant;
 }  elseif ($this->count_contents() >= 101 && $this->count_contents() <= 175 ) {
	  $discount_quant = $this->total * 15 / 100;
	  $this->total -= $discount_quant;
 }  elseif ($this->count_contents() >= 50 && $this->count_contents() <= 100 ) {
	  $discount_quant = $this->total * 10 / 100;
	  $this->total -= $discount_quant;
}
 }

Link to comment
Share on other sites

JanZ I appreciate your response. Yes I am PHP challenged. All I know is what I've learnt here. I was hoping there was some kind of WYSIWYG PHP tool that would let me choose what I want to happen and it generate the code but I have yet to find one.

 

I have not yet tested the code you have post but once I do I'll alert you on the results. Thanks

Link to comment
Share on other sites

JanZ I installed the code and it works fine up to the cart contents however, on the confirmation page it shows and deducts 10% only for even the high quantities.

 

I also assume that modifications must be done to the other files (2) in the contrib package to make the discount choices display in the modules configuration at the admin page. This is where I have the 10% discount entered and it seems one of the files (catalog/includes/modules/order_total/ot_discount_quant.php) writes these changes to the database.

 

<?php
 class ot_discount_quant {
var $title, $output;

function ot_discount_quant() {
  $this->code = 'ot_discount_quant';
  $this->title = MODULE_ORDER_TOTAL_DISCOUNT_QUANT_TITLE;
  $this->description = MODULE_ORDER_TOTAL_DISCOUNT_QUANT_DESCRIPTION;
  $this->enabled = ((MODULE_ORDER_TOTAL_DISCOUNT_QUANT_STATUS == 'true') ? true : false);
  $this->sort_order = MODULE_ORDER_TOTAL_DISCOUNT_QUANT_SORT_ORDER;

  $this->output = array();
}

function process() {
  global $order, $currencies, $discount_quant, $cart;
  //if the module is on, then apply a discount
  if(MODULE_ORDER_TOTAL_DISCOUNT_QUANT_STATUS == 'true'){
	if($cart->count_contents() >= MODULE_ORDER_TOTAL_DISCOUNT_QUANT_ITEMS){
	  $discount_quant = $order->info['subtotal'] * MODULE_ORDER_TOTAL_DISCOUNT_QUANT_PERCENT / 100;
	  $order->info['total'] -= $discount_quant;
	  $this->output[] = array('title' => $this->title . ':',
							  'text' => $currencies->format($discount_quant, true, $order->info['currency'], $order->info['currency_value']),
							  'value' => $discount_quant);
	}
  }
}

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

  return $this->_check;
}

function keys() {
  return array('MODULE_ORDER_TOTAL_DISCOUNT_QUANT_STATUS', 'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_SORT_ORDER' ,'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_ITEMS', 'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_PERCENT');
}

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 ('Discount per Quantity', 'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_STATUS', 'true', 'Do you want allow the discount per quantity?', '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_DISCOUNT_QUANT_SORT_ORDER', '1', 'Sort order of display.', '6', '2', now())");
  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Quantity Required', 'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_ITEMS', '6', 'The minimal amount of products to apply the discount.', '6', '3', now())");
  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Discount Percentage', 'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_PERCENT', '6', 'If the discounts are available, this indicates the percentage to apply to the total amount.', '30', '4', now())");
}

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

 

I greatly appreciate your time and assistance on this.

Link to comment
Share on other sites

I tried the following but it just returned an error. Ofcourse I don't have a clue of what I'm doing.

tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Quantity Required', 'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_ITEMS', '149', 'The minimal amount of products to apply the discount.', '149', '3', now())");
tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Discount Percentage', 'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_PERCENT', '149', 'If the discounts are available, this indicates the percentage to apply to the total amount.', '15', '4', now())");
tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Quantity Required', 'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_ITEMS', '50', 'The minimal amount of products to apply the discount.', '50', '3', now())");
tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Discount Percentage', 'MODULE_ORDER_TOTAL_DISCOUNT_QUANT_PERCENT', '50', 'If the discounts are available, this indicates the percentage to apply to the total amount.', '10', '4', now())");
}
}

Edited by peeta
Link to comment
Share on other sites

I also assume that modifications must be done to the other files (2) in the contrib package to make the discount choices display in the modules configuration at the admin page. This is where I have the 10% discount entered and it seems one of the files (catalog/includes/modules/order_total/ot_discount_quant.php) writes these changes to the database.
You are right, haven't looked at those files. If you don't have a problem with the numbers of items and discount percentages hard-coded in the files instead of into the configuration table (like in shopping_cart.php) then I think you would only need to change the ot_discount_quant.php file, function process (haven't tested this):

	function process() {
  global $order, $currencies, $discount_quant, $cart;
  //if the module is on, then apply a discount
  if(MODULE_ORDER_TOTAL_DISCOUNT_QUANT_STATUS == 'true'){
	if($cart->count_contents() >= MODULE_ORDER_TOTAL_DISCOUNT_QUANT_ITEMS){ 
// if MODULE_ORDER_TOTAL_DISCOUNT_QUANT_ITEMS is 50 all higher quantities are catched
$mod_order_total_discount_quant_percent = 0;
if($cart->count_contents() >= 251 ){
$mod_order_total_discount_quant_percent = 25;
} elseif ($cart->count_contents() >= 176 && $cart->count_contents() <= 250 ) {
$mod_order_total_discount_quant_percent = 20;
} elseif ($cart->count_contents() >= 101 && $cart->count_contents() <= 175 ) {
$mod_order_total_discount_quant_percent =  15;
} elseif ($cart->count_contents() >= 50 && $cart->count_contents() <= 100 ) {
$mod_order_total_discount_quant_percent =  10;
}	
	  $discount_quant = $order->info['subtotal'] * $mod_order_total_discount_quant_percent / 100;
	  $order->info['total'] -= $discount_quant;
	  $this->output[] = array('title' => $this->title . ':',
							  'text' => $currencies->format($discount_quant, true, $order->info['currency'], $order->info['currency_value']),
							  'value' => $discount_quant);
	} // end if($cart->count_contents() >= MODULE_ORDER_TOTAL_DISCOUNT_QUANT_ITEMS)
  }
}

Link to comment
Share on other sites

Janz... again I thank you for the guide. It works perfectly!

 

In the admin panel where it was installed as a module under Order Total it only shows the original fields for quantity which contains only the 10% / 50 quant discount. The others are not displayed so I suppose I can always change them manually in the codes. If you do add the mods maybe you should post to this contribution as version 2.

 

Thanks again

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...