Jump to content

What function enables a Customer?


Yuriy

Recommended Posts

Hi, 

what function enables a Customer?

It seems not AdminCustomersController.php processGuestToCustomer(), nor AdminCustomerPreferencesControllerCore updateOptionPsB2bEnable($value), checked overriden also. Used logging. PrestaShop™ 1.6.0.14

We need to add a php code at the point of enabling Customer.

So, when a Customer becomes enabled, an http post request should go somewhere.

In general, how to find a needed function? I'm new to php. Restart of server is undesired.

7.thumb.png.9a621ab23fb3338970ba0151efb7747d.png

 

 

Link to comment
Share on other sites

4 hours ago, cristic said:

The function responsible for enabling/disabling a model (in this case Customer class) is AdminController::processStatus().

Mmm... cristic, I can't detect the call of function classes\controller\AdminController::processStatus(). 

Tried to put inside the function: error_log, debug_backtrace, echo '<script>alert("Controller run()");</script>'; and print_r Exception trace - the ways I know, and nothing of this showed.

Meanwhile, function of the same file AdminController::__construct() was called, ran.     And CustomerCore::__construct() also seen.

Maybe processStatus() was overriden? But no, here are all the files that contain text "processStatus()":

8.png.cab479533767a9a918002d6de3d2b794.png

AdminCategoriesController was not called.

Also I tried another methods to find the function, but nothing. Miracles.

Link to comment
Share on other sites

You want to try and find a hook that is executed upon the change of customer status.

If we start with AdminController processStatus function.  We see that it uses the following code when the status of any object is changed (in this case the customer object)

if ($object->toggleStatus())

So if we open the Customer class now, and search for toggleStatus we see that it calls the parent toggleStatus

parent::toggleStatus();

The parent class is ObjectModel, so we search for toggleStatus in ObjectModel and find that it changes the active variable of the object, and then calls the update function

        // Update active status on object
        $this->active = !(int)$this->active;

        // Change status to active/inactive
        return $this->update(false);

So then we locate the update function, first in the customer class and then in the objectmodule class.  Nothing really going on in the customer class, certainly no hooks have been found yet.  However we find update in the ObjectModel and we see hooks being executed

// @hook actionObject*UpdateBefore
Hook::exec('actionObjectUpdateBefore', array('object' => $this));
Hook::exec('actionObject'.get_class($this).'UpdateBefore', array('object' => $this));

....

// @hook actionObject*UpdateAfter
Hook::exec('actionObjectUpdateAfter', array('object' => $this));
Hook::exec('actionObject'.get_class($this).'UpdateAfter', array('object' => $this));

That is how you trace code to find what you are looking for

So now that we know there are hooks that can be used to detect the change in status, now you need to create a module that will register one of these hooks, and react to it. 

Now You might find it difficult to figure out what exactly was changed, and if that is the case, then perhaps hooks will not work for you, and instead you might consider creating an override of the Customer class, and extending the toggleStatus function.

Link to comment
Share on other sites

1 hour ago, bellini13 said:

creating an override of the Customer class, and extending the toggleStatus function

 

Thank God for your reply, bellini13 

Alright, I tried. public_html//override/classes/Customer.php already existed, just added:

    public function toggleStatus()
    {
        parent::toggleStatus();

        ini_set("log_errors", 1);
        ini_set("error_log", "log.log");
        error_log("Customer toggleStatus()");
    }

And that's strange: no "toggleStatus" in log. At the same time AdminController::__construct()  and CustomerCore::__construct() appear in log.

Of course I tried to log outside parent public_html//classes/Customer.php - just for test - and no result also.

That's the problem: the functions that should be called seem to be not. But system is working well since 2015. And customers become enabled/disabled. Why the functions don't print in log? and debug_backtrace, js popup, print_r Exception don't act too.

10.thumb.png.e40a9f708cdfa4cad9135716225006df.png

Not understood where the functions that do logic dwell then.

Directories in public_html:

11.png.e31dd5e1fe26db66c3d67cf7a47012af.png

 

Link to comment
Share on other sites

Dear community,

the issue is not new. Similar topics (still not solved):

473073-need-to-implement-a-custom-build-feature-but-stuck-once-again

484762-further-action-during-status-update-activeinactive

279295-send-email-to-user-after-activation-in-bo-14x

269483-admin-action-sur-un-champs-bool-dans-liste-product-comme-statut

182152-what-hook-gets-executed-on-customer-enable - not confirmed as solution. The same I tried with no success.

A strange issue: functions that are supposed to act just don't act.

I'm thinking of upgrading Prestashop because these issues stop appearing after 2015. But update is additional headache, undesired now...

 

Edited by Yuriy (see edit history)
Link to comment
Share on other sites

add your debugging code directly to the toggleStatus function in the core Customer class and then to the ObjectModel class just to see if those core functions are being called. 

That will help rule out if this is an override issue, or just an issue in that the expected functions are not being called.

Link to comment
Share on other sites

Added already. classes/Customer.php - no effect in log:

public function toggleStatus()
	{
		ini_set("log_errors", 1);
		ini_set("error_log", "log.log");
		error_log("CustomerCore toggleStatus() begin");

		parent::toggleStatus();

		error_log("CustomerCore toggleStatus() 1");

		/* Change status to active/inactive */
		return Db::getInstance()->execute('
		UPDATE `'._DB_PREFIX_.bqSQL($this->def['table']).'`
		SET `date_upd` = NOW()
		WHERE `'.bqSQL($this->def['primary']).'` = '.(int)$this->id);

                try {
............

classes/ObjectModel.php - no effect:

    public function toggleStatus()
    {
        ini_set("log_errors", 1);
        ini_set("error_log", "log.log");
        error_log("ObjectModel toggleStatus() begin");

                echo "ObjectModel toggleStatus()"; 
                echo '<script>alert("ObjectModel toggleStatus()");</script>'; 

        // Object must have a variable called 'active'
        if (!array_key_exists('active', $this))
            throw new PrestaShopException('property "active" is missing in object '.get_class($this));

        // Update only active field
        $this->setFieldsToUpdate(array('active' => true));

        // Update active status on object
        $this->active = !(int)$this->active;

        // Change status to active/inactive
        return $this->update(false);
    }

But! classes/Customer.php constructor is shown in log. I even tried to insert http request code into the constructor - it launches successfully.

	public function __construct($id = null)
	{
		$this->id_default_group = (int)Configuration::get('PS_CUSTOMER_GROUP');
		parent::__construct($id);

		ini_set("log_errors", 1);
		ini_set("error_log", "log.log");
 		error_log("CustomerCore __construct end");
	}

Now these are all the files in the whole public_html dir, that contain "toggleStatus":

12.png.695306229e27885f387844370c9d3562.png

What to say more? It's interesting issue :)  Prestashop welcomes me hardly.

I guess that answer is somewhere near, maybe very simple...

Edited by Yuriy (see edit history)
Link to comment
Share on other sites

Good news.

I found the place in code: override/controllers/admin/AdminCustomersController.php::processChangeStatusVal(). 

It was obvious from the begining, still don't know why it didn't show any logging and debug popups, or echos. But it sends http POST fine.
Thank God for all the generous help by people here.
 
So the main caveat was impossibility of detecting the function run because logging/debug popups/echos didn't show up.
 
SOLVED
 
Edited by Yuriy (see edit history)
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...