Assigning User Group on order creation

  • Posts: 36
  • Thank you received: 6
1 year 5 months ago #357553

-- HikaShop version -- : 5.0.2
-- Joomla version -- : 4.4.1
-- PHP version -- : 8
-- Browser(s) name and version -- : Firefox 120.0.1 (64-bit)

Hi,

I want to assign a user group when a customer orders a certain product.

This is possible by setting "User group after purchase" for the product in question.

However, the user group is only assigned once the order is set to confirmed.

I am aware of the possible implications, but still want to assign user groups immediately after the order is created.

How would I go about doing this?

Please Log in or Create an account to join the conversation.

  • Posts: 83811
  • Thank you received: 13571
  • MODERATOR
1 year 5 months ago #357566

Hi,

You would have to edit the file plugins/hikashop/group/group.php and change the line:

if($order->order_status != $confirmed){
to:
if($order->order_status != 'created'){
That way, the group would be added when the order is created.

The following user(s) said Thank You: NTV

Please Log in or Create an account to join the conversation.

  • Posts: 36
  • Thank you received: 6
1 year 5 months ago #357572

Hi,

works fine, thanks a lot!

But this change will get overridden with the next update to group.php - correct?
If so, is there a way to make an override like with Views?

Please Log in or Create an account to join the conversation.

  • Posts: 83811
  • Thank you received: 13571
  • MODERATOR
1 year 5 months ago #357574

Hi,

That's correct.
You can make an override. Basically, you need to rename the plugin. Change its folder / files / class to new names based on the Joomla documentation on how to make plugins so that you basically have your own custom user group after purchase plugin.
This requires some development knowledge.

Please Log in or Create an account to join the conversation.

  • Posts: 36
  • Thank you received: 6
1 year 5 months ago #357682

Hi,

I think that could be in the realm of my capabilities. Would I need to rename or copy the original Hika Plugin?

If I rename it, would the original not be added again with the new update?

And if I copy it, how would I stop the original from running as well?

Last edit: 1 year 5 months ago by NTV.

Please Log in or Create an account to join the conversation.

  • Posts: 83811
  • Thank you received: 13571
  • MODERATOR
1 year 5 months ago #357690

Hi,

You would need to copy the plugin. The original plugin will be updated / added back automatically after each update, so if you don't want to use it, you want to disable it in the Joomla plugins manager.

The following user(s) said Thank You: NTV

Please Log in or Create an account to join the conversation.

  • Posts: 36
  • Thank you received: 6
1 week 1 hour ago #367110

Hi,
digging this up again, because we tried to delegate the assigning of user groups to HikaSerial and discovered that HikaSerial is not an option for us (yet).

However, I had another idea:
Could I create an onAfterOrderCreate() function in a custom plugin, check if there are user groups associated with the product and then set it myself, leaving the original HikaShop code untouched?

Sound counter-intuitive, but for me it would be less overhead and also future proof (since we already have a custom plugin).

Something like this:

        foreach ($order->cart->full_products as $product) {
	    $group = $product->product_group_after_purchase;
            if ($group != '') {
		$user = $userClass->get($order->order_user_id);
                $email = $user->user_email;
                
                // Find Joomla user via email
                $db = JFactory::getDbo();
                $query = $db->getQuery(true)
                    ->select($db->quoteName('id'))
                    ->from($db->quoteName('#__users'))
                    ->where($db->quoteName('email') . ' = ' . $db->quote($email));
                $db->setQuery($query);

                if ($id = $db->loadResult()) $user = JFactory::getUser($id);
                else return;
                UserHelper::addUserToGroup($user->id, $group);
            }
        }
Tested it, seems to work.

Any problems this may cause that I'm not aware of?

Last edit: 6 days 22 hours ago by NTV.

Please Log in or Create an account to join the conversation.

  • Posts: 83811
  • Thank you received: 13571
  • MODERATOR
6 days 20 hours ago #367114

Hi,

Well, that's basically similar to what I was suggesting in my previous message when I told you you could make a copy of the group plugin of HikaShop.
Whether you make a copy of the plugin, or you just copy / paste the onAfterOrderCreate function from the code of the plugin to your custom plugin and just apply the modification I proposed in my first message, either of these are fine.

The following user(s) said Thank You: NTV

Please Log in or Create an account to join the conversation.

Time to create page: 0.071 seconds
Powered by Kunena Forum
loading