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

    Currently I have this chunk of code that outputs the content of my shopping basket:

    $output='<ul>';
    $tpl='<li>[+name+] x [+quantity+]</li>';
    
    	$items = array();
    	$empty = true;
    	foreach($order->lineItems as $item){
    		$itemid = $item->id;
    		$quantity = $item->qty;
    		$realItem = $products[$itemid];
    		$name = $realItem->name;
    		$price = $realItem->price();
    		$total = $realItem->price() * $quantity;
    		$modx->setPlaceholder('quantity', $quantity);
    		$modx->setPlaceholder('items', $items);
    		$modx->setPlaceholder('price', $price);
    		$modx->setPlaceholder('name', $name);
    		$output.=$modx->mergePlaceholderContent($tpl);
    		$empty = false;
    	}
    	$modx->setPlaceholder('total', $order->subtotal());
    	$output.='<li>[+total+]</li></ul>';
    	return $output;
    


    And it works well, but what I’d really like to do is completely separate the output into two chunks, I’d like a Row chunk that contains the output listing items in the basket, and an Outer template that it all fits into, I just can’t get my head around it. (php noob).

    Could anyone point me in the right direction? Cheers,

    Matt.
      • 4172
      • 5,888 Posts
      you can try this:

      <?php
      
      //[!getMyItems? &outerTpl=`outerchunk` &rowTpl=`rowchunk`!]
      
      
       $output='no items';
      
      
      	$items = array();
      	$empty = true;
      	$rowsoutput='';
      	foreach($order->lineItems as $item){
      		$itemid = $item->id;
      		$quantity = $item->qty;
      		$realItem = $products[$itemid];
      		$name = $realItem->name;
      		$price = $realItem->price();
      		$total = $realItem->price() * $quantity;
      		$rowph=array();
      		$rowph['quantity']= $quantity;
      		$rowph['price']=$price;
      		$rowph['name']=$name;
      		$rowsoutput.=$modx->parseChunk($rowTpl,$rowph,'[+','+]');
      		$empty = false;
      	}
      	
      	if ($rowsoutput !== ''){
      		$outerph=array();
      		$outerph['rowsoutput']=$rowsoutput;
      		$outerph['total']=$order->subtotal();
      		$output=$modx->parseChunk($outerTpl,$outerph,'[+','+]');
      	}
      
      	return $output;
      ?>


      rowchunk:
      <li>[+name+] x [+quantity+]</li>


      outerchunk
      <ul>
      [+rowsoutput+]	
      <li>[+total+]</li>
      </ul>	


      with bloX you would have getdatas.php for that task:

      <?php
      
      //getdatas.php for blox
      
      
      	$items = array();
      	$empty = true;
      	$rowsoutput='';
      	foreach($order->lineItems as $item){
      		$itemid = $item->id;
      		$quantity = $item->qty;
      		$realItem = $products[$itemid];
      		$name = $realItem->name;
      		$price = $realItem->price();
      		$total = $realItem->price() * $quantity;
      		$rowph=array();
      		$rowph['quantity']= $quantity;
      		$rowph['price']=$price;
      		$rowph['name']=$name;
      		$rows[]=$rowph;
      	}
      
      $bloxdatas['total']=$order->subtotal();
      $bloxdatas['innerrows']['rowchunk']=$rows;
      
      ?>


      bloxouterTpl.html:

      [+innercounts.rowchunk:isnot=`0`:then=`
      <ul>
      [+innerrows.rowchunk+]	
      <li>[+total+]</li>
      </ul>:else=`no items`
      `+]	


      rowchunkTpl.html:
      <li>[+name+] x [+quantity+]</li>








        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 34162
        • 1 Posts
        Hi Bruno,

        Thanks mate, really appreciate the help! I’m not familiar with BloX, so I’m going to look into that. I’ve tried your first example but I couldn’t get any output at all? – not to worry, I’m pretty good at working things out when I’ve got a vague direction, I just didn’t know where to start!

        Cheers, Matt.
          • 34162
          • 1 Posts
          OK, well I’ve just done a whole bunch of testing, the outer template is being called ok and I can see its content, the total in the outer template is not being populated and the inner template is not being called at all?!

          I’m completely stumped, it really does look like it should work.. I’ve read all I can get my hands on, could it be a problem with $modx->parseChunk()

          Any ideas would be much appreciated!
            • 4172
            • 5,888 Posts
            sorry, I forgot
            ,'[+','+]'
            for $modx->parseChunk.

            Added it to the code above.
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 12553
              • 2 Posts
              Ha, I went back to doing it the ugly way, saw your post today and gave it another go, it works perfectly! Thanks so much for taking the time to help me out, really appreciate it!

              laugh Cheers,

              Matt.
                Break it, break it some more. RTFM. Break it. Fix it. Break it