Jump to content

Unexpected strangeness with $pages->find


JoshoB
 Share

Recommended Posts

So, I'm building my own webshop using ProcessWire. Everything is coming together nicely. Users can now place an order and the details are saved to a page with template "shop-order". This template has no file, since it's not supposed to output anything on the frontend except via the API.

Now, when a user is logged in, they can go to their account page. There's an option to list their previous orders. It's a markup file (account-orders.php) that gets loaded via TemplateFile(). Here's the full code of the markup:

<?php namespace ProcessWire;

/*
==========================
Markup: account-orders.php
==========================
*/

// Get the orders
$orders = wire('pages')->find("template=shop-order, order_user_id=$customer, sort=-datetime, limit=10, include=all"); 

?><h1><?php echo $page->title; ?></h1>
<?php echo $page->body; ?>
<?php if($orders->count > 0): ?>
	<?php foreach($orders as $order): ?>
		<article class="order">
			<header>
				<h2><?php echo _('Ordered on'); ?> <?php echo $order->datetime; ?></h2>
			</header>
			<table>
				<?php foreach($order->order_products as $product): ?>
					<?php $product = $pages->get("id=" . $product); ?>
					<tr>
						<td><?php echo $product->title; ?>test</td>
					</tr>
				
				<?php endforeach; ?>
			</table>
		</article>
	<?php endforeach; ?>
<?php else: ?>
	<p><?php echo _('No orders found.'); ?></p>
<?php endif; ?>

$customer is defined in the template (= $user->id).

It all works. There's one order for this user, and when I use print_r($order) it's listed. The code also cycles through the products (since I can see the word "test" three times, top to bottom, in the table that I created in HTML). The only problem is: no output from the order is actually displayed. So instead of printing the date and the titles of products, ProcessWire displays nothing. And yet, it does load the data and cycles through it in my foreach loop. 

Now, the orders are pages that have, as rootparent, a page called "Shop" that is hidden from lists/searches and not accessible to anyone except superusers. I've changed this to make it accessible, but still no luck (have changed it back now). I also created files for the templates to see if that was the problem, but the results are the same: for some reason, ProcessWire doesn't output the data (I've removed the files again).

So, I'm a bit at a loss. But experience tells me that there's undoubtedly something that I've missed that a forum user here will note almost immediately. As always, I'd be extremely grateful for any assistance in solving this issue! :-)

Edit: BTW, "include=all" makes no difference.

Link to comment
Share on other sites

Does the customer have view permission for template: shop-order? If not, try to add check_access=0 to your selector string.

// Get the orders
$orders = wire('pages')->find("template=shop-order, order_user_id=$customer, sort=-datetime, limit=10, check_access=0, include=all");
  • Like 1
Link to comment
Share on other sites

I'm thinking you dont actually get $product using $pages->get('id=' . $product). Do you have Tracy Debugger installed? What does var_dump($product) give you, before and after you override it?

<?php foreach($order->order_products as $product): ?>
	<?php $product = $pages->get("id=" . $product); ?>
	<tr>
		<td><?php echo $product->title; ?>test</td>
	</tr>				
<?php endforeach; ?>
  • Like 2
Link to comment
Share on other sites

Oh man, what an idiot I've been!

Thanks abdus -- you're correct, it wasn't loading $product correctly since order_products is a repeater field.

That part now reads:

<?php foreach($orders as $order): ?>
	<?php $product = $pages->get("id=" . $order); ?>
	<article class="order">
		<header>
			<h2><?php echo _('Ordered on'); ?> <?php echo $order->datetime; ?></h2>
		</header>
		<table>
			<?php foreach($order->order_products as $order): ?>
				<?php $product = $pages->get("id=" . $order->order_product_id); ?>
				<tr>
					<td><?php echo $product->title; ?></td>
				</tr>
			<?php endforeach; ?>
		</table>
	</article>
<?php endforeach; ?>

So yeah... :-[

 

Link to comment
Share on other sites

This part looks like it could perhaps be optimised better:

<?php foreach($order->order_products as $order): ?>
    <?php $product = $pages->get("id=" . $order->order_product_id); ?>

Could you use a Page Reference field to hold the order products rather than a repeater that holds each product's ID?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...