1

So I'm having this entry (a product) that contains a selectbox field with two values:

  • Reserved
  • Available

By default this field is set to 'Available', but when the user lands on a certain page (a thank you page after "ordering" this product with the entry id as a querystring in the URL), I'd like to change the value of that selectbox to 'Reserved'.

Anyone can put me in the right direction on this one?

Thanks in advance!

4
  • Do you want to resave the entry or just want to display certain things based on url parameter? Do you want it to save automatically or do you want to create a form? Commented Jun 25, 2018 at 20:14
  • Resaving the entry automatically at page load of the "/thank-you?id=33" page (with 33 being the entry id of that product)
    – mdmngz
    Commented Jun 25, 2018 at 21:00
  • 2
    Do you want to use twig or would you use PHP for it? Commented Jun 25, 2018 at 21:00
  • Twig, please! If PHP can be avoided, the better :)
    – mdmngz
    Commented Jun 26, 2018 at 6:59

1 Answer 1

1

Twig

You can check for certain URL parameters via

{% set param = craft.app.getRequest().getParam('id') %}
{% if param is not null %}
    {% set entry = craft.app.getElements().getElementById(param) %}
    {# insert your new field value! not the label, the value/handle
    {% do entry.setFieldValue('fieldHandle', 'Reserved') %}
    {% do craft.app.getElements().saveElement(entry) %}
{% endif %}

However I would not recommend doing this in Twig

PHP

You can see how to use Template Variables and create your boilerplate at pluginfactory.io

{% do craft.myAwesomePlugin.saveEntry(entry, {fieldHandle: 'yourValue'}) %}

And your function could be

public function saveEntry($entry, $attributes = []){
    $entry->getContent()->setAttributes($attributes);
    craft()->entries->saveEntry($entry);
}

That's just a "quick" version, I suggest you to take a look at EntriesController to see how Craft handles it in detail

4
  • Thanks! I can see by the "craft.app.xxx" that this is Craft 3 code? The project still runs on Craft 2, is this also available in 2? Why wouldn't you recommend doing this in Twig?
    – mdmngz
    Commented Jun 26, 2018 at 7:11
  • No it does not work in Craft 2 - Twig is a template engine and not supposed to contain to much logic. You only have limited opportunities for error handling so if something unexpected happens you can't handle it really well Commented Jun 26, 2018 at 7:14
  • Yes, I understand, you asked if I wanted to use Twig or PHP so I thought I had a choice and would avoid PHP if possible :) Can you set me on the way on how to do this with Craft 2? I assume this needs a custom plugin? I'm not afraid to write some PHP code, but just need to know some steps in order to get this done
    – mdmngz
    Commented Jun 26, 2018 at 7:30
  • I've edited the answer Commented Jun 26, 2018 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.