Jump to content

Module that is called when cart is modified


Recommended Posts

Hi 

I'm a bit stuck. Actually, I don't know where to start!

I've made shipping modules in the past and they are fairly simple to iterate through the cart products and work out the weight/price of the order.

However, I now find myself with 8 shipping modules and I feel that the multiple iterations must be "costing" a lot in terms of server performance - particularly when there are a large number of products in the cart!

What I'm trying to do is create a module that is called each time the cart is updated so that the module can do the calculation once and pass this information to my shipping modules (via a table in the database).

 

Can someone show me the barebones of how to do this please?

 

Thanks

 

Link to comment
Share on other sites

@endriu107 Of course! Thank you 🙂

Thanks for pointing that out. I haven't had any need to investigate hooks so far, so this is the prompt I needed.

Once I've got a module that does *something*, I'll post it.

Cheers

  • Like 1
Link to comment
Share on other sites

I've created a basic module that installs and uninstalls then I've added the hook part like this:

	public function install()
	{
        $file = dirname(__FILE__) . '/logging.log';
        if ($this->registerHook('actionCartSave')) {
            file_put_contents($file, "hook registered\n", FILE_TEXT | LOCK_EX);
        } else {
            file_put_contents($file, "hook NOT registered\n", FILE_APPEND | LOCK_EX);
        }
	   return parent::install(); 
	}

And also a function for the hook to call:

    public function hookActionCartSave()
    {
        $file = dirname(__FILE__) . '/logging.log';
        file_put_contents($file, 'hookActionCartSave triggered'."\n",FILE_APPEND | LOCK_EX );
        if (!isset($this->context->cart)) {
            file_put_contents($file, 'no cart!'."\n",FILE_APPEND | LOCK_EX );
            return;
        }
            file_put_contents($file, 'cart detected'."\n",FILE_APPEND | LOCK_EX );
            $cart = $this->context->cart;
    }

The logging.log file is created with "hook registered" but then it never changes, which makes me think that the hook isn't calling the function. Am I missing something?

 

Thanks

 

Link to comment
Share on other sites

At first you must register hook, do it like this:

 

public function install()
{
	return parent::install()
	&& $this->registerHook('actionCartSave');
}

public function hookActionCartSave()
{
	$file = dirname(__FILE__) . '/logging.log';
	file_put_contents($file, 'hookActionCartSave triggered'."\n",FILE_APPEND | LOCK_EX );
	if (!isset($this->context->cart)) {
		file_put_contents($file, 'no cart!'."\n",FILE_APPEND | LOCK_EX );
		return;
	}
	file_put_contents($file, 'cart detected'."\n",FILE_APPEND | LOCK_EX );
	$cart = $this->context->cart;
}

 

Link to comment
Share on other sites

Thanks for that!

I think it didn't work because I was trying to add the hook before installing the module.

It works perfectly now so I can get on with the rest of my module now.

 

Thank you so much for your help.

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...