Jump to content

Recommended Posts

Posted

I wan´t to change the parent page date when a child is added 1sek - 3 min left of the date/time.

$wire->addHookAfter('Pages::added', function(HookEvent $event) {
   $page = $event->arguments(0);
    
    $pageparentdate = $page->parent->auction_end_date;
    
    $parentdateminusstart = date( 'Y-m-d H:i:s', strtotime($pageparentdate . ' - ' . "3" . ' minutes'));
    $parentdateminusend = date( 'Y-m-d H:i:s', strtotime($pageparentdate));
    
    if($page->template != 'auction-bid, auction_bid_date>=$parentdatestart, auction_bid_date<=$parentdateend') return;
    
    $parent = $page->parent;
    $parentdate = $parent->auction_end_date;
    $parentdateadded = date( 'Y-m-d H:i:s', strtotime($parentdate . ' + ' . "3" . ' minutes'));
    
    $parent->setAndSave('auction_end_date', $parentdateadded);

});

Parent template= auction-page

Child template=auction-bid

End date field = auction_end_date

The code is working but i don´t know how to do with the date. 3 min > enddate.

Posted

Well the code is not working correct. I don´t now how to fix it. I want it to look for if the current time is less than 3min from the end date.

Posted

If i use this

if($page->template != "auction-bid, auction_bid_date>=$parentdateminusstart, auction_bid_date<=$parentdateminusend") return;

It will not work, but if i remove the date variables then it will work. So something is wrong with it.. hmm

if($page->template != "auction-bid") return;

Should i place the variables elsewhere?

Posted
36 minutes ago, DV-JF said:

@Flashmaster82 use TracyDebugger and check your variables where ever you want by doing

<?php bd($test); // variable or object you want to test ?>

See example screenshot:

1902868920_2022-12-0813_10_41-.png.6e60eba1c01a997f27fd2cd4d5d77a97.png

But how should i test a variabel in ready.php? I´m not so familiair with the debug tool.

Posted
7 minutes ago, Flashmaster82 said:

But how should i test a variabel in ready.php? I´m not so familiair with the debug tool.

Just do it the same way, here i did bd($page) on line 31 in ready.php

1441046750_2022-12-0813_55_10MozillaFirefox.png.986b4b72680f9118d479202ce486bfb7.png

  • Thanks 1
Posted
13 hours ago, Flashmaster82 said:
   if($page->template != "auction-bid, auction_bid_date>=$parentdateminusstart, auction_bid_date<=$parentdateminusend") return;

Sorry, I somehow overlooked this line. You can’t compare $page->template to this string because it is not a valid template name. The line will always return because the condition can never be true.

I think you probably want this:

if (!$page->matches("template=auction-bid, auction_bid_date>={$parentdateminusstart}, auction_bid_date<={$parentdateminusend}"))
    return; //only continue if the added bid’s auction_bid_date is less than 3 minutes before the parent’s auction_end_date.

See https://processwire.com/api/ref/page/matches/.

  • Thanks 1
Posted
$wire->addHookAfter('Pages::added', function(HookEvent $event) {
   $page = $event->arguments(0);
    
    $pageparentdate = $page->parent->auction_end_date;
    
    $parentdateminusstart = date( 'Y-m-d H:i:s', strtotime($pageparentdate . ' - ' . "3" . ' minutes'));
    $parentdateminusend = date( 'Y-m-d H:i:s', strtotime($pageparentdate));
 
    if (!$page->matches("template=auction-bid, auction_bid_date>={$parentdateminusstart}, auction_bid_date<={$parentdateminusend}"))
    return;
    
    $parent = $page->parent;
    $parentdate = $parent->auction_end_date;
    $parentdateadded = date( 'Y-m-d H:i:s', strtotime($parentdate . ' + ' . "3" . ' minutes'));
    
    $parent->setAndSave('auction_end_date', $parentdateadded);

});

Thank you so much @Jan Romero Current code.

 

Error! I get Undefined variable $pageparentdate? Don´t know how to define it?

Posted

Is the line that throws the warning within the function you just showed?! Clearly the variable is defined right here:

$pageparentdate = $page->parent->auction_end_date;

I’m not seeing the problem right now, sorry!

  • Sad 1
Posted

Yes, its not getting the parent page? Should i defined it before? I´m lost

PHP Warning: Undefined variable $pageparentdate

image.thumb.png.411b2a7b0eaee92e8376c638b77d4e99.png

Also i got

Undefined variable $parent
Posted

Well, for what it’s worth, it’s just a Warning, so it’s not going to crash your site, although it probably will when you upgrade your PHP to version 9 in a couple of years.

Since I’m still not seeing the problem in the code you posted, I assume it’s somewhere else. The Warning should tell you a line number to investigate, but also I would advise you to install a PHP language server such as Intelephense for VSCode. It will show you such problems while you write your code.

Posted

Maybe I have to define the auction-bid before i can define the variables with the parent? Because it's not working right now

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
  • Recently Browsing   0 members

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