Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

implement a badge system in reward points addon Rate Topic   - - - - -

 
  • developer
  • Member
  • Members
  • Join Date: 02-Jan 12
  • 42 posts

Posted 25 September 2012 - 07:05 AM #1

I want to add an additional feature to the rewards program. I would like to implement a badge system such as gold, silver, bronze etc and customers can be automatically upgraded according to their spending. can anyone show me right direction to do this in cs-cart ?

I am thinking about possible approaches to do this
- should i put a hook in save order and calucate total spending of that user and assign it to perticular user group ?
- or make a cron to run everyday and check every order and do above thing.

Any help will be appreciated.

 
  • Flow
  • Super Duper and Amazingly Sexy Senior
  • Members
  • Join Date: 13-Oct 10
  • 2306 posts

Posted 25 September 2012 - 08:15 AM #2

Interesting idea! I guess you could transform the amount of rewards points into a badge, or call a certain badge if reward points are over an x amount. The problem might be, if someone spends points, they will be downgraded. Will have to look into this more.

When life hands you lemons, bring on the Tequila baby!


 
  • developer
  • Member
  • Members
  • Join Date: 02-Jan 12
  • 42 posts

Posted 04 October 2012 - 09:18 AM #3

Hello All

I made a "Badge System" in my own way,

I need to make hooks for two events,
'update_profile'
'change_order_status',

I want to assign user to "Bronze" User Group once He/She registered, so i made hook for 'update_profile'. $bronzeUserGroupId is the id of that User Group in below function,
function fn_my_changes_update_profile($action, $user_data, $current_user_data) {
	    if($action == "add"){
		    /*  Assign User to Bronze Group	 */
		    $bronzeUserGroupId = 5;
		    $dataAry = array(
		    'user_id' => $user_data['user_id'],
		    'usergroup_id' => $bronzeUserGroupId,
		    'status' => 'A'
		    );
		    db_query("INSERT INTO ?:usergroup_links ?e", $dataAry);
		    /*  Assign User to Bronze Group	 */
	    }
    }

and after that i made hook to 'change_order_status' event, as reward points only added once order goes to "Complete" status,

function fn_my_changes_change_order_status($status_to, $status_from, $order_info, $force_notification, $order_statuses, $place_order) {
	    if($status_to == "C"){
		    if($order_info['user_id']) {
			    $userId = $order_info['user_id'];
			    /*  Generating Array of currently assigned Usergroups to User   */
			    $curUserGroups = array();
			    $selectCondition = array (
			    'user_id' => $userId
			    );
			    $resultData = db_get_array('SELECT * FROM ?:usergroup_links WHERE ?w', $selectCondition);
			    foreach($resultData as $rsltData)
			    {
				    $curUserGroups[$rsltData['usergroup_id']] = $rsltData['status'];
			    }
			    /*  Generating Array of currently assigned Usergroups to User   */
			    /*  Assign Your Custom User Group Ids Here  */
			    $userGroupIds = array (
			    'Platinum' => 8,
			    'Gold' => 7,
			    'Silver' => 6,
			    'Bronze' => 5
			    );
			    /*  Assign Your Custom User Group Ids Here  */
			    /*  Calculate Total Reward Points Amount of User    */
			    $resultData2 = db_get_row('SELECT SUM(amount) FROM ?:reward_point_changes WHERE user_id = ?i AND amount > 0', $userId);
			    $userTotalRewardPoints = $resultData2['SUM(amount)'];
			    /*  Calculate Total Reward Points Amount of User    */
			    if($userTotalRewardPoints > 10000)
			    {
				    /* Platinum User Group */
				    if(isset($curUserGroups[$userGroupIds['Platinum']])) {
					    if($curUserGroups[$userGroupIds['Platinum']] != 'A'){
						    /*  Update User to Platinum Group	 */
						    $updateData = array (
						    'status' => 'A'
						    );
						    $whereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Platinum']
						    );
						    db_query('UPDATE ?:usergroup_links SET ?u WHERE ?w', $updateData, $whereCondition);   
						    /*  Update User to Platinum Group	 */
						    /*  Remove User From Gold User Group    */
						    $deleteWhereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Gold']
						    );
						    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
						    /*  Remove User From Gold User Group    */
					    }
				    }
				    else {
					    /*  Assign User to Platinum Group	 */
					    $platinumAry = array(
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Platinum'],
					    'status' => 'A'
					    );
					    db_query("INSERT INTO ?:usergroup_links ?e", $platinumAry);
					    /*  Assign User to Platinum Group	 */
					    /*  Remove User From Gold User Group    */
					    $deleteWhereCondition = array (
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Gold']
					    );
					    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
					    /*  Remove User From Gold User Group    */
				    }
			    }
			    elseif($userTotalRewardPoints > 5000)
			    {
				    /* Gold User Group */
				    if(isset($curUserGroups[$userGroupIds['Gold']])) {
					    if($curUserGroups[$userGroupIds['Gold']] != 'A'){
						    /*  Update User to Gold Group	 */
						    $updateData = array (
						    'status' => 'A'
						    );
						    $whereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Gold']
						    );
						    db_query('UPDATE ?:usergroup_links SET ?u WHERE ?w', $updateData, $whereCondition);   
						    /*  Update User to Gold Group	 */
						    /*  Remove User From Silver User Group    */
						    $deleteWhereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Silver']
						    );
						    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
						    /*  Remove User From Silver User Group    */
					    }
				    }
				    else {
					    /*  Assign User to Gold Group	 */
					    $goldAry = array(
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Gold'],
					    'status' => 'A'
					    );
					    db_query("INSERT INTO ?:usergroup_links ?e", $goldAry);
					    /*  Assign User to Gold Group	 */
					    /*  Remove User From Silver User Group    */
					    $deleteWhereCondition = array (
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Silver']
					    );
					    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
					    /*  Remove User From Silver User Group    */
				    }
			    }
			    elseif($userTotalRewardPoints > 2500)
			    {
				    /* Silver User Group */
				    if(isset($curUserGroups[$userGroupIds['Silver']])) {
					    if($curUserGroups[$userGroupIds['Silver']] != 'A'){
						    /*  Update User to Silver Group	 */
						    $updateData = array (
						    'status' => 'A'
						    );
						    $whereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Silver']
						    );
						    db_query('UPDATE ?:usergroup_links SET ?u WHERE ?w', $updateData, $whereCondition);   
						    /*  Update User to Silver Group	 */
						    /*  Remove User From Bronze User Group    */
						    $deleteWhereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Bronze']
						    );
						    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
						    /*  Remove User From Bronze User Group    */
					    }
				    }
				    else {
					    /*  Assign User to Silver Group	 */
					    $SilverAry = array(
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Silver'],
					    'status' => 'A'
					    );
					    db_query("INSERT INTO ?:usergroup_links ?e", $SilverAry);
					    /*  Assign User to Silver Group	 */
					    /*  Remove User From Bronze User Group    */
					    $deleteWhereCondition = array (
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Bronze']
					    );
					    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
					    /*  Remove User From Bronze User Group    */
				    }
			    }
			    else
			    {
				    //Do Nothing
			    }
			    return true;
		    }
	    }
    }

You only need to specify all User Groups ids in $userGroupIds and Reward point's range in above function.

 

Posted 04 October 2012 - 10:06 AM #4

What a nice implementation!

It would be a good idea to connect badges to user groups via admin panel. And good idea also, if you can create different badges with images, rules, etc... via admin panel.
I think this addon is very very very useful. If you would like to "release" it as a standalone "badge for reward points" addon and you need some help to sell it, please contact me.

Regards,
Istvan