We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10165
    • 129 Posts
    I’ve been trying to get my head around this for a while - I struggle to spot php mistakes, but think I’m almost there, print_r shows all the right variables and no log errors, but can’t quite get it to update, here’s the code:
    //<?php
    global $modx;
    
    //$id = $modx->documentIdentifier;
    $table = 'modx_site_tmplvar_contentvalues';
    
        $query = 'SELECT value from '.$table.' WHERE tmplvarid = 27 AND contentid = '.$id.'';
        $rs = $modx->db->query($query);
    while( $row = $modx->db->getRow($rs) ) {
         if($row['value']!= '') {
    
    $price_without_vat = $row['value'];
    $vat = 17.5; // define % vat
        $price = ($price_without_vat + ($vat*($price_without_vat/100))); // work out the amount of vat 
        $price_with_vat = round($price, 2); // round to 2 decimal places 
    //    $output = $price_with_vat; // the final price with VAT added
    //echo $output;     
    
    // update the with VAT price field in the incVAT tv
    $e = &$modx->Event;
    switch ($e->name) {
        case "OnDocFormSave":
            $fields = array('value' => $price_with_vat);
            $modx->db->update( $fields, $table, 'tmplvarid = 28', 'contentid = "'.$id.'"' );
    //print_r($price_with_vat);
    //print_r($fields);
    //print_r($id);
    
            break;
        default :
            return; // stop here - this is very important.
            break;
    }
    
    
    } else {
            return; // stop
            break;
    			}
    }
            return; // stop
            break;
    //?>
    

      • 4041
      • 788 Posts
      [ removed non working code ]
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 10165
        • 129 Posts
        Hi Breezer, thanks I’ve tried it but still not working - I think I’ve realised what the problem might be - rather than update if there is no value then the TV doesn’t exist so I really need to be checking and doing either an insert or update I guess?

        cheers
          • 10165
          • 129 Posts
          Still no good, I’ve tried to do what I thought was the issue..
          //<?php
          
          $table = 'modx_site_tmplvar_contentvalues';
          
          // update the with VAT price field in the incVAT tv
          $e = &$modx->Event;
          switch ($e->name) {
              case "OnDocFormSave":
                  $query = 'SELECT value from '.$modx->getFullTableName('site_tmplvar_contentvalues').' WHERE tmplvarid = 27 AND contentid = '.$id.'';
                  $rs = $modx->db->query($query);
                  if($rs) {
                      $row = $modx->fetchRow($rs);
          
                      $price_without_vat = $row['value'];
                      $vat = 17.5; // define % vat
                      $price = ($price_without_vat + ($vat*($price_without_vat/100))); // work out the amount of vat
                      $price_with_vat = round($price, 2); // round to 2 decimal places
                      //$output = $price_with_vat; // the final price with VAT added
                      //echo $output;
          			
          			
          			$query2 = 'SELECT value from '.$modx->getFullTableName('site_tmplvar_contentvalues').' WHERE tmplvarid = 28 AND contentid = '.$id.'';
                  $rs2 = $modx->db->query($query2);
                  if($rs2) {
                      $row2 = $modx->fetchRow($rs2);
          			   $exists = $row2['value'];
          print_r($exists);
          			   
          			   if($exists) {
          				               $fields = array('value' => $price_with_vat);
          			                   $modx->db->update( $fields, $table, 'tmplvarid = 28', 'contentid = "'.$id.'"' );
          			   } else {
          				   			   $fields = array('value' => $price_with_vat, 'tmplvarid' => '28', 'contentid' => $id);
          		                       $modx->db->insert( $fields, $table);
          			   }
          			   
          
          //print_r($price_with_vat);
          //print_r($fields);
          //print_r($id);
                  }
                  break;
              default: '';
          return; // stop here - this is very important.
          }
          //?>
          

            • 4041
            • 788 Posts
            Yes, I think that you would have to use an insert if the vat tv doesn’t exist.

            Another question, the code refences tplvarid 27 and 28, I assume 27 is the tv for price without vat, and 28 is the price with vat?
              xforum
              http://frsbuilders.net (under construction) forum for evolution
              • 10165
              • 129 Posts
              yeah that’s right 27 is without VAT and 28 with VAT
                • 10165
                • 129 Posts
                really stuck with this damn thing, I’ve tried adding a select to check whether the incVAT tv has a value and do either an insert or an update but nothing is working now, not sure how to check what’s happening as I’m getting no errors and don’t seem to be able to get anything to print out.
                //<?php
                
                $table = 'modx_site_tmplvar_contentvalues';
                
                // update the with VAT price field in the incVAT tv
                $e = &$modx->Event;
                switch ($e->name) {
                    case "OnDocFormSave":
                        $query = 'SELECT value from '.$modx->getFullTableName('site_tmplvar_contentvalues').' WHERE tmplvarid = 27 AND contentid = '.$id.'';
                        $rs = $modx->db->query($query);
                        if($rs) {
                            $row = $modx->fetchRow($rs);
                
                            $price_without_vat = $row['value'];
                            $vat = 17.5; // define % vat
                            $price = ($price_without_vat + ($vat*($price_without_vat/100))); // work out the amount of vat
                            $price_with_vat = round($price, 2); // round to 2 decimal places
                            //$output = $price_with_vat; // the final price with VAT added
                            //echo $output;
                			
                			
                			$query2 = 'SELECT value from '.$modx->getFullTableName('site_tmplvar_contentvalues').' WHERE tmplvarid = 28 AND contentid = '.$id.'';
                            $rs2 = $modx->db->query($query2);
                            if($rs2) {
                               $row2 = $modx->fetchRow($rs2);
                			   $exists = $row2['value'];
                			   
                			   if($exists) {
                				               $fields = array('value' => $price_with_vat);
                			                   $modx->db->update( $fields, $table, 'tmplvarid = 28', 'contentid = "'.$id.'"' );
                			   } else {
                				   			   $fields = array('value' => $price_with_vat, 'tmplvarid' => '28', 'contentid' => $id);
                		                       $modx->db->insert( $fields, $table);
                			   }
                			
                //print_r($exists);
                
                        }else{
                        break;
                    default: '';
                return; // stop here - this is very important.
                        }
                
                        break;
                    default: '';
                return; // stop here - this is very important.
                }
                //?>

                  • 4041
                  • 788 Posts
                  ok, this version works finally smiley

                  Set the names of the tvs, id of the vat tv, and tax in the top area. Also make sure you check onDocFormSave on the Events tab of the plugin and you should be good to go.
                  <?php
                  // table name
                  $table = $modx->getFullTableName('site_tmplvar_contentvalues');
                  
                  // names of the tvs
                  $tv_no_vat ='price_no_vat';
                  $tv_w_vat ='price_w_vat';
                  
                  // id of the price with vat
                  $tv_w_vat_id ='2';
                  
                  // tax amount
                  $vat = 17.5;
                  
                  $e = &$modx->Event;
                  switch ($e->name) {
                      case "OnDocFormSave":
                          $vat = 17.5;
                          $tv = $modx->getTemplateVarOutput(array($tv_no_vat, $tv_w_vat), $docid=$id);
                          if($tv[$tv_no_vat] !=''){
                              $price = $tv[$tv_no_vat] + ($vat*($tv[$tv_no_vat]/100));
                              $price_with_vat = round($price, 2);
                              //$fields = array('value' => $price_with_vat);
                              if($tv[$tv_w_vat] ==''){
                                  $fields = array('value' => $price_with_vat, 'tmplvarid' => $tv_w_vat_id, 'contentid' => $id);
                                  $modx->db->insert( $fields, $table);
                              }else{
                                  $fields = array('value' => $price_with_vat);
                                  $modx->db->update( $fields, $table, 'tmplvarid='.$tv_w_vat_id.' AND contentid='.$id );
                              }
                          }
                  
                          break;
                      default: '';
                  return; // stop here - this is very important.
                  }
                    xforum
                    http://frsbuilders.net (under construction) forum for evolution
                    • 10165
                    • 129 Posts
                    Hey thanks for your effort on this Breezer, really appreciate it - I still can’t get it working though : (
                    - I’ll keep trying to suss it out

                    cheers
                      • 17422
                      • 225 Posts
                      you work on modx Evo ?

                      i try on modx revo with :
                      <?php
                      // table name
                      $table = $modx->getFullTableName('site_tmplvar_contentvalues');
                      
                      // names of the tvs
                      $tv_no_vat ='price_no_vat';
                      $tv_w_vat ='price_w_vat';
                      
                      // id of the price with vat
                      $tv_w_vat_id ='13';
                      
                      // tax amount
                      $vat = 17.5;
                          $tv = $modx->getTemplateVarOutput(array($tv_no_vat, $tv_w_vat), $docid=$id);
                              if($tv[$tv_no_vat] !=''){
                                  $price = $tv[$tv_no_vat] + ($vat*($tv[$tv_no_vat]/100));
                                  $price_with_vat = round($price, 2);
                                  //$fields = array('value' => $price_with_vat);
                                  if($tv[$tv_w_vat] ==''){
                                      $fields = array('value' => $price_with_vat, 'tmplvarid' => $tv_w_vat_id, 'contentid' => $id);
                                      $modx->db->insert( $fields, $table);
                                  }else{
                                      $fields = array('value' => $price_with_vat);
                                      $modx->db->update( $fields, $table, 'tmplvarid='.$tv_w_vat_id.' AND contentid='.$id );
                                  }
                              }


                      and work fine.

                      but i have error log :
                       [2010-08-27 11:02:18] (ERROR @ /homez.40/ideesimp/pizza-du-lion/core/model/modx/modtemplatevar.class.php : 422) PHP warning: DirectoryIterator::__construct(/homez.40/ideesimp/pizza-du-lion/core/model/modx/processors/element/tv/renders/mgr/output/) [<a href='directoryiterator.--construct'>directoryiterator.--construct</a>]: failed to open dir: No such file or directory
                      [2010-08-27 11:02:18] (ERROR @ /homez.40/ideesimp/pizza-du-lion/core/model/modx/modtemplatevar.class.php : 422) PHP warning: DirectoryIterator::__construct(/homez.40/ideesimp/pizza-du-lion/core/model/modx/processors/element/tv/renders/mgr/output/) [<a href='directoryiterator.--construct'>directoryiterator.--construct</a>]: failed to open dir: No such file or directory
                      


                      i found solution here

                      MODx Revolution 2.0.1-rc1

                      http://github.com/modxcms/revolution/commit/bfbd7b1b5036a645405546bfe9226cbaf535523f


                      Laurent