On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • I have a shopping cart in Evo 1.0.8.
    In the code block below, between lines 32 to 44, I am trying to retrieve the 4 items from a row in a table in the database and add them to the session array created at lines 22 to 28. Without the added code (32 to 44) the file works fine, however with the code added nothing is returned and the cart is empty.
    Any help to show me where my error is would be appreciated.

    // [[orderDetailsInToSESSION]]
    
    if ( empty($_GET['customer']) ) return "no customer account specified";
    else {
        // sanitize and validate the input type to make sure it's what you are expecting;
        $customer = $modx->db->escape($_GET['customer']);
        $orderId = $modx->db->escape($_GET['orderId']);
        // get the details from the orderId
        $db_query = $modx->db->select("productId, quantity", $modx->getFullTableName('customer_order_details'), "orderId='" . $orderId . "'");
        if ($modx->db->getRecordCount($db_query) > 0) {
            while ($row = $modx->db->getRow($db_query)) {
                // get the current prices and names of the products previously ordered
                $document_tvs = $modx->getTemplateVars(array("pagetitle", "price", "specialPrice"), "name", $row['productId']);
    
             // to put the returned array into a simpler/easier to use array use the following
                   if (is_array($document_tvs)) {  
                   foreach( $document_tvs as $document_TV) {
                    $docTVArray[$document_TV['name']] = $document_TV['value'];
                   }
                  }
    
              // put these details into the session
             $_SESSION['shoppingCartContents'][] = Array (
                  'productId' => $row['productId'],
                 'productTitle' => $docTVArray['pagetitle'],
                  'price' => ( !empty( $docTVArray['specialPrice']) ) ?  $docTVArray['specialPrice'] :  $docTVArray['price'],
                  'quantity' => $row['quantity']
              );
            }
    	}
    		
    	// if the productId is 219 get the 4 items in the row and add to session array
    		if ($_SESSION['shoppingCartContents']['productId'] == 219) {
    		$db_query = $modx->db->SELECT("hummusOne, hummusTwo, mayo, jam", $modx->getFullTableName('customer_order_giftpack_details'), "orderId='" . $orderId . "'");
    		if ($modx->db->getRecordCount($db_query) > 0) {
                      while ($row = $modx->db->getRow($db_query)) {
    		    $_SESSION['shoppingCartContents']['hummusOne'] => $row['hummusOne'],
    		    $_SESSION['shoppingCartContents']['hummusTwo'] => $row['hummusTwo'],
    		    $_SESSION['shoppingCartContents']['mayo'] => $row['mayo'],
    		    $_SESSION['shoppingCartContents']['jam'] => $row['jam']
                 );
    	   }
            }
        } // end of product219 if
     }
    

    This question has been answered by breezer. See the first response.

    [ed. note: Twobears last edited this post 8 years, 5 months ago.]
    • discuss.answer
      Give this a try:
          // if the productId is 219 get the 4 items in the row and add to session array
          if ( $_SESSION['shoppingCartContents']['productId'] == 219 ) {
      
              $items = $modx->db->getRow( $modx->db->select( 'hummusOne, hummusTwo, mayo, jam', $modx->getFullTableName( 'customer_order_giftpack_details' ), 'orderId="' . $orderId .'"' ) );
      
              if( $items ){
              
                  $_SESSION['shoppingCartContents']['hummusOne'] = $items['hummusOne'];
                  $_SESSION['shoppingCartContents']['hummusTwo'] = $items['hummusTwo'];
                  $_SESSION['shoppingCartContents']['mayo']      = $items['mayo'];
                  $_SESSION['shoppingCartContents']['jam']       = $items['jam'];
              
              }
      
          } // end of product219 if
        xforum
        http://frsbuilders.net (under construction) forum for evolution
      • Hi Breezer
        Thanks for your help with this. Used your suggestion with a slight modification. Success, many thanks.
        // if the productId is 219 get the 4 items in the row and add to session array
        if ( $_SESSION['shoppingCartContents'][0]['productId'] == 219 ) {
         
            $items = $modx->db->getRow( $modx->db->select( 'hummusOne, hummusTwo, mayo, jam', $modx->getFullTableName( 'customer_order_giftpack_details' ), 'orderId="' . $orderId .'"' ) );
         
            if( $items ){
             
                $_SESSION['shoppingCartContents'][0]['hummusOne'] = $items['hummusOne'];
                $_SESSION['shoppingCartContents'][0]['hummusTwo'] = $items['hummusTwo'];
                $_SESSION['shoppingCartContents'][0]['mayo']      = $items['mayo'];
                $_SESSION['shoppingCartContents'][0]['jam']       = $items['jam'];
             
            }
         
          } // end of product219 if
        
        [ed. note: Twobears last edited this post 8 years, 5 months ago.]
        • I find I much prefer using hummus in tuna salad or egg salad than mayo, but it doesn't work at all for coleslaw!
            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
          • Glad it helped smiley

            If that is the entire snippet here is a slightly condensed version I put together (untested) that might work if you're interested:
            // [[orderDetailsInToSESSION]]
            
            if ( empty( $_GET['customer'] ) ){
            
                return "no customer account specified";
            
            }else{
            
                // sanitize and validate the input type to make sure it's what you are expecting;
                $customer = $modx->db->escape( $_GET['customer'] );
            
                $orderId = $modx->db->escape( $_GET['orderId'] );
            
                // get the details from the orderId
                $order_details = $modx->db->getRow( $modx->db->select( 'productId, quantity', $modx->getFullTableName( 'customer_order_details' ), 'orderId="' . $orderId .'"' ) );
            
                if( $order_details ){
                
                    // get the current prices and names of the products previously ordered
                    $doc_tvs = $modx->getTemplateVars( array( 'pagetitle', 'price', 'specialPrice' ), 'name', $order_details['productId'] );
            
                    // put these details into the session
                    $_SESSION['shoppingCartContents'][] = Array (
                            'productId'     => $order_details['productId'],
                            'productTitle'  => $doc_tvs['pagetitle'],
                            'price'         => ( !empty( $doc_tvs['specialPrice'] ) ) ?  $doc_tvs['specialPrice'] :  $doc_tvs['price'],
                            'quantity'      => $order_details['quantity']
                            );
            
                    // if the productId is 219 get the 4 items in the row and add to session array
                    if( $order_details['productId'] == 219 ){
            
                        $items = $modx->db->getRow( $modx->db->select( 'hummusOne, hummusTwo, mayo, jam', $modx->getFullTableName( 'customer_order_giftpack_details' ), 'orderId="' . $orderId .'"' ) );
            
                        if( $items ){
            
                            $_SESSION['shoppingCartContents'][0]['hummusOne'] = $items['hummusOne'];
                            $_SESSION['shoppingCartContents'][0]['hummusTwo'] = $items['hummusTwo'];
                            $_SESSION['shoppingCartContents'][0]['mayo']      = $items['mayo'];
                            $_SESSION['shoppingCartContents'][0]['jam']       = $items['jam'];
            
                        }
                    }
                }
            }
              xforum
              http://frsbuilders.net (under construction) forum for evolution