Post by xds » Tue Jul 13, 2010 6:06 pm

Image

After button press:
Image


First off, I'm not so great at PHP so my way of doing things may be a bit sloppy. I was looking for a mod to set category specific discount rates based on customer group but was unable to find such a thing. My solution was just to add a quick way to add to auto fill the Product Discounts tab with each group and the discount percentage I wanted to award. This was done on 1.4.8b and works for me but I can't guarantee anything to anyone nor can I offer you support if this doesn't work how you want it to.

Step 1. Create your customer groups with a percentage value in front of the group name. For example:

Code: Select all

New Customer
5% Discount Group
10% Returning Customer
15% Friend
20% Whoelsale
What you put after the percentage doesn't matter as long as the first thing in the name of the group is a number and a percent sign, it will automatically calculate these. You will notice my New Customer group has no number or percentage sign and this group will be completely ignored by the auto fill function.

Step 2. Open admin\view\template\catalog\product_form.tpl and scroll down to around line 455 where you see:

Code: Select all

   <tfoot>
      <tr>
         <td colspan="6"></td>
         <td class="left" id="discount_foot"><a onclick="addDiscount();" class="button"><span><?php echo $button_add_discount; ?></span></a></td>
      </tr>
   </tfoot>
</table>
Here we are going to add an id to the tfoot, and another tr as follows:

Code: Select all

<tfoot id="discount_foot">
            <tr>
              <td colspan="6"></td>
              <td class="left" id="discount_foot"><a onclick="addDiscount();" class="button"><span><?php echo $button_add_discount; ?></span></a></td>
            </tr>
            <tr id="discount_filler">
              <td colspan="6" align="right"><span style="color:#900; font-weight:bold;">This should only be performed if NO discount levels are defined above: </span></td>
              <td class="left"><a onclick="addAllDiscountLevels();" class="button"><span><?php echo $button_add_all_levels; ?></span></a></td>
            </tr>
          </tfoot>
        </table>
What this does is adds another table row with a new button for us to use to auto fill our discount discounts for each customer group we have with a number followed by a percent sign.

Step 3. Scroll down again to around line 767 where you see the closing tags for the function addDiscount, it will look like this:

Code: Select all

	$('#discount tfoot').before(html);
		
	$('#discount_row' + discount_row + ' .date').datepicker({dateFormat: 'yy-mm-dd'});
	
	discount_row++;
}
//--></script>
After the "/script" tag, press enter to insert a new line and paste in the following:

Code: Select all

<script type="text/javascript"><!--
var discount_row = <?php echo $discount_row; ?>;

function addAllDiscountLevels() {
	
	document.getElementById('discount_foot').removeChild(document.getElementById('discount_filler'));
	
	<?php 	
	foreach ($customer_groups as $group_discount) { 
	preg_match('/([0-9]*)%/', $group_discount['name'], $match);
	if ($match[1] > 0) {
	$discount_modifier = 1 - ($match[1] / 100);
	?>	
	
	var discount_value = Math.round((<?php echo $discount_modifier; ?> * document.product_form.price.value)*100)/100;	
	html  = '<tbody id="discount_row' + discount_row + '">';
	html += '<tr bgcolor="#C8F7CB">'; 
    html += '<td class="left"><input type="hidden" name="product_discount[' + discount_row + '][customer_group_id]" value="<?php echo $group_discount['customer_group_id']; ?>"><?php echo $group_discount['name']; ?>';
    html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][quantity]" value="1" size="2" /></td>';
    html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][priority]" value="2" size="2" /></td>';
	html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][price]" value="' + discount_value + '" /></td>';
    html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][date_start]" value="2010-01-01" class="date" /></td>';
	html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][date_end]" value="2050-01-01" class="date" /></td>';
	html += '<td class="left"><a onclick="$(\'#discount_row' + discount_row + '\').remove();" class="button"><span><?php echo $button_remove; ?></span></a></td>';
	html += '</tr>';	
    html += '</tbody>';
	
	$('#discount tfoot').before(html);
		
	$('#discount_row' + discount_row + ' .date').datepicker({dateFormat: 'yy-mm-dd'});
	
	discount_row++;
	<?php }} ?>
}
//--></script>
This is the actual function that will insert a new discount entry for each customer group, find the percent value, calculate the price based on whatever you typed into the price field on the Data tab, and insert the discount prices for you.

