Page 1 of 2

Database query help please

Posted: Sun Apr 02, 2023 7:19 pm
by bestmate
Please can someone guide me how to query the database products_options_values table to get the product_options_values_name using product_options_values_id value?

Thank you.

Re: Database query help please

Posted: Sun Apr 02, 2023 8:13 pm
by Kofod95
I would think something like

Code: Select all

SELECT products_options_values_name FROM products_options_values WHERE products_options_values_id = " . (int)$the_id_you_want_the_name_for . " AND language_id = " . (int)$_SESSION['languages_id']
Would do it. I don't remember whether the language_id is fetched differently in catalog compared to admin, but if memory serves, the above goes for admin at least.

//Daniel

Re: Database query help please

Posted: Sun Apr 02, 2023 10:06 pm
by bestmate
Thanks very much @Kofod95 , very much appreciated.

Another question if I may. How can I pass a selectbox value/option to it using a link?

Re: Database query help please

Posted: Mon Apr 03, 2023 6:22 am
by Kofod95
I'm am a little unsure about what it is you want to do, but if you just want the ability to create links that will show the name of the option_value, you could populate the variable in the above query like

Code: Select all

$the_id_you_want_the_name_for = $_GET['attribute_value_id'];
and create the link like

Code: Select all

$Linker->build(page.php, ['attribute_value_id' => $the_value]);
You may need to use $GLOBALS['Linker']->build(...
$Admin->link(...
Or $GLOBALS['Admin']->link(...
depending on the context.
Also, I may be misremembering, but you could look at any nearby link in core to check for the correct syntax :)

//Daniel

Re: Database query help please

Posted: Mon Apr 03, 2023 7:41 am
by bestmate
Thank you again Daniel.

What I want to do is set a selectbox value using a link or JavaScript instead of clicking the selectbox. For example:

<script type="text/javascript">
$(document).ready(()=>{
$("#input_5") option[value=8]").attr('selected', 'selected');
});
</script>

Perhaps this can be done a better way or using a URL to set the value?

Re: Database query help please

Posted: Mon Apr 03, 2023 7:57 am
by Kofod95
For product_info or somewhere else?
Is it the same option you want to be preselected, or do you want the power to have different options preselected in different circumstances?
My approach to it would vary based on the outcome wanted :)

//Daniel

Re: Database query help please

Posted: Mon Apr 03, 2023 8:59 am
by bestmate
Thank you @Kofod95

Yes, this is for product_info and a different value will be passed to the selectbox each time. How would you go about this. Is using PHP an option or just JavaScript?

Re: Database query help please

Posted: Mon Apr 03, 2023 9:25 am
by burt
EG: product_info.php?products_id=4%7B1%7D2

In a new install, gets you to the product_info page for "Shiny Red Apples", with the "Box Size" of "24" pre-selected.

This piece: %7B1 is the Option (here is option "1"), and this piece is the Attribute %7D2 (here is attribute "2"). If you have multiple Options, then the URL would end up longer, something like this;

product_info.php?products_id=4%7B1%7D2%7B2%7D4

Which says product_info page for "Shiny Red Apples", with the "Box Size" [%7B1] of "24" [%7D2] pre-selected and "another option" [%7B2] of "whatever" %7D4 selected.

You can see this in action, by performing the following;

1. Go to the product info page of any product with selectable option(s).
2. Select options and Add To Cart.
3. From the Cart page, click the LINK back to the product. This link would look something like the URLS above.
4. Scroll down and see that the options are pre-selected.

Now you need to think how to use this piece of core functionality to do what you need to do.

Re: Database query help please

Posted: Mon Apr 03, 2023 9:46 am
by Kofod95
@burt's answer shows the reason I asked what you wanted - core is able to preselect attributes from link-parameters, so no need to re-invent the wheel ;)

//Daniel

Re: Database query help please

Posted: Mon Apr 03, 2023 9:54 am
by bestmate
Hello @burt . Thanks so much for this guide. I followed the steps you outlined and it works only if a product is already in the cart and you want to click back to it.

In my case, I want to make the selection before adding item to the cart. For example:

product_info.php?products_id=781{2}8{3}4

The above will not work if I just pass it in the URL if it's not already in my cart.