Page 1 of 1
Bypass payment if cost is zero
Posted: Fri Jun 21, 2024 3:06 pm
by 14Steve14
To the collective brains of this forum.
What is the best way to bypass the payment page if the total for the order is zero?
We offer a few free downloads on our website and have successfully bypassed the shipping for all virtual products, but need some code, or a hook that will bypass the payment page if the order is zero.
Re: Bypass payment if cost is zero
Posted: Fri Jun 21, 2024 4:33 pm
by raiwa
Re: Bypass payment if cost is zero
Posted: Fri Jun 21, 2024 5:02 pm
by 14Steve14
Trouble is that the code that I was referring to in that thread was a hook. That hook was based on old code and meant that all the payment modules needed code changes to work.
I was hoping that there was a simpler way to notice that the cart contained virtual products at a zero cost, and then only used one payment module, or bypassed the payments altogether.
Re: Bypass payment if cost is zero
Posted: Fri Jun 21, 2024 5:17 pm
by raiwa
I see 2 approaches:
If you wish to use the checkout flow for these zero cost products:
Make a copy of a simple payment module like cod and modify it to show only if cart content has zero cost (just to hide it for normal orders).
Make a checkout_payment page hook which does the following if the order value is zero:
- sets the payment to the modified payment module
- redirects to checkout confirmation
The other way which I'm using in my shop for free addons:
Add a download button to free virtual products (in product info and optional on the product listings), which directly downloads the file. I'm using the SKU field for to hold the filename, but if you are using it already, add a new column to the product table. The file could be located in the "pub" directory.
Or if you wish some more security, use a copy of the download.php file with modifications to download the file without the order and download limit parts.
Re: Bypass payment if cost is zero
Posted: Sat Jul 06, 2024 3:36 pm
by 14Steve14
Thanks rainer for that explanation.
As a non coder it means nearly nothing to me though.
I have a working payment module for free products that only shows when the order value is zero. That bit works fine.
The problem being is that the other payment modules Stripe and Paypal both also show, so its switching those off in the payment is zero. Is it possible to use the same code that is in the free payment module, but in reverse to switch off those two payment methods. If so where in the module code does that code need to go.
This is the code I am trying to add
Code: Select all
// disable the module if the order only contains virtual products
if ($GLOBALS['order']->info['total'] == 0) {
$this->enabled = false;
return;
}
In the COD module that code is added just after
Code: Select all
public function update_status() {
if (!$this->enabled || !isset($GLOBALS['order'])) {
return;
}
but that code does not appear in either the Stripe of Paypal modules. I have tried adding the code in several different places in each module and although it hides the module, it also hides the module when it should show when the order value is over Zero so I am assuming that it is in the incorrect place.
We use the free downloads as a way to encourage users to sign up to the website so a button on the product page is not ideal, but as a last resort could be something that we use.
Can anyone help with the placement of this code or a way to create a hook to switch off the other payment modules and only allowing the free payment module to be selected on free products with zero weight.
Re: Bypass payment if cost is zero
Posted: Sat Jul 06, 2024 4:32 pm
by ecartz
Have you tried copying that function from COD to your module?
Code: Select all
public function update_status() {
if (!$this->enabled || !isset($GLOBALS['order'])) {
return;
}
// disable the module if the order only contains virtual products
if ($GLOBALS['order']->info['total'] == 0) {
$this->enabled = false;
return;
}
parent::update_status();
}
Re: Bypass payment if cost is zero
Posted: Sat Jul 06, 2024 4:37 pm
by 14Steve14
ecartz wrote: ↑Sat Jul 06, 2024 4:32 pm
Have you tried copying that function from COD to your module?
Code: Select all
public function update_status() {
if (!$this->enabled || !isset($GLOBALS['order'])) {
return;
}
// disable the module if the order only contains virtual products
if ($GLOBALS['order']->info['total'] == 0) {
$this->enabled = false;
return;
}
parent::update_status();
}
To be honest No I have not. I was looking for something similar in the current modules code.
A bit silly I know but if I copy the complete function into one of the other modules does it matter where in the module it is placed. I take it it should be near the top.
Re: Bypass payment if cost is zero
Posted: Sat Jul 06, 2024 5:24 pm
by ecartz
Method order does not matter. Just place it inside the class and outside any other methods.
Re: Bypass payment if cost is zero
Posted: Sun Jul 07, 2024 4:18 pm
by 14Steve14
ecartz wrote: ↑Sat Jul 06, 2024 5:24 pm
Method order does not matter. Just place it inside the class and outside any other methods.
Many thanks. Place the complete function right at the top of the Stripe and PayPal modules and all seems to be working as I would like.