Step 4. Open admin\controller\catalog\product.php and around line 531 insert this entry:

Code: Select all

		$this->data['button_add_all_levels'] = $this->language->get('button_add_all_levels');
Step 5. Open admin\language\english\english.php and around line 52 insert this:

Code: Select all

$_['button_add_all_levels']   = 'Add All Levels';
All done!

Keep in mind, if you edit a price on a product, you will have to go and delete all pre-existing discount entries and use the Add All Levels button again to insert them with the updated values. All new fields added by this button will be highlighted in green to signify the new ones just in-case. The color change will not be there after you save the product and go back to it though. I hard-coded a warning message in there instead of adding it to a language file because I am lazy and it was just for information purposes. The button also hides itself on initial click to keep you from hitting it twice, it will be there when you edit the product next time.

Thanks for 1.5!


xds
New member

Posts

Joined
Mon Jul 12, 2010 9:53 am

Post by Maansy » Sun Jul 18, 2010 12:17 pm

thanks XDS
i am sure i will be using this mod when the store goes live :)

ALL Templates :: 1.5.1+ Templates :: 50%-75% PRICE DROP ONLY at OpencartStuff.com


User avatar
Active Member

Posts

Joined
Thu Jun 24, 2010 6:04 am


Post by cmerry » Sun Jul 18, 2010 10:25 pm

Thanks XDS for this mod, but gives me an error:

Undefined offset: 1 in C:\XAMPP\htdocs\admin\view\template\catalog\product_form.tpl on line 783

on line 783 I have:

if ($match[1] > 0) {


User avatar
Active Member

Posts

Joined
Sat Jul 17, 2010 8:42 pm

Post by SteveSherry » Fri Jul 23, 2010 6:09 am

cmerry wrote:Thanks XDS for this mod, but gives me an error:

Undefined offset: 1 in C:\XAMPP\htdocs\admin\view\template\catalog\product_form.tpl on line 783

on line 783 I have:

if ($match[1] > 0) {
I'm getting the same error, but mine is saying the following line

Code: Select all

   $discount_modifier = 1 - ($match[1] / 100); 

My Website ¦ Summer Madness Special Offer ¦


Active Member

Posts

Joined
Thu Apr 08, 2010 7:47 am
Location - Wirral, UK

Post by xds » Sun Jul 25, 2010 5:52 am

You are using openCart 1.4.8b?

You setup your customer groups named like 10% Discount, 15% Discount etc etc?
The first part of your customer groups must have a number immediately followed by a percent sign.

Javascript looks like the following?

Code: Select all

	discount_row++;
}
//--></script>

<?php //begin addition ?>
<script type="text/javascript"><!--
var discount_row = <?php echo $discount_row; ?>;

function addAllDiscountLevels() {
	
	document.getElementById('discount_foot').removeChild(document.getElementById('discount_filler'));
	
	<?php 	
	foreach ($customer_groups as $group_discount) { 
		preg_match('/([0-9]*)%/', $group_discount['name'], $match);
		if ($match[1] > 0) {
			$discount_modifier = 1 - ($match[1] / 100);
		?>	
	
		var discount_value = Math.round((<?php echo $discount_modifier; ?> * document.product_form.price.value)*100)/100;	
		html  = '<tbody id="discount_row' + discount_row + '">';
		html += '<tr bgcolor="#C8F7CB">'; 
	    html += '<td class="left"><input type="hidden" name="product_discount[' + discount_row + '][customer_group_id]" value="<?php echo $group_discount['customer_group_id']; ?>"><?php echo $group_discount['name']; ?>';
    	html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][quantity]" value="1" size="2" /></td>';
	    html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][priority]" value="2" size="2" /></td>';
		html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][price]" value="' + discount_value + '" /></td>';
	    html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][date_start]" value="2010-01-01" class="date" /></td>';
		html += '<td class="left"><input type="text" name="product_discount[' + discount_row + '][date_end]" value="2050-01-01" class="date" /></td>';
		html += '<td class="left"><a onclick="$(\'#discount_row' + discount_row + '\').remove();" class="button"><span><?php echo $button_remove; ?></span></a></td>';
		html += '</tr>';	
	    html += '</tbody>';
	
		$('#discount tfoot').before(html);
		
		$('#discount_row' + discount_row + ' .date').datepicker({dateFormat: 'yy-mm-dd'});
	
		discount_row++;
	<?php }} ?>
}
//--></script>

