Jump to content
Harmen

Can't add pages to Page field [Asmselect]

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?

Share this post


Link to post
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();

 

Share this post


Link to post
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();

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Ok... just some other thoughts.

Is that page field setup correctly?

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

Are those pages you want to add hidden or unpublished?

Does your query find those pages?

 

 

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
3 minutes ago, wbmnfktr said:

I guess it's something totally obvious but right now I'm out off new ideas. Sorry.

That's okay. It's weird but I can't figure it out either

Share this post


Link to post
Share on other sites

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

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

 

Share this post


Link to post
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

Share this post


Link to post
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.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By David Karich
      The Page Hit Counter module for ProcessWire implements a simple page view counter in backend. Page views of visitors are automatically tracked on defined templates, with monitoring of multiple page views. This gives you a quick overview of how many visitors have read a news or a blog post, for example, without first having to open complex tools such as Google Analytics. This module quickly provides simple information, e.g. for editors. Or, for example, to sort certain news by most page views. For example for "Trending Topics".

       
      Works with ProCache and AdBlockers. With a lightweight tracking code of only ~320 bytes (gzipped). And no code changes necessary! In addition GDPR compliant, since no personal data or IP addresses are stored. Only session cookies are stored without information. 
      In addition, there are some options, for example filtering IP addresses (for CronJobs) and filtering bots, spiders and crawlers. You can also configure the lifetime of the session cookies. Repeated page views are not counted during this period. It is also possible to exclude certain roles from tracking. For example, logged in editors who work on a page are not counted as page views.

      Sort by hits and access page views (hit value)
      Each trackable template has an additional field called phits. For example, you want to output all news sorted by the number of page views.
      // It is assumed that the template, e.g. with the name "news", has been configured for tracking. $news = $pages->find("template=news, sort=-phits"); To output the page views of a tracked page, use:
      echo $page->phits; Example: Tracking a page hit via API and jQuery
      If you want to track a template that does not represent a full page to automatically inject a tracking script, you can define allowed API templates in the module that you can track. Below is an example of how you can track a click on news tag using jQuery. This will allow you to find out which keywords are clicked the most. For example, you can sort and display a tag cloud by the number of hits. Suppose your keywords have the template "news_tag". The template "news_tag" was also configured in the Page Hit Counter Module as a trackable API template.
      Example PHP output of keywords / tags:
      // Required: the data attribute "data-pid" with the ID of the template to be tracked. echo $pages->find("template=news_tag, sort=-phits")->each("<a href='{url}' class='news_tag' data-pid='{id}'>{title}</a>"); Example Tracking Script with jQuery:
      /** * Required: Data attribute "data-pid" with the ID of the news tag template * Required: Send the POST request to the URL "location.pathname.replace(/\/?$/, '/') + 'phcv1'" * Required: The POST parameter "pid" with the ID of the template */ $(function(){ if($('a.news_tag').length > 0) { $('a.news_tag').each(function(){ var tPID = $(this).data("pid"); if(tPID) { $(this).on("click", function(){ $.post(location.pathname.replace(/\/?$/, '/') + 'phcv1', {pid: tPID}); }); } }); } }); So simply every click on a tag is counted. Including all checks as for automatic tracking. Like Bot Filtering, Session Lifetime, etc.
      _______________________________________________________
      Background: This module is the result of a customer requirement, where the editors are overwhelmed with analytics or no tracking tools were allowed to be used. However, a way had to be found to at least count page views in a simple form for evaluations. Furthermore, by using ProCache, a way had to be found to count views of a page without clearing the cache.
      _______________________________________________________
      Pros
      Automatic Page View Tracking Lightweight tracking code, only ~320 bytes (gzipped) No code or frontend changes necessary Works with ProCache! Even if no PHP is executed on the cached page, the tracking works Works with browser AdBlockers No cache triggers (for example, ProCache) are triggered. The cache remains persistent GDPR compliant, session-based cookie only, no personal information Filtering of IPs and bots possible Exclude certain roles from tracking Ability to reset Page Views Works with all admin themes Counter database is created as write-optimized InnoDB API to track events for templates that are not viewable No dependencies on libraries, pure VanillaJS (Automatic tracking script) Works in all modern browsers Pages are sortable by hits Cons
      Only for ProcessWire version 3.0.80 or higher (Requires wireCount()) Only for PHP version 5.6.x or higher No support for Internet Explorer <= version 9 (Because of XMLHttpRequest()) No historical data, just simple summation (Because of GDPR) Planned Features / ToDos
      API access to hit values Since version 1.2.1 Possibility to sort the pages by hits (Request by @Zeka) Since version 1.2.0 Don't track logged in users with certain roles (Request by @wbmnfktr) Since version 1.1.0 Possibility to reset the counter for certain pages or templates (Request by @wbmnfktr) Since version 1.1.0 Better bot filter Since version 1.1.0 Disable session lifetime, don't store cookies to track every page view (Request by @matjazp) Since version 1.2.1 Option to hide the counter in the page tree (Request by @matjazp) Since version 1.2.1 Option to hide the counter in the page tree on certain templates Since version 1.2.1 API to track events for templates that are not viewable Since version 1.2.2 Changelog
      1.2.4
      Bug-Fix: Resetting the counters on system pages (e.g. 404) does not work (Reported by wbmnfktr) Bug-Fix: Tracking endpoint is logged as 404 if module "Jumplinks" is installed (Reported by wbmnfktr) Enhancement: Corrected few typos (Merged from Sergio #6 – THX!) 1.2.3
      Bug-Fix: Tracking script triggers 404 if pages are configured without slash (#3) Reported by @maxf5 Enhancement: Reduction of the tracking script size if it's gzipped (~320 bytes) Enhancement: Documentation improvement Enhancement: Corrected few typos 1.2.2
      New feature: API to track events for templates that are not viewable Enhancement: Documentation improvement 1.2.1
      API access to hit values Use $page->phits Bug-Fix: No tracking on welcomepage (Reported by wbmnfktr; Thx to matjazp) Bug-Fix: Tracking script path on subfolders (Reported by matjazp) Bug-Fix: Tracking on pages with status "hidden" Enhancement: Change database engine to InnoDB for phits field Enhancement: Option to disable session lifetime set session lifetime to 0, no cookies Enhancement: Better installation check Enhancement: AJAX Request asyncron Enhancement: Reduction of the tracking script size by ~20% Enhancement: Option to hide the counter in the page tree You can output the counter with the field name "phits" Enhancement: Option to hide the counter in the page tree on certain templates Enhancement: Option for activate general IP validation Enhancement: Reduction of tracking overhead up to ~30ms Enhancement: Better bot list for detection 1.2.0
      New feature: Sort pages by hits – New field phits Migrate old counter data to new field 1.1.0
      New feature: Exclude tracking of certain roles New feature: Reset Page Views Better bot filter and detection 1.0.0
      Initial release Notes
      By default, the page views are stored as INT in the database. This allows a maximum counter value of 4.2 billion views (4,294,967,295) per page. If you need more, change the type to BIGINT directly in the database. But I recommend to use Google Analytics or similar tools if you have such a large number of users.
      _______________________________________________________
      Download GitHub: ProcessWire Page Hit Counter (Version 1.2.4)
      PW Module Directory: ProcessWire Page Hit Counter (Version 1.2.4)
      Install via ProcessWire (Classname): PageHitCounter
      _______________________________________________________
      Update information
      If you have used version 1.2.1 from the DEV branch, please replace it completely with the new master version.
    • By Ivo
      Hi Folks,
      This PW site https://ancientlakesmagnesium.com.au/ currently runs Snipcart. Products are built in PW pages but the cart and checkout is managed by Snipcart. Snipcart adds new products to its inventory when first added to the cart, thus a copy of the product catalog is on the Snipcart account. My Client manages all the sales/transactions on Snipcart. The problem is Snipcart is limited, my client wishes to connect to smile.io which is much cheaper to do with a pre-built Shopify app and to have PayPal Subscriptions. Snipcart is showing to be limited in several things now that their business has grown.  
       
      My question is, has anyone had success plugging in products from PW pages into Shopify via the Shopify API? I spoke to Shopify support and they pointed me to the one post in this forum, but it doesn't give any detailed instructions and it quite old now. My other option is to build a Shopify system as a sub domain and somehow build an app that mirrors the menu structure on the PW site so that the user doesn't have to maintain 2 menus. 
       
      If anyone has any suggestions that would be most appreciated.
       
      cheers,
      Ivo
    • By VeiJari
      Hello forum! 
      I started to write my first hook for Processwire but I'm pretty confused how you should write these. My idea is to hook after publishing in init.php (called before templates)  for a certain template. 
       
      Here's the code: 

      Trying to reset the checkbox (ajasta) and then saving it so it shows unchecked in the admin page when publishing "article" page. 
      But the code isn't doing anything. Not even dumping anything
      What seems to be the problem? And have you made a similar hook for this usage or am I doing it totally wrong? 😄
      Thanks for the support in advance!
    • By EyeDentify
      Hello Gentlemen and Ladies.

      I have not posted for a while but now i need your help figuring out some things.
      The Documentation has come a long way and i love it.
      Though on the page:
      https://processwire.com/api/ref/pageimages/

      I am trying to figure out if when i want to add an image to an existing image field with multiple images alldready in it and using the method $page->images->add()
      <?PHP /* get the images object array for the Page */ $myPageImg = $page->images; /* define the image to add */ $newImg = 'http://www.somesite.com/image.jpg'; /* Thanks Autofahrn, forgot about the output formating */ $page->of(false); /* create a new Pageimage object with the given URL and add to the Pageimages array */ $myPageImg->add($newImg); /* save the page */ $page->save(); ?> I am pretty sure i missed a few steps in the code above?
      Is that string suppose to be an URL like "http://www.somesite.com/image.jpg" and the method will automaticly download the image and create an Pageimage Object and add it to Pageimages array or does it have to exist on the host first and i supply a file path to that image?

      I guess im confused about that, hope you guys could clarify that for me.

      And if it needs to be allready downloaded to my host before adding the image, what would be the best API methods for that task?
      Just point me in the right direction and i will figure it out.
      Sorry for the bad explaination but i could not figure out a better way of asking.

      Thanks in advance.
      /EyeDentify
    • By Gadgetto
      Hi,
      for my GroupMailer module I've created a custom Fieldtype + Inputfield module which provides multi-column field values. The first field column is a visible text field and there are some other columns which are not presented to user (they are rendered as hidden form fields).

      This is the database schema:
      $schema['data'] = 'text NOT NULL'; // we're using 'data' to represent our 'subject' field $schema['sendstatus'] = 'tinyint NOT NULL DEFAULT 0'; // message send status $schema['recipients'] = "int(10) unsigned NOT NULL DEFAULT 0"; // recipients counter $schema['sent'] = "int(10) unsigned NOT NULL DEFAULT 0"; // sent counter $schema['started'] = "int(10) unsigned NOT NULL DEFAULT 0"; // message sending start $schema['finished'] = "int(10) unsigned NOT NULL DEFAULT 0"; // message sending finished This are the ___wakeupValue and ___sleepValue methods:
      Now I try to extend this Fieldtype/Inputfield to provide multi language features.
      Only the first value ("data" which represents the "subject" field) should be/needs to be multi language!
      I had a look at the built in Fieldtypes (e.g FieldtypeText & FieldtypeTextLanguage) which provides multi language support but I couldn't find a similar case (multi-value field with language support). All built in Fieldtypes are single-value fields.
      I know this is a very "general" question but maybe somebody could push me in the right direction?
×
×
  • Create New...