flash Posted December 8, 2022 Posted December 8, 2022 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.
flash Posted December 8, 2022 Author Posted December 8, 2022 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.
zoeck Posted December 8, 2022 Posted December 8, 2022 Looks to me like $parentdate is never set? (before $parentdateminusstart)
flash Posted December 8, 2022 Author Posted December 8, 2022 The parent field (auction_end_date) is set. First i add auction-page, then users will add bid (auction-bid).
DV-JF Posted December 8, 2022 Posted December 8, 2022 @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: 1 1
flash Posted December 8, 2022 Author Posted December 8, 2022 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?
flash Posted December 8, 2022 Author Posted December 8, 2022 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: But how should i test a variabel in ready.php? I´m not so familiair with the debug tool.
DV-JF Posted December 8, 2022 Posted December 8, 2022 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 1
Jan Romero Posted December 8, 2022 Posted December 8, 2022 Now do bd($page->parent->auction_end_date) from inside the hook 1
Jan Romero Posted December 8, 2022 Posted December 8, 2022 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/. 1
flash Posted December 8, 2022 Author Posted December 8, 2022 $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?
Jan Romero Posted December 8, 2022 Posted December 8, 2022 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! 1
flash Posted December 8, 2022 Author Posted December 8, 2022 Yes, its not getting the parent page? Should i defined it before? I´m lost PHP Warning: Undefined variable $pageparentdate Also i got Undefined variable $parent
Jan Romero Posted December 9, 2022 Posted December 9, 2022 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.
flash Posted December 9, 2022 Author Posted December 9, 2022 Maybe I have to define the auction-bid before i can define the variables with the parent? Because it's not working right now
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now