Jump to content

Use lazy cron in child pages which are not reachable directly with a link


Juergen
 Share

Recommended Posts

Hello @ all,

I have a product page and several price pages as children. Here is the structure:

- Product

-- Price 1

-- Price 2

-- Price 3

-- etc

Product is the parent page and the price pages are all children.

The product page is linked and you can call it directly over an URL. The children pages (sub pages) are part of the product page, but you cannot call it directly with a link.

Goal:

I want to use lazy cron to change some values in the children pages (fe set another price at a date in the future)

I have a module where I have place all the code, but there were no changes in the children pages after lazy cron is triggered. Here is my code:

    public function init() {
        $this->addHook('LazyCron::everyMinute', $this, 'RunOfferPages'); 
    }

        //RUN CRONJOB
        //OFFER PRICES
        
        public function RunOfferPages($event) {
         $currentdatetime = time(); // Get timestamp for current date
         $pricelistpages = wire("pages")->find("template=products");
	    foreach($pricelistpages as $pp) {
              $children = $pp->children("template=productpricelistitem");
               foreach($children as $p){
                if($p->offerend){
                 if(($p->afterofferend == "1")AND(($p->offerend)<$currentdatetime)){ //set back to standard price after offerend
                  $p->pricetype = "1";//set back price type to standardprice
                  $p->offertprice = "";//delete offerprice
                  $p->offerstart = "";//delete offerdate start
                  $p->offerend = "";//delete offerdate end
                  $p->save();//save the price page
                 }
                }
              }
             $pp->save();//save the productpage
          }
        }

I guess this line of code doesnt grab the children:

$children = $pp->children("template=productpricelistitem");

I have used cron for parent pages and it works, the problem is only at children pages. It seems that they wont be affected with these lines of code.

Has someone an idea how to make it work on childpages - I cannot call them directly to trigger the cron job because they are only a part of the parent page.

Thanks in advance

Link to comment
Share on other sites

It works now, I have changed the code to:

		     foreach($pricelistpages as $pp) {
              //start foreach
              $pp->setOutputFormatting(false); // without this we can't save the page later on
              $childpages = $pp->children("template=productpricelistitem");
               foreach($childpages as $p){
                $p->setOutputFormatting(false); // without this we can't save the page later on
                $this->message("Kind");
                if($p->offerend){
                 if(($p->afterofferend == "1")AND(($p->offerend)<$currentdatetime)){ //set back to standard price after offerend
                  $p->pricetype = "1";//set back price type to standardprice
                  $p->offertprice = "";//delete offerprice standardprice
                  $p->offerstart = "";//delete offerdate start
                  $p->offerend = "";//delete offerdate end
                  $p->save();//save the price page
                 }
                }
              //end foreach
              }
          $pp->save();//save the productpage
          }
        }

As you can see I inserted $p->setOutputFormatting(false);

Link to comment
Share on other sites

Cron job stops working for now :(

For testing purposes I added Ryans example code directly in a template to show if cron job triggers.

// create your hook function
function myHook(HookEvent $e) {
    echo "30 Minutes have passed!";
}
// add a hook to your function:
wire()->addHook('LazyCron::every30Minutes', null, 'myHook');

It outputs nothing, if this code is directly in a template, so it seems that cron job doesnt work at all. The template is not cached!!!!

Link to comment
Share on other sites

Does no one have a hint why the values of the fields will not be changed after the cronjob had been triggered?

Here is the complete module code:

<?php

class AddCronJob extends WireData implements Module
{
    
    public static function getModuleInfo()
    {
        return array(
            'title' => 'Add Cron jobs for various functions.',
            'version' => 100,
            'summary' => 'Executing cron job tasks depending on time set',
            'singular' => true,
            'autoload' => true
        );
    }
    
    public function init()
    {
        $this->addHook('LazyCron::every30Seconds', $this, 'RunOfferPages');
    }
    
    public function RunOfferPages(HookEvent $e)
    {
        
        $currentdatetime = time(); // Get timestamp for current date
        $pricelistpages  = wire("pages")->find("template=products"); //get the parent page with the product template
        $pricelistpages->setOutputFormatting(false);
        
        foreach ($pricelistpages as $pp) {
            $pp->setOutputFormatting(false); // without this we can't save the page later on
            $childpages = $pp->children("template=productpricelistitem"); //get the children with the productpricelistitem
            foreach ($childpages as $p) {
                $p->setOutputFormatting(false); // without this we can't save the page later on
                $offerend = $p->getunformatted(offerend);
                if ($offerend) {
                    if (($p->afterofferend == "1") AND ($offerend < $currentdatetime)) {
                        $p->pricetype   = "1"; //set back price type to standard price
                        $p->offertprice = ""; //delete offerprice
                        $p->offerstart  = ""; //delete offerdate start
                        $p->offerend    = ""; //delete offerdate end
                        $p->save(); //save the price page
                    }
                }
            }
            $pp->save(); //save the productpage
        }
    }
}
Link to comment
Share on other sites

I changed the code a little bit. I dont use children in this case and now it works.

<?php
class AddCronJob extends WireData implements Module
{
    public static function getModuleInfo()
    {
        return array(
            'title' => 'Add Cron jobs for various functions.',
            'version' => 100,
            'summary' => 'Executing cron job tasks depending on time set',
            'singular' => true,
            'autoload' => true
        );
    }
    public function init()
    {
        $this->addHook('LazyCron::every30Seconds', $this, 'ChangeOfferPages');
    }
    public function ChangeOfferPages(HookEvent $e)
    {
        $seconds     = $e->arguments[0];
        $currentdatetime = time(); // Get timestamp for current date
        $pricelistpages  = wire("pages")->find("template=productpricelistitem"); //get the page with the price template
        foreach ($pricelistpages as $pp) {
            $pp->setOutputFormatting(false); // without this we can't save the page later on
            $offerend = $pp->getunformatted(offerend);
            if ($offerend) {
                if (($pp->afterofferend == "1") AND ($offerend < $currentdatetime)) {
                    $pp->pricetype   = "1"; //set back price type to standard price
                    $pp->offertprice = ""; //delete offerprice
                    $pp->offerstart  = ""; //delete offerdate start
                    $pp->offerend    = ""; //delete offerdate end
                    $pp->save(); //save the price page
                }
            }
        }
    }
}
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...