<?php // end of my addition ?>

<script type="text/javascript"><!--
var special_row = <?php echo $special_row; ?>;

Thanks for 1.5!


xds
New member

Posts

Joined
Mon Jul 12, 2010 9:53 am

Post by SteveSherry » Sun Jul 25, 2010 9:47 am

I'm running 1.4.8b
and yes, my code is exact copy of yours,
it's just weird, I add 10% group, I click the "add all levels" button and the button vanished, but nothing happens.
and also, nothing is appearing in my error log file this time....... ???

My Website ¦ Summer Madness Special Offer ¦


Active Member

Posts

Joined
Thu Apr 08, 2010 7:47 am
Location - Wirral, UK

Post by cmerry » Mon Jul 26, 2010 6:40 am

I'm running 1.4.8b and I have same problem :(
PHP Notice: Undefined offset: 1 in C:\XAMPP\xampp\htdocs\admin_ryokucha\view\template\catalog\product_form.tpl on line 783


User avatar
Active Member

Posts

Joined
Sat Jul 17, 2010 8:42 pm

Post by xds » Sat Jul 31, 2010 7:56 am

Ok, I have found the problem. It is because at least one of your customer groups does not start with a number and a percent sign. I just realized during all my testing ALL of my groups had a value, inlcuding my New customer group which was named "0% New Customer"

It seems my foreach isn't properly looping on fail. For now, renaming your groups to make sure ALL of the starting with a number and a % sign will solve it.

Example:

Code: Select all

0% New Customer (Default)
5% Discount
10% Discount
15% Discount

Thanks for 1.5!


xds
New member

Posts

Joined
Mon Jul 12, 2010 9:53 am

Post by cmerry » Tue Aug 03, 2010 4:38 am

xds wrote:Ok, I have found the problem. It is because at least one of your customer groups does not start with a number and a percent sign. I just realized during all my testing ALL of my groups had a value, inlcuding my New customer group which was named "0% New Customer"

It seems my foreach isn't properly looping on fail. For now, renaming your groups to make sure ALL of the starting with a number and a % sign will solve it.

Example:

Code: Select all

0% New Customer (Default)
5% Discount
10% Discount
15% Discount
+ must change this javascript code

Code: Select all

var discount_value = Math.round((<?php echo $discount_modifier; ?> * document.product_form.price.value)*100)/100; 
to

Code: Select all

var discount_value = Math.round((<?php echo $discount_modifier; ?> * document.forms["form"].price.value)*100)/100;
and you have working Auto Fill Product Discounts by Group :dance:


User avatar
Active Member

Posts

Joined
Sat Jul 17, 2010 8:42 pm

Post by amdev » Wed Aug 04, 2010 9:59 am

Many Thanks....
I using 148b and it work great













----------------------------------------------------------------------------------------------------------------------------------------------
nkdonut | Chaophraya Riverside

ร้านค้าออนไลน์
OpenCart Thailand Support Forum
How to Upgrade oc1.5 to 2.0.1.1
Upgrading OpenCart From v.1.4 or v.1.5 to V.2.2 Step by step


User avatar
Active Member

Posts

Joined
Fri Nov 27, 2009 3:40 pm
Location - Bangkok - Thailand

Post by SteveSherry » Mon Aug 16, 2010 8:11 am

Fantastic! all working now!

My Website ¦ Summer Madness Special Offer ¦


Active Member

Posts

Joined
Thu Apr 08, 2010 7:47 am
Location - Wirral, UK

Post by Cigs » Mon Jan 23, 2012 2:12 am

Does it mean that you still have to press "Add All Levels" button on each product page?

New member

Posts

Joined
Thu Nov 24, 2011 5:23 am
Who is online

Users browsing this forum: No registered users and 1 guest