Address dropdown override

  • Posts: 48
  • Thank you received: 3
3 weeks 4 days ago #364126

-- HikaShop version -- : 5.1.1
-- Joomla version -- : 5.2.0
-- PHP version -- : 8.2

hi
I need to add a class to dropdown fields in checkout address block with overrides

earlier i created overrides for filters and ... as described in developer docs and it worked great

but when i do the same for field.override.php it doesnt work

please see the attached screenshot ..

Attachments:

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

  • Posts: 82823
  • Thank you received: 13370
  • MODERATOR
3 weeks 4 days ago #364129

Hi,

The field class file is special. It has the main class hikashopFieldClass which you can override normally. But it also have one class for each field type. It also has parent type classes like hikashopFieldDropdown and hikashopFieldItem which the type classes extend from.

So, suppose that your field uses the "single dropdown" type. The class which will be used is hikashopFieldSingledropdown which extends from hikashopFieldDropdown. When a field of that type has to be rendered, the _getFieldTypeClass method of hikashopFieldClass will look for the class hikashopFieldSingledropdown and load it.
If that class extends from hikashopFieldDropdown it will use it, even if you have a hikashopFieldDropdownOverride class defined.

So knowing all that, you need to do two things:
1. create an override class of hikashopFieldClass and override _getFieldTypeClass to look for hikashopFieldSingledropdownOverride and try to use it instead of the default class.
2. create hikashopFieldSingledropdownOverride as a copy of hikashopFieldSingledropdown and extend it from hikashopFieldDropdownOverride instead of hikashopFieldDropdown if you want it to use your override class.

Potentially, we could add by default a few lines of code in _getFieldTypeClass to look for an override class for the type classes. That way, developers would only have to handle that second point.
We didn't do it because HikaShop actually already has a field API which allows developers to add their own custom types to HikaShop:
www.hikashop.com/support/documentation/6...entation.html#fields
The plugins/hikashop/datepicker_field/ plugin is a good example of this as this is the plugin responsible for adding the "Advanced date picker" type to HikaShop.
A custom type can extend an existing type if customization is necessary.

Now, I feel like you're going about this the wrong way.
Why not create one or two view overrides where the custom address fields are displayed and just inject the class you want ?
You could for example edit the view file checkout / show_block_address via the menu Display>Views and change the line:

<?php echo $html; ?>
to:
<?php echo str_replace('hikashop_field_dropdown','hikashop_field_dropdown uk-select', $html); ?>

In fact, in your view override, I would rather recommend to remove all the code and replace it with something like this:
<?php
ob_start();
include HIKASHOP_ROOT.'components/com_hikashop/views/checkout/tmpl/show_block_address';
$html = ob_get_clean();
echo str_replace('hikashop_field_dropdown','hikashop_field_dropdown uk-select', $html);
Doing it like this means your customization is really minimal. Updates to the original show_block_address view file will still be taken into account so your customization is much more resilient to updates in the long term.

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

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

Time to create page: 0.061 seconds
Powered by Kunena Forum