Using the "field" class

  • Posts: 9
  • Thank you received: 1
4 months 3 weeks ago #361814

-- HikaShop version -- : 5.1.0
-- Joomla version -- : 4.4.5

Hello,
I'm a relative beginner with Hikashop but I've experience with Woocommerce.
For a plugin development project I need to interact with the hikashop_field table. More precisely I want to retrieve all the values of a singledropdown field based on its field_realname and then update them.
I understand that I must start with hikashop_get('class.field'); but then I'm lost ; I've looked into the code of com_hikashop/classes/field.php but without success (and because it extends hikashopClass maybe the answer is in the base class)
Any pointer would be appreciated, thank you.

Last edit: 4 months 1 week ago by DomB22.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
4 months 3 weeks ago #361816

Hi,

I suppose you'll want to do something like this:

$class = hikashop_get('class.field');
$field = $class->get($field_column_name, $field_table);
foreach($field->field_value as $value => $valueObject) {
 echo $valueObject->title;
}

While this could allow you to, for example, run a script, to update the values of custom fields in the database, based on what you're trying to do, you might be able to use a lighter approach.
What you could do is to make an override of hikashopFieldClass ( www.hikashop.com/support/documentation/6...ntation.html#classes ) and in your extended override class, override the loadValues method in order to change the values. This way you could change the values of the field on the fly when the field is being loaded.
This way, you don't have to create a plugin. Just an override file with a few lines of code in it.

The following user(s) said Thank You: DomB22

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

  • Posts: 9
  • Thank you received: 1
4 months 3 weeks ago #361823

Thank you for the reply and the override tip, Nicolas.
Per the following code I managed to retrieve a field value based on its namedkey and no error is raised.
However the new field value with the added option is not saved into the database, I must be missing another thing?

$class = hikashop_get('class.field');
$field = $class->getField('couleur', 'product');
$lenew = new stdClass();
$lenew->value = "azur";
$lenew->disabled = 0;
$field->field_value['bleue'] = $lenew;
$class->save($field)); 

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

  • Posts: 9
  • Thank you received: 1
4 months 3 weeks ago #361825

FYI I achieved what I wanted with :

$class->addValue($field->field_id, 'bleue', 'azur', 0);
but I'm still intested in understanding my previous error.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
4 months 3 weeks ago #361826

Hi,

Ah right, I forgot about the save part. You can't use the save method directly. The field_value attribute is discarded because it isn't in a valid form. You need to set the field_value in the format that is the same as what you can see in the database.
So you'll have to do something like this:

$field_value = array();
foreach($field->field_value as $value => $object) {
 $field_value = $object->title.'::'.$value.'::'.$object->disabled;
}
$field->field_value = implode("\n", $field_value);
Then, the save call will take into account the field_value.

The addValue method is actually way better for adding / updating values. I had forgotten about it.

Last edit: 4 months 3 weeks ago by nicolas.
The following user(s) said Thank You: DomB22

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

Time to create page: 0.078 seconds
Powered by Kunena Forum