Jump to content

Get child page ID


Harmen
 Share

Recommended Posts

Hello,

 

On my website I want to import categories from another website based on prestashop. For each categorie a new child page will be created, but if I want to import the categories again because there were some changes made in the categories in Prestashop, then the page has to be updated and not create a new page. So I tried the following: 

$parent = $this->session->Parent;
foreach($categories as $item) {
			//make sure end of string ends with alphanumeric
			$str_catlink = $item['link_rewrite'];
			$category_name = preg_replace('/[^a-z0-9]+\Z/i', '', $str_catlink);

			// see if we already have this item
			$page = $parent->child("name=($category_name)");

			// if we don't have this item already then create it
			if(!$page->id) {
				$page = new Page();
	//etc etc etc

But if I want to import the same categories now, there is an error: Call to a member function child() on a non-object. How can this be solved? Does anyone have an idea?

Link to comment
Share on other sites

14 minutes ago, Soma said:

You can't store Objects in session.

So, if I declare $parent in another way it will work? I hope so, but without this check if the page already exists, it works perfectly. So I think it is not the session that makes the problem...

 

I wrote this in a module file by the way, maybe that makes sense?

Edited by Harmen
Link to comment
Share on other sites

Why not just do 

foreach($categories as $item) {
    //get the page if it exists
    $p = $pages->get("parent=/parent-page/,title=the_title_of the_page_to_be_created_updated");
    if($p->id){
        // page exists
        $p->title = "new title";
        $p->save();
    } else {
        // page does not exist
        $np = new Page();
        $np->template = "template-of-page-to-be-created";
        $np->parent = $pages->get("/parent-page/");
        $np->title = "the title";
        $np->save();
}

 

  • Like 1
Link to comment
Share on other sites

 

fbg13, I think your code works but I have to declare $pages. So $pages = ????

My code now looks like this:

protected function importPage() {
		
		$categories = $this->get_category();
		//The CSV means nothing, I modified the importCSVfiles module...
		$parent = $this->session->csvParent;
		
		$Template = $this->session->csvTemplate;

		foreach($categories as $item) {
			$str_catlink = $item['link_rewrite'];
			$category_name = preg_replace('/[^a-z0-9]+\Z/i', '', $str_catlink);

			//get the page if it exists
			$p = $pages->get("parent=$parent,title=$category_name");
			if ($p->id) {
				// page exists
				$p->title = "$category_name";

				$p->save();
			} else {
				// page does not exist
				$np = new Page();
				$np->template = $Template;
				$np->parent = $pages->get("$parent");
				$np->title = "$category_name";
				$np->save();
			}

		}

So, if I can declare the $pages I can try it. Suggestions are welcome...

Link to comment
Share on other sites

Replace $pages with  $this->pages or wire("pages")

$p = $this->pages->get("parent=/parent-page/,title=the_title_of the_page_to_be_created_updated");

$p = wire("pages")->get("parent=/parent-page/,title=the_title_of the_page_to_be_created_updated");

 

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

×
×
  • Create New...