Displaying custom fields with php

  • Posts: 64
  • Thank you received: 6
  • Hikashop Business
3 years 1 week ago #331686

-- HikaShop version -- : Business 4.4.1
-- Joomla version -- : Joomla! 3.9.23 Stable

Hello,
quick question with no hurry.
How to get user custom field value in the product page?
Tried this but without success:
<?php
$user = hikashop_loadUser(true);
echo $user->custom_field; (using the CF column name)
?>


var_dump($user) -> NULL

Going further and deeper.
I'd like understand how to show custom fields in the different views of Hikashop using php.
What I need to do is to create some variables, based on custom fields, to change what the view will show to the user.
Right now I have just 2 main groups of users: customers and resellers (+ guest)
Customers/Guests need to see just final price (vat included) and "per unit" text under it.
Resellers need to see the base price (no Vat) and "per box" text under it.
I've been able to use the joomla user groups to control this situation, using this code (I post it here, cause maybe it could help someone else)
$user   = JFactory::getUser();
$groups = JAccess::getGroupsByUser($user->id);
$groups_list = '(' . implode(',', $groups) . ')';

$db = JFactory::getDbo();
$query = $db->getQuery(true)
	->select($db->qn('title'))
	->from('#__usergroups')
	->where($db->qn('id') . ' IN ' . $groups_list);
$db->setQuery($query);
$rows = $db->loadObjectList();
$grossista = false;
$gruppo ='';
$ids = array_column($rows, 'title');
foreach ($ids as $id) {
	switch($id) {
		case 'Grossisti':
			$grossista = true;
			$gruppo = 'grossisti';
		break;
		case 'Privati':
			$gruppo = 'privati';
		break;
		case 'Guest':
			$gruppo = 'guest';
		break;
	}
}
(Maybe it could be better written, but I'm not a developer)
Now, this is not a neat solution: if something changes I need to remember all the occurrences where I've injected the code.
Moreover, we're planning to add a new group: "Dropshippers" that will have its own prices and maybe other features.

What I'm planning to achieve:
I already have a user custom field: user_type (dropdown) with 2 value: customer / reseller; controlled by admin panel.
Simply adding a new value in this dropdown could help a lot cause I could avoid using the Joomla Groups and I could control what to show in the different situation, if I'll be able to recall that value everywhere in Hikashop views.
I'll also need to add a Boolean (Vat/No Vat) to better control how the price will be shown and a mass action that control the boolean based on the user_type custom value. (already have one that changes the Joomla Group the user belong to as I modify the user_type value).
It seems to me, that, by this way, I'll be better organized for future changes and I could clean up a bit my override's codes.

Thanks!
Davide

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

  • Posts: 81513
  • Thank you received: 13068
  • MODERATOR
3 years 1 week ago #331690

Hi,

The code

<?php
$user = hikashop_loadUser(true);
echo $user->custom_field; (using the CF column name)
?>
is what I would have recommended.
So I can only guess that there is either a problem with where that code is placed in the view override, or with the user you're using for your tests.
if var_dump($user); outputs null, then either the current user is not logged in, or there is a synchronization issue between the Joomla user account and the user entry in HikaShop, or the hikashop_loadUser function is not called because of the area were it is placed is not run by PHP and only the var_dump code is run.

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

  • Posts: 64
  • Thank you received: 6
  • Hikashop Business
3 years 1 week ago #331793

Today it decided that it was a good day to start showing the custom field.
Nice!
Sorry for wasting your time unnecessarily.
Thank you, as always

Davide

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

Time to create page: 0.061 seconds
Powered by Kunena Forum