Jump to content

Need Assistance Please - Cronjob to disable categories when empty


kdmonk1

Recommended Posts

I have a cronjob that calls a php file at the root folder. The php file grabs the top category and  if any categories are empty (all products are disabled) it disables the category. (I won't to hide categories that are empty, that is my goal) I need assistance from the community on trying to figure this out. Listed below is the error (Uncaught Error: Call to undefined method Context::getTopCategory())

PFA: php file

Please assist and thanks.

Prestashop 1.7.8.0 / CPanel / php 7.3 (ea-php73)

Cronjob: 0011*/usr/bin/curl https://mysite.com/update_categories.php   (Set at 1 year for now until I can figure this out)

Error: 

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   259    0   259    0     0    471      0 --:--:-- --:--:-- --:--:--   471


<br />
<b>Fatal error</b>:  Uncaught Error: Call to undefined method Context::getTopCategory() in /home/local/public_html/update_categories.php:7
Stack trace:
#0 {main}
  thrown in <b>/home/local/public_html/update_categories.php</b> on line <b>7</b><br />

update_categories.php

Edited by kdmonk1 (see edit history)
  • Like 1
Link to comment
Share on other sites

Thanks @WebSoft, that helped me het past that line now I am running into the following issue. When going through the foreach is where the issue occurs. (getProducts() is passing in like 11 arguments not sure how to handle that)Thoughts?

<br />
<b>Fatal error</b>:  Uncaught Error: Call to a member function getProducts() on int in /home/local/public_html/update_categories.php:9
Stack trace:
#0 {main}
  thrown in <b>/home/local/public_html/update_categories.php</b> on line <b>9</b><br />

  • Thanks 1
  • Confused 1
Link to comment
Share on other sites

<?php
require dirname(__FILE__).'/config/config.inc.php';

$id_lang = Context::getContext()->language->id;
$categories = Category::getTopCategory($id_lang);
foreach($categories as $cate) {
/*public function getProducts(
        $idLang,
        $p,
        $n,
        $orderyBy = null,
        $orderWay = null,
        $getTotal = false,
        $active = true,
        $random = false,
        $randomNumberProducts = 1,
        $checkAccess = true,
        Context $context = null
    ) */
	$category = new Category((int) $cate['id_category']);

	if (!$category->getProducts($id_lang, 0, 0, null, null, true)) {
		$category->active = 0;
		$category->update();
	}
}

 

Link to comment
Share on other sites

Thanks @WebSoft, I was able to get pass with no errors. Looks like the categories are still there so I need to look into that. Have you seen anyone trying to do something like this before? My head is spinning trying to get this to work. If possible can you test on your end to validate that it works or not?

 

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

  • Like 1
Link to comment
Share on other sites

Hi @WebSoft, looks like the website was cached but it worked. The categories that were empty are no longer in the tree. They have been disabled. Can you validate my code again in regards to clearing the cache (clearSmartyCache)? Maybe I need to turn caching on or something. Thoughts?

 

update_categories.php

Edited by kdmonk1
Updated file (see edit history)
Link to comment
Share on other sites

Thank you @WebSoft, I tried the script this time and it failed. Not because of the code you provided. 

<br />
<b>Fatal error</b>:  Uncaught PrestaShopException: a category cannot be its own parent in /home/local/public_html/classes/Category.php:237
Stack trace:
#0 /home/local/public_html/update_categories.php(26): CategoryCore-&gt;update()
#1 {main}
  thrown in <b>/home/local/public_html/classes/Category.php</b> on line <b>237</b><br />

___

How can I avoid the parent id matching the id?

$category = new Category((int) $cate['id_category']);

	if (!$category->getProducts($id_lang, 0, 0, null, null, true)) {
		$category->active = 0;
		$category->update();
	}

The call to update is failing on first if statement.

 public function update($nullValues = false)
    {
        if ($this->id_parent == $this->id) {
            throw new PrestaShopException('a category cannot be its own parent');
        }

        if ($this->is_root_category && $this->id_parent != (int) Configuration::get('PS_ROOT_CATEGORY')) {
            $this->is_root_category = 0;
        }

        // Update group selection
        $this->updateGroup($this->groupBox);

        if ($this->level_depth != $this->calcLevelDepth()) {
            $this->level_depth = $this->calcLevelDepth();
            $changed = true;
        }

        // If the parent category was changed, we don't want to have 2 categories with the same position
        if (!isset($changed)) {
            $changed = $this->getDuplicatePosition();
        }

        if ($changed) {
            if (Tools::isSubmit('checkBoxShopAsso_category')) {
                foreach (Tools::getValue('checkBoxShopAsso_category') as $idAssoObject => $idShop) {
                    $this->addPosition($this->position, (int) $idShop);
                }
            } else {
                foreach (Shop::getShops(true) as $shop) {
                    $this->addPosition($this->position, $shop['id_shop']);
                }
            }
        }

        $ret = parent::update($nullValues);
        if ($changed && !$this->doNotRegenerateNTree) {
            $this->cleanPositions((int) $this->id_parent);
            Category::regenerateEntireNtree();
            $this->recalculateLevelDepth($this->id);
        }
        Hook::exec('actionCategoryUpdate', ['category' => $this]);

        return $ret;
    }

 

 

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
×
×
  • Create New...