We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10357
    • 573 Posts
    I have a tv that shows the calculation of a discounted price:

    My plugin onDocFormsave:
    <?php
    if ($resource->get('parent') == 14) {
        $price = $resource->getTVValue('price'); 
        $discountPercent = $resource->getTVValue('discount');
        $discountAmount =  $price * $discountPercent/100;
        $finalPrice =  $price - $discountAmount;
        $resource->setTVValue('finalPrice', $finalPrice);
    }
    $resource->save();
    return '';
    


    It works fine, but the tv only get's updated once the resource has been saved and the page refreshed. Is there any where to update it once the price field has been edited?
      • 3749
      • 24,544 Posts
      I think the only way to do that would be with JavaScript. Since the plugin is already updating the DB, all the JS would have to do is update what's on the screen.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        What if you used OnBeforeDocFormSave, and modified the TV field values to be saved?
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 3749
          • 24,544 Posts
          I think the OP wants to update the field as soon as the price field is edited, without the page reloading.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 10357
            • 573 Posts
            Correct, I did not know you could use javascript in the backend - I guess this is a done in a plugin on something like OnDocFormRender?
              • 3749
              • 24,544 Posts
              Yes, you can use JS in the Manager, but it's not fun. wink

              OnDocFormRender is probably the event you want. Unfortunately, the ExtJS used in the Manager creates much of the form on the fly, so if you look at the source, you may not see much. You have to look at the source in Firebug or something similar to find out the ids of the fields you want to modify.

              You may also have to load JQuery unless you want to use plain JS or ExtJS/modExt, which is already loaded.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 47243
                • 30 Posts
                Hi,
                been struggling with getting et setting tv values too until i found this post https://github.com/modxcms/revolution/issues/11486
                which is a workaround to get the real uncached values of the tv.
                Indeed it seems that getTVValue is getting the cached version.

                So instead of using getTVValue, use

                $tvValue = $modx->getObject('modTemplateVarResource', array(
                    'tmplvarid' => $tvId,
                    'contentid' => $resourceId,
                ));
                
                return $tvValue->get('value');


                  • 3749
                  • 24,544 Posts
                  Just be aware that that won't work if the TV is set to its default value (there won't be a modTemplateVarResource object), or if it's a TV that needs rendering (e.g., @ bindings, image TV's, etc.).

                  BTW, this will do the same thing as the code above, but will be slightly quicker and use less memory because it doesn't retrieve the object, just the field value:

                  $query = $modx->newQuery('modTemplateVarResource', array(
                      'contentid' => $resourceId,
                      'tmplvarid' => $tvId,
                  ));
                  $query->select('value');
                  return $modx->getValue($query->prepare());
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting