We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10495
    • 18 Posts
    Hi,

    I wonder if someone with a lot more Modx-fu could have a look at this and see if they can get it to work

    it’s a very basic shopping cart script based on wfcart, it’s almost working, but it looks like the sessions aren’t being kept

    just paste the code into a snippet and call it from a document, you’ll see what I mean

    the immediate item is put in the basket (it puts the lotion in the basket... smiley but won’t recognise another... it’s got to be a session thing...

    thanks for any help, if I can get basic functionality, I can work it up into a very simple repository item (and yes I know the codes messy, I’ll clean it up, promise)

    oh and it works just fine outside of modx, in a plain old PHP file, this is an idea of what I’m trying to do www.bnionline.co.uk/test.php (the array outputs at the top and the bottom are just to show what goes wrong when brought into modx)

    and there might be the( action="") that needs setting to your doc alias, I’ve commented it with some *****’s

    cheers for any help

    Gary

    <?php
    session_start();
    $cart =& $_SESSION['wfcart']; // point $cart to session cart.
    if(!is_object($cart)) $cart = new wfCart(); // if $cart ( $_SESSION['cart'] ) isn't an object, make a new cart
    echo'<br><br><br>'; print_r($_SESSION['wfcart']);
    $products = array();
    $products[1] = array("id"=>1,"name"=>"Grey Name Badge","price"=>1.00);
    $products[2] = array("id"=>2,"name"=>"Blue Name Badge","price"=>1.00);
    $products[3] = array("id"=>3,"name"=>"Black Name Badge","price"=>1.00);
    $products[31] = array("id"=>31,"name"=>"Drinks Coasters (box of 4)","price"=>9);
    $products[32] = array("id"=>32,"name"=>"Mouse mats (5)","price"=>15);
    $products[33] = array("id"=>33,"name"=>"Silver card wallets (10)","price"=>27.50);
    $products[34] = array("id"=>34,"name"=>"Set of 3 books","price"=>33.00);
    
    
    //start of code
    
        // check to see if any items are being added
    if($_POST['add']) {
    	$product = $products[$_POST['id']];
    	$cart->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']);
    }
    if($_POST['remove']) {
    	$rid = intval($_POST['id']);
    	$cart->del_item($rid);
    
    }
    if($_POST['email']) {
    	// make email
    	
    	if($cart->itemcount > 0) {
    	$message = "order form";
    	$subject = $message;
    	$emailfrom = "[email protected]";
    	$emailto = "[email protected]";
    	
    	$message .= "\n\r\n\r";
    	foreach($cart->get_contents() as $item) {
    		
    		$message .= $item['qty']." @ ";
    		$message .= number_format($item['price'],2)." - ";
    		$message .= $item['info']." = ";
    		$message .= "£".number_format($item['subtotal'],2)."\n\r";
    	}
    	$message .= "---------------------\n\r";
    	$message .= "Total: £".number_format($cart->total,2);
    } else {
    	$message .= "No items in cart";
    }
    	mail($emailto,$subject,$message,
    			"From: ".$emailfrom."\r\n"."Reply-To: ".$emailfrom);	
    			
    	mail($emailto,"Copy of ".$subject,$message,
    			"From: ".$emailfrom."\r\n"."Reply-To: ".$emailfrom);	
    			
    			
    	$cart->empty_cart();
    		echo"<h3>Thankyou your order has been submitted</h3>";
    
    
    }
    
    // spit some forms
    // You can have many different types of forms, such as many quantity boxes
    // and an "add to cart" button at the bottom which adds all items
    // but for the purposes of this demo we will handle one item at a time. 
    echo "<table width='100%'>";
    foreach($products as $p) {
    	$pricetext = "";
    	if($p['price']<>0) $pricetext = "£".number_format($p['price'],2);
    	echo "<tr><td><form method='post' action=''>"; // ****note this might need changing to your document alias to get it to work
    	echo "<input type='hidden' name='id' value='".$p['id']."'/>";
    	echo "".$p['name'].'</td><td>'.$pricetext."</td><td> ";
    	echo "<input type='text' name='qty' size='5' value='1'></td><td><input type='submit' value='Add to order' name='add'>";
    	echo "</form></td></tr>";
    }
    echo "</table>";
    
    
    echo "<h3>Items in cart</h3>";
    
    if($cart->itemcount > 0) {
    echo "<table>";
    	foreach($cart->get_contents() as $item) {
    		echo "<tr><td>";
    		echo $item['qty']." @ ";
    		echo " £".number_format($item['price'],2)."</td><td>";
    		echo $item['info']."</td><td>";
    		echo "Subtotal :£".number_format($item['subtotal'],2)."</td><td>";
    		echo "<form method=post><input type='hidden' name='id' value='".$item['id']."'/><input type='submit' name='remove' value='Remove'/></form></td></tr>";
    	}
    	
    	echo "<tr><td> </td><td> </td><td>";
    	echo "<b>Total: £".number_format($cart->total,2);
    	echo "</b></td><td> </td></table>";
    	echo "<form method=post><input type='submit' name='email' value='Submit Order by Email'/></form></td></tr>";
    } else {
    	echo "No items in cart";
    }
    echo'<br><br><br>'; print_r($_SESSION['wfcart']);
    class wfCart {
    	var $total = 0;
    	var $itemcount = 0;
    	var $items = array();
            var $itemprices = array();
    	var $itemqtys = array();
            var $iteminfo = array();
    
    	function cart() {} // constructor function
    
    	function get_contents()
    	{ // gets cart contents
    		$items = array();
    		foreach($this->items as $tmp_item)
    		{
    		        $item = FALSE;
    
    			$item['id'] = $tmp_item;
                            $item['qty'] = $this->itemqtys[$tmp_item];
    			$item['price'] = $this->itemprices[$tmp_item];
    			$item['info'] = $this->iteminfo[$tmp_item];
    			$item['subtotal'] = $item['qty'] * $item['price'];
                            $items[] = $item;
    		}
    		return $items;
    	} // end of get_contents
    
    
    	function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
    	{ // adds an item to cart
                    if(!$price)
    		{
    		        $price = 0.00;
    		}
    
                    if(!$info)
    		{
                            $info = wf_get_info($itemid);
    		}
    
    		if($this->itemqtys[$itemid] > 0)
                    { // the item is already in the cart..
    		  // so we'll just increase the quantity
    			$this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
    			$this->_update_total();
    		} else {
    			$this->items[]=$itemid;
    			$this->itemqtys[$itemid] = $qty;
    			$this->itemprices[$itemid] = $price;
    			$this->iteminfo[$itemid] = $info;
    		}
    		$this->_update_total();
    	} // end of add_item
    
    
    	function edit_item($itemid,$qty)
    	{ // changes an items quantity
    
    		if($qty < 1) {
    			$this->del_item($itemid);
    		} else {
    			$this->itemqtys[$itemid] = $qty;
    			// uncomment this line if using 
    			// the wf_get_price function
    			// $this->itemprices[$itemid] = wf_get_price($itemid,$qty);
    		}
    		$this->_update_total();
    	} // end of edit_item
    
    
    	function del_item($itemid)
    	{ // removes an item from cart
    		$ti = array();
    		$this->itemqtys[$itemid] = 0;
    		foreach($this->items as $item)
    		{
    			if($item != $itemid)
    			{
    				$ti[] = $item;
    			}
    		}
    		$this->items = $ti;
    		$this->_update_total();
    	} //end of del_item
    
    
            function empty_cart()
    	{ // empties / resets the cart
                    $this->total = 0;
    	        $this->itemcount = 0;
    	        $this->items = array();
                    $this->itemprices = array();
    	        $this->itemqtys = array();
                    $this->iteminfo = array();
    	} // end of empty cart
    
    
    	function _update_total()
    	{ // internal function to update the total in the cart
    	        $this->itemcount = 0;
    		$this->total = 0;
                    if(sizeof($this->items > 0))
    		{
                            foreach($this->items as $item) {
                                    $this->total = $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]);
    				$this->itemcount++;
    			}
    		}
    	} // end of update_total
    
    }
    ?>


    admin: change to code tags (from quote tags)
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      MODx sets its own sessions, which can cause problems if you’re setting your own. It can also cause problems with switching from http to https, since these are treated as different domains. I addressed these in my ShopX attempt at implementing a simple cart script. The cart itself works great; the checkout (shipping, taxes, etc) proved to be a bit too much for me to deal with, so it never really went anywhere. http://modxcms.com/forums/index.php/topic,1575.0.html
        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
        • 10495
        • 18 Posts
        thanks for your input Susan, I don’t want any shipping or merchant facilities so shopX might be just up my street smiley

        cheers

        Gary
          • 10495
          • 18 Posts
          OK I’ve got this working...

          basically the php page was running fine on it’s own so I just used the iframe widget to show it in the site...

          you can see it in action here http://www.bnionline.co.uk/stock-ordering.html (please don’t send the order in smiley

          wish I’d done that in the first place instead of installing kiweecart (then uninstalling it cause I couldn’t get it to work), hacking treasure chest to not use paypal and then running into a whole dustbin full of worms with javascript conflicts, downloading and installing shopX, then not being able to find any sort of documentation to help to get it running

          I’m really hoping Revo has a decent shop system developed for it, that would kick ass smiley

          maybe I’ll try and break the wfcart down to use eform etc, if anyone needs this (ie a very simple, 1 page cart that emails the transaction), give me a shout

          Cheers

          Gary
            • 27866
            • 1 Posts
            Hello,

            Can you advise how did you make it working?

            I have copied your script for my homework, but it does not working.

            Would you mind teaching me?

            Thank you.

            The cart is working, I am able to add, remove but these words are show up with the cart.

            wfCart Object ( [total] => 0 [itemcount] => 0 [items] => Array ( ) [itemprices] => Array ( [1] => 25 ) [itemqtys] => Array ( [1] => 0 ) [iteminfo] => Array ( [1] => Shirt ) )
            Notice: Undefined index: add in C:\wamp\www\HW4\code.php on line 17

            Notice: Undefined index: remove in C:\wamp\www\HW4\code.php on line 21

            Notice: Undefined index: email in C:\wamp\www\HW4\code.php on line 26

            Thank you so much.



              • 10495
              • 18 Posts
              Hi Vanessa

              OK first off, lets get rid of the echo statements (they’re just for debugging) so if you comment out the

              echo'<br><br><br>'; print_r($_SESSION['wfcart']);


              at line 5 and 100

              now I’m not too sure what the undefined index refers to, could be some setup in your WAMP, I’m probably not checking that some variable is there before using it, but we can always just turn off error reporting in the script with this at the top

              error_reporting(0);


              don’t forget to add your email in lines 35 and 36 and you might have to hard code the URL in line 71 (the action parameter for the form)

              once you’ve got the script working well as an external document, you can include it into modx with the iframe widget, it works great for me smiley

              hope that helps

              Gary

                • 26931
                • 2,314 Posts
                Hi garybaldy ,

                how do you manage the products? are they hardcoded in an external file, or can you edit the products from the modx-backend?

                jan
                  • 10495
                  • 18 Posts
                  Hi Jan

                  at the minute they’re hardcoded into the external file, but it wouldn’t be too hard to get a snippet generating the code via the api I wouldn’t think smiley

                  Gary
                    • 26931
                    • 2,314 Posts
                    okay, thank you smiley
                      • 27330
                      • 884 Posts
                      btw, i noticed the buttons column get smaller with each add to order click. least of your concerns I guess but just in case you missed it.