Jump to content

Help with hook


Flashmaster82
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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.

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...