Automatically add an option as selected

  • Posts: 69
  • Thank you received: 1
  • Hikashop Business Hikashop Essential
2 weeks 2 days ago #372241

-- HikaShop version -- : 6.3.0
-- Joomla version -- : 5.4.6

We have software products which have additional options, one of which is 12 months Support

We have got the "Support" option showing with the peroduct in the frontend (we have linked it an an "Option") but it's a selected option. We need to be able have have the "Support" option be set to "selected" by default. How can this be done please?

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

  • Posts: 85796
  • Thank you received: 14089
  • MODERATOR
2 weeks 2 days ago #372242

Hello,

There is no setting for this yet, but we will add one in the next release of HikaShop: you will be able to mark a product as selected by default when it is used as an option, directly on the product edit page.

In the meantime, you can do it with a small override of the "option" view. Go to Display > Views, open the product "option" view and save it once so HikaShop creates the override (your change is then kept on updates).

In that file, find the line that builds the option selector, it passes (int)$value as the selected value and looks like this (the helper may read JHtml or HTMLHelper depending on your version):

$html = JHtml::_('select.'.$select.'list', $option_values, $map, $attribs, 'value', 'text', (int)$value, $id);

Just above that line, add:
if(empty($optionElement->variants) && $optionElement->product_id == 123)
    $value = $optionElement->product_id;

Replace 123 with the id of your "Support" option product. That option will then be pre-selected on the product page, and the customer can still unselect it. To pre-select every option that has no variants, remove the "&& $optionElement->product_id == 123" part.

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

  • Posts: 69
  • Thank you received: 1
  • Hikashop Business Hikashop Essential
2 weeks 1 day ago #372255

Thank you for the reply. What happens if a product has a different option which we need to be selected by default?

For example, we have the following products:

Software Student (Product ID: 6)
Software Essentials (Product ID: 1)
Software Premium (Product ID: 3)
Software Advanced (Product ID: 5)
Software Sheep (Product ID: 44)

We then have a tailored Support package which we need to set to "selected" for each individual one:

Support for Software Student (Product ID: 39)
Support for Software Essentials (Product ID: 36)
Support for Software Premium (Product ID: 13)
Support for Software Advanced (Product ID: 13)
Support for Software Sheep (Product ID: 45)

So for example:

'Software Essentials (Product ID: 1)' needs to have 'Support for Software Essentials (Product ID: 36)' set to selected by default

'Software Premium (Product ID: 3)' needs to have 'Support for Software Premium (Product ID: 13)' set to selected by default

Software Student (Product ID: 6) needs to have Support for Software Student (Product ID: 39) set to selected by default

and so on...

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

  • Posts: 85796
  • Thank you received: 14089
  • MODERATOR
2 weeks 1 day ago #372257

Hi,

With several option products to pre-select, you do not want to hardcode each id in the override. The clean way is a custom product field that flags which products are pre-selected, then you check that field instead of a fixed id.

1. Go to Display > Custom Fields and create a new field with Table set to "Product" and type "Single dropdown", with two values: 0 for "No" and 1 for "Yes" (default "No"). Give it a column name, for example option_default. Publish it. It then appears on every product edit page.

2. Edit each Support product (39, 36, 13, ...) and set that new field to "Yes". Leave all the other products on "No".

3. In your option view override, replace the fixed id test with the field:

if(empty($optionElement->variants) && !empty($optionElement->option_default))
	$value = $optionElement->product_id;

Use the column name you chose in place of option_default. From then on you decide which options are pre-selected directly from the product edit page, with no further code change when you add a new product.

If you would rather not add a field, you can instead list the ids in the override and add to the list over time:
$preselect = array(39, 36, 13);
if(empty($optionElement->variants) && in_array($optionElement->product_id, $preselect))
	$value = $optionElement->product_id;

Note that, thinking about this, we've decided to add extra options to products for this in the next version of HikaShop.
So, ideally, I would recommend switching to the new options after you update HikaShop to 6.5.1 (or a later version).

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

Time to create page: 0.065 seconds
Powered by Kunena Forum