We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17301
    • 932 Posts
    Great smiley
      ■ email: [email protected] | ■ website: https://alienbuild.uk

      The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
      • 51020
      • 670 Posts
      Quote from: lkfranklin at May 10, 2018, 05:07 AM
      Try this:

      <!--?php
      session_start();
       
      $output = '';
      $items = '';
      $basket_total = 0;
       
      $errors = array();
      $success = array();
       
       
       
      //---------------------------------------------------------------
      // Get user profile if they are logged in
      $user = $modx--->getUser();
      $profile = $user->getOne('Profile');
       
       
       
      //-- ADDITIONAL CODE ADDED 10.05.18 - NOT WORKING
      /*$profile = $modx->user->getOne('Profile');*/
      $extended = $profile->get('extended');
      echo $extended['company'];
      //-- ADDITIONAL CODE ADDED 10.05.18 - NOT WORKING
       
       
       
       
      // Get posted variables
      $name = $profile->fullname;
      //$company = $profile->company;
      $address = $profile->address;
      $city = $profile->city;
      $postcode = $profile->zip;
      $country = $profile->country;
      $email = $profile->email;
      $phone = $profile->phone;
       
       
       
      //---------------------------------------------------------------
       
      $basket = $_SESSION['basket'];
      foreach ($basket as $key => $item) {
          $items .= $modx->getChunk('duraflex.quote.row', $item);
          $basket_total += $item['quantity'] * $item['price'];
      }
       
      //---------------------------------------------------------------
       
      if (isset($_POST['submit'])) {
       
          // Get posted variables
          $name = $_POST['name'];
          /*$company = $_POST['company'];*/
          $company = $extended['company'];
          $address = $_POST['address'];
          $city = $_POST['city'];
          $postcode = $_POST['postcode'];
          $country = $_POST['country'];
          $email = $_POST['email'];
          $phone = $_POST['phone'];
       
          // Get MODX settings
          $to = '[email protected]'; //$modx->getOption('emailsender');
          $from = $modx->getOption('emailsender');
          $site_name = $modx->getOption('site_name');
          $subject = "Duraflex: Quote request by $name";
          $message = $modx->getChunk('duraflex.quote.email', array(
              'items' => $items,
              'name' => $name,
              'company' => $company,
              'address' => $address,
              'city' => $city,
              'postcode' => $postcode,
              'country' => $country,
              'email' => $email,
              'phone' => $phone,
       
          ));
       
          // Send email: TODO
          $modx->getService('mail', 'mail.modPHPMailer');
          $modx->mail->reset();
          $modx->mail->set(modMail::MAIL_BODY, $message);
          $modx->mail->set(modMail::MAIL_FROM, $from);
          $modx->mail->set(modMail::MAIL_FROM_NAME, $site_name);
          $modx->mail->set(modMail::MAIL_SENDER, $from);
          $modx->mail->set(modMail::MAIL_SUBJECT, $subject);
          $modx->mail->address('to', $to);
          $modx->mail->address('to', '[email protected]');
          $modx->mail->setHTML(true);
       
          if (!$modx->mail->send()) {
              $modx->log(modX::LOG_LEVEL_ERROR,'[FormIt] An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
              return false;
          }
          $modx->mail->reset();
       
          $output = $modx->getChunk('duraflex.quote.confirmation', array(
              'title' => $title,
              'errors' => implode('<li>', $errors),
              'success' => implode('</li><li>', $success),
              'items' => $items,
              'name' => $name,
              'company' => $company,
              'address' => $address,
              'city' => $city,
              'postcode' => $postcode,
              'country' => $country,
              'email' => $email,
              'phone' => $phone,
          ));
       
          // Save to user account if applicable
          if ($user = $modx->getUser()) {
               
              // Save their order to the database
              if ($modx->addPackage('duraflex', MODX_CORE_PATH . 'components/duraflex/model/', 'me')) {
                  $order = $modx->newObject('Duraflex');
                  $order->set('userid', $user->get('id'));
                  $order->set('status', 'new');
                  $order->set('orderdate', date("Y-m-d H:i:s"));
                  $order->set('data', json_encode($basket));
                  $order->save();
              }
               
              // Update user profile with the details they entered?
              // if ($profile = $modx->getObject('modUserProfile', array('internalKey' => $id))) {
                  // $fullname = $profile->get('fullname');
              // }
         
               
               
          }
       
          // Destroy basket
          unset($_SESSION['basket']);
       
       
      } else {
       
          $output = $modx->getChunk('duraflex.quote', array(
              'title' => $title,
              'errors' => implode('</li><li>', $errors),
              'success' => implode('</li><li>', $success),
              'items' => $items,
              'name' => $name,
              'company' => $company,
              'address' => $address,
              'city' => $city,
              'postcode' => $postcode,
              'country' => $country,
              'email' => $email,
              'phone' => $phone,
              'basket_total' => $basket_total,
          ));
       
      }
       
      return $output;
      
      </li>

      Yep that was it - thanks again!!
        • 51020
        • 670 Posts
        Quote from: tm2000 at May 10, 2018, 05:05 AM
        GOT IT: With a bit io trial and error - I worked it out - see below if interested:
        
        // Get posted variables
        $name = $profile->fullname;
        $company = $extended['company'];
        $address = $profile->address;
        $city = $profile->city;
        $postcode = $profile->zip;
        $country = $profile->country;
        $email = $profile->email;
        $phone = $profile->phone;
        


        Thanks so much for the help LK!

        Andy

        I just found an issue with this - if the user isn't logged in - the page it's on breaks!
        This is what I have:

        $profile = $modx->user->getOne('Profile');
        $extended = $profile->get('extended');
        


        Any idea why this breaks the page if the user is not logged in?
        If i comment it out, the page works fine.
          • 17301
          • 932 Posts
          I think you'll need to put it into an if statement to account for users who don't have a profile with that information attached.
            ■ email: [email protected] | ■ website: https://alienbuild.uk

            The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
            • 51020
            • 670 Posts
            Quote from: lkfranklin at May 11, 2018, 06:49 AM
            I think you'll need to put it into an if statement to account for users who don't have a profile with that information attached.
            Thanks - that's where my phop let's bme down - would it be something along these lines:
                if ($user = $modx->getUser()) {
            
            ----
            
            }
            
            


            #clutchingAtStraws...
              • 51020
              • 670 Posts
              Quote from: tm2000 at May 11, 2018, 06:54 AM
              Quote from: lkfranklin at May 11, 2018, 06:49 AM
              I think you'll need to put it into an if statement to account for users who don't have a profile with that information attached.
              Thanks - that's where my phop let's bme down - would it be something along these lines:
                  if ($user = $modx->getUser()) {
              
              ----
              
              }
              
              


              #clutchingAtStraws...

              Or I think maybe this:

              $user = $modx->getLoginUserName();
               
              if ($user) {
                  $profile = $modx->user->getOne('Profile');
                  $extended = $profile->get('extended');
              }else{
                  //nada
              }
              


                • 17301
                • 932 Posts
                Try this:

                <?php
                
                $user = $modx->getUser();
                
                if (!$user) {
                
                	return '';
                
                } else{
                
                	// Run your code
                
                }
                  ■ email: [email protected] | ■ website: https://alienbuild.uk

                  The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
                  • 51020
                  • 670 Posts
                  Quote from: lkfranklin at May 11, 2018, 07:13 AM
                  Try this:

                  <!--?php
                  
                  $user = $modx--->getUser();
                  
                  if (!$user) {
                  
                  	return '';
                  
                  } else{
                  
                  	// Run your code
                  
                  }

                  Thanks - your code didn't work for some reason - but this does:

                  $user = $modx->getLoginUserName();
                   
                  if ($user) {
                      $profile = $modx->user->getOne('Profile');
                      $extended = $profile->get('extended');
                  }else{
                      //do nothing
                  }
                  


                  Not sure why or how - but I've tested logged in and non logged in and it works! Thanks for the steer
                    • 3749
                    • 24,544 Posts
                    I think this would work, and should be a little faster:

                    if (isset($modx->user) && $modx->user instanceof modUser) {
                        $profile = $modx->user->getOne('Profile');
                        $extended = $profile->get('extended');
                    }else{
                        //do nothing
                    }
                      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
                      • 51020
                      • 670 Posts
                      Quote from: BobRay at May 11, 2018, 11:52 PM
                      I think this would work, and should be a little faster:

                      if (isset($modx->user) && $modx->user instanceof modUser) {
                          $profile = $modx->user->getOne('Profile');
                          $extended = $profile->get('extended');
                      }else{
                          //do nothing
                      }
                      Thanks Bob - I'll swap that in if it's a more efficient way!