We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15861
    • 24 Posts
    I’m in the middle of setting up a ShopX cart and have the products working well and a product catalog page that works. Now, I’m trying to get the "Add to Cart" feature to work, and have included the ShopXCart function working. However, I’m not convinced the ShopXSession plugin is working, because the session data never actually seems to change.

    Here’s the ShopXCart call that appears on the product detail page:

    [!ShopXCart? &checkout=`4029` &debug=`GET` !]
    


    And here’s the current version of the ShopXSession that I’m using. I’ve made a couple of alterations, but just to help me debug that it’s actually working:

    <?php
    // ShopXSession - plugin for MODx 0.9.x ShopX
    // released to the Public Domain - use as you like.
    // sets SESSION variables from the POST or GET as submitted
    // by the various forms of shopX
    // To Do: validate the input!
    
    // System Events - OnWebPageInit
    
    $e = &$modx->Event;
    
    switch ($e->name) {
    
        case "OnWebPageInit":
            setPlaceholder('sessionDebug', 'In the ShopXSession plugin.');
            
            $_GET['sdebug'] = 'In the ShopXSession plugin.'); 
            
            // clear the cart
            if (isset($_GET['a']) && $_GET['a'] == 'clear') {
              unset($_SESSION['taxable']);
              unset($_SESSION['orders']);
              unset($_SESSION['order']);
              unset($_SESSION['tokens']);
              $modx->sendRedirect($modx->makeURL($modx->documentIdentifier));
            }
            
            
            // check for refresh or back button with token array
              $token = is_numeric($_POST['token'])?$_POST['token']:'';
            if (sizeof($_SESSION['tokens']) == 0 || !in_array($token, $_SESSION['tokens'], true)) {
                
                // get the Add to Cart values
                if(isset($_POST['add'])) {
                  $itemid = $_POST['itemid'];
                  $name = $_POST['name'];
                  $quantity = $_POST['quantity'];
                  $price = ereg_replace("[^0-9.]","",$_POST['shopItemPrice']);
                  // add any custom values for each item here
            
                  // add up quantities if repeat addition
                  if(is_array($_SESSION['orders'][$itemid])) {
                    $quantity += $_SESSION['orders'][$itemid]['quantity'];
                  }
                  // create an array of valid POST values
                  $line = array(
                  "itemid"=>$itemid,
                  "name"=>$name,
                  "quantity"=>$quantity,
                  "price"=>$price,
                  // add any custom values for each item here
                  );
                  
                  // load values into the session
                  $_SESSION['orders'][$itemid] = $line;
                  // add to non-repeating token array
                  $_SESSION['tokens'][] = $token;
              }
            
              // adjust SESSION values if checkout cart is modifed
              if(isset($_POST['scEditCart'])) {
                foreach($_POST['scItemQuantity'] as $key=>$value) {
                  if($value == 0) { 
                    unset($_SESSION['orders'][$key]);
                  } else {
                    $_SESSION['orders'][$key]['quantity'] = $value;
                  }
                }
              } 
            } // end if not repeating token
            
            // load the tax values
            if(!isset($_SESSION['taxable'])) {
              $modx->runSnippet('ShopXTax');
            }
            
            // get the Shipping values - customize here if necessary to match a shipping option
            if(isset($_POST['shipping'])){
              $shipmethod = $_POST['shipmethod'];
              $values = explode('||',$_POST[$shipmethod]);
              $_SESSION['order']['shipmethod'] = $shipmethod;
              $_SESSION['order']['calcmethod'] = $values[0];
              $_SESSION['order']['calcamount'] = $values[1];
            }
            
            // get the payment option
            if(isset($_POST['paychoice'])) {
              $paymethod = $_POST['paymethod'];
              $_SESSION['order']['paymethod'] = $paymethod;
            }
            
            // load user data from payment modules 
            // include external php file, using conditionals to determine which module to load
            // for example, to get user data for ECHO credit card processing
            //if($_SESSION['order']['paymethod'] == 'echo' && isset($_POST['some_value)) {
            //include "assets/snippets/ShopX/payments/echo.php";
            //}
            exit;
        break;
        default:
            return;
    }
    ?>
    


    So, I haven’t changed any functionality. I’ve wrapped it up like the ModX docs show for their example plugins, and I’ve tried setting a placeholder just to verify whether it’s working or not. The placeholder never displays any data, so I have to assume that it’s not working.

    I’m a decent PHP programmer so I’m not afraid of getting my hands dirty in the code, but I feel like I’m missing something simple here. Oh, and I do have that plugin set to fire on OnWebPageInit, and it is active, though I’ve locked it so that only the administrator can change it.

    Any ideas would be great since I need to get this website up and running!

    Thanks,
    Lonnie
      • 15861
      • 24 Posts
      My bad. The sessions weren’t the problem. Instead, somewhere along the way I didn’t close a parenthesis so it was spitting out and error. Since this was my first site where I really dug in and started created several snippets, I didn’t think to check the Error Log provided by ModX.

      Thought I should reply in case anyone else was running into anything similar.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Glad it was that easy to fix. ShopX got out of control real fast, which is why I don’t support it; it started as a simple proof-of-concept to create a simple catalog and shopping cart, then very quickly "just grew" beyond my level of ecommerce competence.
          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
          • 15861
          • 24 Posts
          Susan, the thing I loved about ShopX, though, was how elegant of a solution it turned out to be. It gives you just enough to get started, and then lets you customize everything about it. Prior to this, I had been working knee-deep in KiweeCommerce code, trying to help improve it, and it just turned out to need so much work. And TreasureChest didn’t handle the payment gateways I needed.

          Again, though, thanks for the code.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Awww...warm fuzzy feelings here kiss kiss
              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