News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Plugin to Re-assign customers to a certain group based on order history [solved]

Started by Peter Pillen, October 10, 2012, 14:55:17 PM

Previous topic - Next topic

Peter Pillen

Situation:

I have succesfully set up 10 different shoppergroups and 10 different calculation rules to work together. For example if a user/customer is assigned to shoppergroup "level 6", they get a 6% discount on all purchased items in the shop... and so on. I'm going to assign each shopper to a certain shopper group based on their order history. You could do this manually after each purchase, but I would like to make virtuemart do this automatically when an user logs in.

Problem:
I can write all the necessary php code and mysql statements myself, but I want my script to execute only once when the user logs in. I just need some pointers which file would be the best to add this code to and how I can catch the login event.

Question 1: I have been looking in the files modules/mod_login but i'm not sure if that is the right place to make the adjustments.
Question 2: Could I catch the login event with $_POST["username"]? In that case I could just add the code somewhere in the template, but that wouldn't be a "clean" solution.

Any pointers are very much appreciated.

Peter Pillen

It just hit me that virtuemart is using the joomla login module, so maybe i'll have to search in the Joomla forum.

Peter Pillen

Found the solution!  8)

Just a question for the forummod: I will be making more plugins in the future to fit my needs, but is this the right forum for me to get help about this kind of problems or should I ask this in another forum?

The solution (for those who are interested):

I have made a plugin that is activated the moment a user logs in, checks the order history of a customer and reassigns the customer to a new shoppergroup according to the parameters that are set.

Files
* directory "fidelity" needs to be created in plugins/user/
* file  plugins/user/fidelity/fidelity.php
* file  plugins/user/fidelity/fidelity.xml
* file  plugins/user/fidelity/index.html

fidelity.php sourcecode
<?php
/**
 * @copyright Copyleft (C) 2012 Open Source Matters, Inc. All rights reserved.
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access
defined('_JEXEC') or die;

/**
 * Fidelity User plugin
 *
 * @package Fidelity.Plugin
 * @subpackage User.fidelity
 * @since 2.5
 */
 
 //IMPORTANT: this plugin must be placed after the joomla login plugin in the administrator backend in order to work
 
class plgUserFidelity extends JPlugin
{
public function onUserLogin()
{
//get the user id that is loaded into the session by joomla login
$fidelity_user=& JFactory::getUser();
$fidelity_user_id=$fidelity_user->id;

//get the current shoppergroup id from the user
$db JFactory::getDBO();
$db->setQuery("SELECT `virtuemart_shoppergroup_id` FROM `#__virtuemart_vmuser_shoppergroups` WHERE `virtuemart_user_id` = ".$fidelity_user_id.";");
$current_shoppergroup_id $db->query();

//make an array with the orders made by this user
$db JFactory::getDBO();
$db->setQuery("SELECT * FROM `#__virtuemart_orders` WHERE `virtuemart_user_id` = ".$fidelity_user_id.";");
$shoppers_orders $db->loadAssocList();

//if orders were found, do the math to check if the shoppergroup is still correct
if(!empty($shoppers_orders)){

//determine the new shoppergroup id with the given parameters
$order_age=$this->params->get('order_age');
$required_status=$this->params->get('required_status');
$treshold=$this->params->get('treshold');
$punishment_limit_days=$this->params->get('punishment_limit_days');
$punishment_coefficient=$this->params->get('punishment_coefficient');
$default_level=$this->params->get('default_level');
$max_level=$this->params->get('max_level');

$last_date="2000-01-01 00:00:00";
$end_date=date("Y-m-d H:i:s",strtotime('-'.$order_age.' month'strtotime(date("Y-m-d H:i:s"))));
$order_total=0;
foreach($shoppers_orders as $order){
if($order['order_status']==$required_status AND $order['created_on']>$end_date){
$order_total=$order_total+$order['order_salesPrice'];
if($last_date<$order['created_on']){
$last_date=$order['created_on'];
}
}
}
unset($order_amount);

$days_between_orders ceil(abs(strtotime($last_date) - strtotime(date("Y-m-d H:i:s"))) / 86400);
$punishment=floor($days_between_orders/$punishment_limit_days);
$new_shoppergroup_level_without_punishment=floor($order_total/$treshold);
$new_shoppergroup_level=$new_shoppergroup_level_without_punishment-($punishment*$punishment_coefficient);

//check if the default shopper group level needs to be set or that the maximum level is not exceeded
if($new_shoppergroup_level<1){
$new_shoppergroup_level_name="'".$default_level."'";
}elseif($new_shoppergroup_level>$max_level){
$new_shoppergroup_level_name=$max_level;
}else{
$new_shoppergroup_level_name=$new_shoppergroup_level;
}

//now lookup the correct shopper group id to go with the new shopper group level
$db JFactory::getDBO();
$db->setQuery("SELECT `virtuemart_shoppergroup_id` FROM `#__virtuemart_shoppergroups` WHERE `shopper_group_name` = ".$new_shoppergroup_level_name.";");
$new_shoppergroup_id $db->loadResult();

//update the users shoppergroup if neccesary
if($current_shoppergroup_id!==$new_shoppergroup_id){
$db JFactory::getDBO();
$db->setQuery("UPDATE ".$db->quoteName('#__virtuemart_vmuser_shoppergroups')." SET ".$db->quoteName('virtuemart_shoppergroup_id')." = ".$new_shoppergroup_id." WHERE ".$db->quoteName('#__virtuemart_vmuser_shoppergroups').".".$db->quoteName('virtuemart_user_id')." = ".$fidelity_user_id." LIMIT 1;");

$result $db->query();
}
}
return true;
}
}


