Jump to content

Can't add pages to Page field [Asmselect]


Harmen
 Share

Recommended Posts

I want to add a few pages to an AsmSelect Page field inside a repeater using the following code:

$trialsPage = wire("pages")->get(28422); // Get the page
$trialsPage->of(false);
$newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater
foreach ($selectedProducts as $selectedProduct){
	$productPage = $pages->get("template=product, reference=$selectedProduct");
	$newTrial->trial_selected_products->add($productPage);
}
$newTrial->save();
$trialsPage->save();

However, when I check the page where the field is located it doesn't have the new values as expected. The selected pages exist, the field is in the right location, made sure that the output formatting is turned off: $page->of(false); But it still doesn't work with a variable. No matter what I try, it doesn't work.

It only works when I replace $selectedProduct with a hardcoded string. Am I doing something wrong here?

Link to comment
Share on other sites

Does it work with $selectedProduct in curly brackets like this {$selectedProduct}?

$trialsPage = wire("pages")->get(28422); // Get the page
$trialsPage->of(false);
$newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater
foreach ($selectedProducts as $selectedProduct){
	$productPage = $pages->get("template=product, reference={$selectedProduct}");
	$newTrial->trial_selected_products->add($productPage);
}
$newTrial->save();
$trialsPage->save();

 

Link to comment
Share on other sites

And another thing with repeaters via API. At least I save after each new repeater - as seen below.

$trialsPage = wire("pages")->get(28422); // Get the page
$trialsPage->of(false);
$newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater
foreach ($selectedProducts as $selectedProduct){
	$productPage = $pages->get("template=product, reference={$selectedProduct}");
	$newTrial->trial_selected_products->add($productPage);
	$newTrial->save();
}
$trialsPage->save();

 

Link to comment
Share on other sites

14 minutes ago, wbmnfktr said:

Does it work with $selectedProduct in curly brackets like this {$selectedProduct}?

Tried that but doesn't work.

 

9 minutes ago, wbmnfktr said:

At least I save after each new repeater - as seen below.

Doesn't fix the problem either.

 

I have no problems editing the other fields in the repeater, it's just the AsmSelect Page field

Link to comment
Share on other sites

12 minutes ago, wbmnfktr said:

Is that page field setup correctly?

Yes, checked everything a couple times. Don't know why I can't set the values

12 minutes ago, wbmnfktr said:

Are the pages you want to add matching the parent/template settings within the page field?

Yes

12 minutes ago, wbmnfktr said:

Are those pages you want to add hidden or unpublished?

No, they are published and not hidden

12 minutes ago, wbmnfktr said:

Does your query find those pages?

Yes

Can it be a problem that the page with this field has a template without a file?

Link to comment
Share on other sites

What sort of field is "reference"? What sort of data is $selectedProduct?

14 hours ago, Harmen said:

It only works when I replace $selectedProduct with a hardcoded string.

If that is so then it indicates that the data held in $selectedProduct is not the same as the data in your hardcoded string.

Tracy Debugger is your friend here...

$trialsPage = wire("pages")->get(28422); // Get the page
$trialsPage->of(false);
$newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater
foreach ($selectedProducts as $selectedProduct){
	// See what is in $selectedProduct
	bd($selectedProduct, 'selectedProduct');
	$productPage = $pages->get("template=product, reference=$selectedProduct");
	// Check that $productPage is not a NullPage (i.e. no matching page found)
	// and that is the right kind of page (template, parent, etc) to add to trial_selected_products
	bd($productPage, 'productPage');
	$newTrial->trial_selected_products->add($productPage);
}
$newTrial->save();
$trialsPage->save();

 

  • Like 1
Link to comment
Share on other sites

18 hours ago, Autofahrn said:

Did you try (shouldn't be necessary, though)


$productPage = $pages->get("template=product, reference={$selectedProduct->id}");

 

Doesn't work, $selectedProduct isn't a Page object but a string.

--

7 hours ago, Robin S said:

What sort of field is "reference"? What sort of data is $selectedProduct?

The reference field is just a Text field. $selectedProduct is a string. The reference field holds the reference of the product which is unique for each product. 

Used TracyDebugger, and this are the results:

Quote

bd($selectedProduct, 'selectedProduct');

This returns a string, the exact same string that the product has stored in the reference field.

Quote

bd($productPage, 'productPage');

This returns a Page Object, containing the actual page that I want to add to the AsmSelect Page field.

Conclusion: $productPage contains an actual page that exists, but for some reason I cannot add this page to the AsmSelect Page Field.

Did some more digging and found the following:

There are 7 categories: each containing a different kind of products. I have no problem with adding the products of the first 4 categories, but for the remaining 3 categories it doesn't work, a hardcode string for these pages doesn't help either, even though I can find the pages manually in the selector.

 

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

×
×
  • Create New...