Jump to content

function in a background and limit the numbers of products in a cart


Recommended Posts

Hi everybody!

 

I'm newbie in prestashop, and want develop a custom module (so, I'm not interesting in to buy a module).

I have 2 questions, and I hope someone can help me:

 

1º- Part of this module have to limit the number of products on the car. I already did it, and it works properly with a override "CartController.php".

The question is so simple: there is a better way to do that than override a controller?

I'm worry about if in future I want to install the module in other site, and there is already a "/override/controllers/CartController.php". whall will happend? The module will delete the current override or will not install it?

 

2º- The second question is a little bit more complicate, and I hope you can undertand me even my terrible english.

So, after you install the module, you have a configuration page; in this page you can set the module and set a time (for example: for 10 minutes, or for 30 minutes) and also "launch" the module. So the module in that moment will start discount on the shop (I mean a "special price rule") for the set time.

The problem is: when I click the button "Save" and launch the module, I execute the code in "getContent()" function, and it works properly, but PHP can't work with a multifunction, so you have to wait the time you set in the configuration (maybe 30 minutes, maybe 10 minutes, maybe a hour), to finally render the form of the configuration page. And it is a problem because, the page looks like a eternal loading (I mean always the configuration page, for the admin), and also because I want to put a button like "Abort the discount" or "Disable the discount" (so you can stop it in any moment) but it will appear only after finish the discount when the form is rended (so it doesn't make sense).

So, there is a way to execute a function "in a background" after save the settings and render the form without wait the end of the execution (so the end of the discount)?

 

Any suggestion will be welcome!

Thanks for your time and your help!

Link to comment
Share on other sites

Welcome to the forums!

1- The new module won't be installed in that case. The user will get a warning telling him the method the new module is supposed to override is overridden already
2- How did you set this up? Sounds like you are using sleep? Why not setting a timeout with javascript, if you do not plan to leave the page? Otherwise you might want to have the page refresh with JS every x minutes, and at every page load check against the start date you set in the module. If it's passed, then trigger the cart rule

Link to comment
Share on other sites

Thanks NemoPS for your answer!

 

2- Yes, from "getContent" I check the settings and call a function who:

- create a specific price rule

- slepp()

- and delete the specific price rule.

And it is calling recursive, so no only one time

I use javascript to reload with timer the homepage for the customers, so they can see the diferentes discounts (depents of the time and settings).

 

I didn't think about reload the configuration page because I was working with the PHP; but you are right, can be a solution:

- set the first discount

- activate the JS

- render the form

 

- with JS and a timer reload Xtimes (so recursive)

-- check the settings

-- call the function

---- create a specific price rule

---- slepp()

---- and delete the specific price rule.

-- change settings

 

I will try and tell if it works...then I will "mark solved"!

Thanks again!

Link to comment
Share on other sites

Hi NemoPS!

 

That is what I want:

I'm doing a module who has a time (for example: 600 seconds, so 10 minutes), and when is active on the 1º minute you have a 100% discount, the 2º a 90% discount, the 3º minute a 80%, ecc...

For the front office is simple, I do with javascript:

- display the timer and the discount

- reload the page every minute to display the new discount (so the new "specific_price_rule") for all the products

 

The problem is with the backoffice. In the configuration page, when you save the settings I do a loop with PHP:

- do the new specific_price_rule

- sleep one minute

- delete the specific_price_rule

 

You can imagine, with the "sleep", the backoffice doesn't reder the form during the process (for 10 minutes). And I want to do that process in background, render the form and put a button like "Abort the process".

 

I had tried your suggestion, and tried the process with a JS. The problem is the synchronization, is difficult, and I can't control the time it takes during the "reload the page", so it is never one minute (sometimes 65 seconds, sometimes 67 seconds,...). So it is no a good solution.

 

Anyway, if there is not another way, with my PHP works properly; the only problem is: I can't do that process in background, render the form and put a button like "Abort the process".

 

I was looking also how to do that in the server in background, but it is complicate because a lot of servers have locked this kind of functions calling from php (exec, popen, shell_exec,...); and you have to change the php.ini... so, if the idea is do something standard, is not a good solution.

 

I was looking also with "threads", but also that doesn't work for me.

 

If you have another idea, it will be welcome!

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