Bypass payment if cost is zero
-
14Steve14
- Senior Contributor
- Posts: 923
- Joined: Fri Oct 25, 2019 7:01 pm
- Phoenix Version: v1.0.9.1
- Has thanked: 17 times
- Been thanked: 103 times
Bypass payment if cost is zero
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.
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.
-
raiwa
- Certified Developer
- Posts: 1640
- Joined: Sat Dec 21, 2019 8:08 am
- Phoenix Version: 1.1.0.6
- : Buy Me A Beverage
- Has thanked: 70 times
- Been thanked: 152 times
Re: Bypass payment if cost is zero
Public Phoenix Change Log Cheat Set on Google Sheets
https://docs.google.com/spreadsheets/d/ ... sp=sharing
Need Help?viewtopic.php?f=10&t=27
https://docs.google.com/spreadsheets/d/ ... sp=sharing
Need Help?viewtopic.php?f=10&t=27
-
14Steve14
- Senior Contributor
- Posts: 923
- Joined: Fri Oct 25, 2019 7:01 pm
- Phoenix Version: v1.0.9.1
- Has thanked: 17 times
- Been thanked: 103 times
Re: Bypass payment if cost is zero
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.
-
raiwa
- Certified Developer
- Posts: 1640
- Joined: Sat Dec 21, 2019 8:08 am
- Phoenix Version: 1.1.0.6
- : Buy Me A Beverage
- Has thanked: 70 times
- Been thanked: 152 times
Re: Bypass payment if cost is zero
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.
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.
Public Phoenix Change Log Cheat Set on Google Sheets
https://docs.google.com/spreadsheets/d/ ... sp=sharing
Need Help?viewtopic.php?f=10&t=27
https://docs.google.com/spreadsheets/d/ ... sp=sharing
Need Help?viewtopic.php?f=10&t=27
-
14Steve14
- Senior Contributor
- Posts: 923
- Joined: Fri Oct 25, 2019 7:01 pm
- Phoenix Version: v1.0.9.1
- Has thanked: 17 times
- Been thanked: 103 times
Re: Bypass payment if cost is zero
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
In the COD module that code is added just after
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.
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;
}Code: Select all
public function update_status() {
if (!$this->enabled || !isset($GLOBALS['order'])) {
return;
}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.
-
ecartz
- Core Team
- Posts: 3084
- Joined: Tue Nov 05, 2019 6:02 pm
- Phoenix Version:
- Has thanked: 4 times
- Been thanked: 208 times
Re: Bypass payment if cost is zero
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();
}-
14Steve14
- Senior Contributor
- Posts: 923
- Joined: Fri Oct 25, 2019 7:01 pm
- Phoenix Version: v1.0.9.1
- Has thanked: 17 times
- Been thanked: 103 times
Re: Bypass payment if cost is zero
To be honest No I have not. I was looking for something similar in the current modules code.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(); }
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.
-
14Steve14
- Senior Contributor
- Posts: 923
- Joined: Fri Oct 25, 2019 7:01 pm
- Phoenix Version: v1.0.9.1
- Has thanked: 17 times
- Been thanked: 103 times
Re: Bypass payment if cost is zero
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.