fidelity.xml sourcecode
<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="user">
<name>Virtuemart Shopperfidelity</name>
<author>Peter Pillen</author>
<creationDate>October 2012</creationDate>
<copyright>(C) copyleft</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>peterpillen@gmail.com</authorEmail>
<authorUrl>www.pillini.be</authorUrl>
<version>2.5.0</version>
<description>Virtuemart Shopperfidelity descr</description>
<files>
<filename plugin="fidelity">fidelity.php</filename>
<filename>index.html</filename>
</files>
<languages>
<language tag="en-GB">en-GB.plg_user_joomla.ini</language>
<language tag="en-GB">en-GB.plg_user_joomla.sys.ini</language>
</languages>
<config>
<fields name="params">
<fieldset name="basic">
<field name="max_level" type="text" default="5" label="Highest level of shoppergroup" description="Number/name of the highest level of shoppergroup created in virtuemart"/>
<field name="default_level" type="text" default="-default-" label="Name of default shoppergroup" description="Name of the default level of shoppergroup created in virtuemart"/>
   <field name="required_status" type="text" default="S" label="Orderstatus" description="Which status must the orders have to be considered for the fidelity discount. Default is 'S'(shipped) so only orders that have actually been shipped to the customer are taken in consideration."/>
   <field name="order_age" type="text" default="24" label="Order age in months" description="The maximum age of the orders (expressed in months) that need to be taken in consideration for the fidelity discount"/>
<field name="treshold" type="text" default="100" label="Reward treshold" description="The order amount per which the customer recieves a level raise. Example: if treshold is $100 and customer orders $350, he will get a 3 level raise."/>
<field name="punishment_limit_days" type="text" default="365" label="Punishment after x-days" description="The maximum number of days between orders to not recieve a shopper level punishment."/>
<field name="punishment_coefficient" type="text" default="1" label="Punishment coëfficient" description="The number of levels a customer should be punished for exceeding the numbers of days set. Example: if set to '3', the customer will drop 3 levels."/>
</fieldset>
</fields>
<params></params>
</config>
</extension>


index.html is a standard blanc page as in the joomle plugin... just copy/paste it

Administrator setup shoppergroups
!the name of shoppergroups must be numbers



and then make a calculation rule for every shopper group.

The plugin
!the plugin must be loaded by the "discover" option to be installed
!the plugin fidelity must be ranked after the joomla plugin in the plugin overview



the settings of plugin as seen above, does the following:
The plugin looks at all orders with the order status "pending" made by the logged in customer in the last 36 months. The customer gets a higher shoppergroup for every €50 bought in the webshop. And if he has not bought anything in the past 365 days, he loses 2 levels in the shoppinggroup. If he has bought a lot whereby he surpasses the highest level, he just gets the highest level (in the example '10'). And if he has no right to be in a shoppergroup, he just gets the '-default-' shoppinggroup.

It needs a bit of tweeking... but... YAYy! My first working plugin  ;D



jenkinhill

Kelvyn
Lowestoft, Suffolk, UK

Retired from forum life November 2023

Please mention your VirtueMart, Joomla and PHP versions when asking a question in this forum