Redirect Customer if Minimum order not reached

Open to all! Ask other shopowners for help.
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Redirect Customer if Minimum order not reached

Post by Portman »

Hi,
I am in the process of creating a Minimum Order feature on my site (we are wholesalers and don't want to deal with small orders)

I have basically got it all working, but I want to make sure that the customer cannot navigate directly to checkout_payment.php or checkout_shipping.php (by changing the url manually) and want to force a redirect on these two pages to shopping_cart.php if the Minimum order has not been reached.

is this something I can do with Hooks?
if so could someone please point me in the right direction because I have no idea how to get started on this one.

thanks heaps for any help you can offer!


Join The Code Co-op to get access to your library in the Code Co-op Forum
User avatar
Kofod95
Senior Contributor
Posts: 748
Joined: Sat Feb 06, 2021 7:38 pm
Phoenix Version: 1.0.8.20
Has thanked: 99 times
Been thanked: 179 times

Re: Redirect Customer if Minimum order not reached

Post by Kofod95 »

https://phoenixcart.org/phoenixcartwiki ... a_New_Hook

Example 2 - use injectRedirects and have one file for each page.
Alternatively, use siteWide (example 1) and check if the page is in the checkout-flow.

Another method could be (I think) to look at the pipeline (includes/system/segments), and see if anything there or in the db.hooks would be more elegant.

You have the continue button disabled, if the amount is to low, right? The same code could be used to initiate the redirect

//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: Redirect Customer if Minimum order not reached

Post by Portman »

THanks for that, That gives me a real starting point....

I'm trying to get my head around the hooks thing... can you help clarify some things though...

so to test it out I have made the following code saved in

templates/override/includes/hooks/shop/checkout_shipping/MinOrderRedirect.php

Code: Select all

<?php
class hook_shop_checkout_shipping_MinOrderRedirect {
	
	function listen_injectRedirects() {

		if( isset($_SESSION['customer_id']) && $_SESSION['wholesale_group'] > '0' ) {	
			$cart_value = $_SESSION['cart']->show_total();		
			$min_order_val = constant('MODULE_STORE_WHOLESALE_MIN_ORDER_VALUE_' . $_SESSION['wholesale_group'] );   
			$min_order = $min_order_val - $cart_value;	
		}else{
			$min_order = '-1';
		}	
	
		if ($min_order > '0') { ?>
			<script>
			alert('this works');
			</script> 
			<?php
		}				
	}	
}	
?>	
This works*** but how do I make it redirect to shopping_cart.php?

Obviously

Code: Select all

			header("Location: shopping_cart.php");
			exit();
Does not work...

The

Code: Select all

			<script>
			alert('this works');
			</script>
is just in there to test that what I am tying to do is working.

***I just learnt that whatever I did here stopps me from progressing past the shipping page... error I get is;

PHP Warning: Cannot modify header information - headers already sent by (output started at /.../templates/override/includes/hooks/shop/checkout_shipping/MinOrderRedirect.php:24) in /.../includes/system/versioned/1.0.8.5/href.php on line 195
User avatar
Kofod95
Senior Contributor
Posts: 748
Joined: Sat Feb 06, 2021 7:38 pm
Phoenix Version: 1.0.8.20
Has thanked: 99 times
Been thanked: 179 times

Re: Redirect Customer if Minimum order not reached

Post by Kofod95 »

I think it's

Code: Select all

$GLOBALS['Href']::redirect($GLOBALS['Linker']->build('shopping_cart.php')); 
You might want to add ->retain_query_except()

Sorry for short answer - I don't have my pc now

//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: Redirect Customer if Minimum order not reached

Post by Portman »

hey @Kofod95, thanks for your help...

I plugged in your suggestion into my code (insted of the alert)

and got the following error;

Code: Select all

PHP Notice:  Undefined index: Href in /.../templates/override/includes/hooks/shop/checkout_shipping/MinOrderRedirect.php on line 15
PHP Fatal error:  Uncaught Error: Class name must be a valid object or a string in /.../templates/override/includes/hooks/shop/checkout_shipping/MinOrderRedirect.php:15
Stack trace:
#0 /.../includes/system/versioned/1.0.8.1/hooks.php(150): hook_shop_checkout_shipping_MinOrderRedirect->listen_injectRedirects(Array)
#1 /.../templates/default/includes/components/template_top.php(15): hooks->cat('injectRedirects')
#2 /.../templates/default/includes/pages/checkout_shipping.php(18): require('/home/.../...')
#3 /.../checkout_shipping.php(43): require('/home/.../...')
#4 {main}
  thrown in /.../templates/override/includes/hooks/shop/checkout_shipping/MinOrderRedirect.php on line 15
I looked through the source code of the rest of the site and found a similar bit of code;

Code: Select all

Href::redirect($GLOBALS['Linker']->build('shopping_cart.php'));
so tried that and this threw up the following error;

Code: Select all

PHP Warning:  Cannot modify header information - headers already sent by (output started at /.../templates/override/includes/hooks/shop/checkout_shipping/MinOrderRedirect.php:22) in /.../includes/system/versioned/1.0.8.5/href.php on line 195
I have not tried the ->retain_query_except() yet, but I'm assuming that is not the problem ... I'm still getting my head around how the website operates so have no idea what is happening here.. have you got any suggestions on what I can try?

Here is the code im using;

Code: Select all

<?php
class hook_shop_checkout_shipping_MinOrderRedirect {
	
	function listen_injectRedirects() {

		if( isset($_SESSION['customer_id']) && $_SESSION['wholesale_group'] > '0' ) {	
			$cart_value = $_SESSION['cart']->show_total();		
			$min_order_val = constant('MODULE_STORE_WHOLESALE_MIN_ORDER_VALUE_' . $_SESSION['wholesale_group'] );   
			$min_order = $min_order_val - $cart_value;	
		}else{
			$min_order = '-1';
		}	
	
		if ($min_order > '0') { 
			$GLOBALS['Href']::redirect($GLOBALS['Linker']->build('shopping_cart.php'));
//			Href::redirect($GLOBALS['Linker']->build('shopping_cart.php'));
		}		
		
	}
	
}	
?>	
User avatar
Kofod95
Senior Contributor
Posts: 748
Joined: Sat Feb 06, 2021 7:38 pm
Phoenix Version: 1.0.8.20
Has thanked: 99 times
Been thanked: 179 times

Re: Redirect Customer if Minimum order not reached

Post by Kofod95 »

I forgot that!

The injectRedirects are called before alle classes are loaded, so the $GLOBAL variable is not set with them yet. Thus you need something like

Code: Select all

Guarantor::ensure_global('Href')::redirect( ...)
I think the $Linker is connected to the Href, but if not, you would need the Guarantor for that as well.

It is not always easy to get the head wrapped around how things work and why in core, but once you're onto something, it's usually rather easy and pleasant to work with - almost everything is possible and it's amazing how flexible the software is!

In general, when you're using the classes for inputs, links images etc, call directly, if application_top.php is loaded in the file your're writing in (that is only if you're creating entirely new pages), use $GLOBALS if you are in another file and injecting after application_top.php is loaded, use Guarantor if you are injecting code before application_top.php is loaded (that is not too often the case, but sometimes, like now)

//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: Redirect Customer if Minimum order not reached

Post by Portman »

:? I have tried just about every varient of Guarantor::ensure_global('Href')::redirect that I can think of... looking through source code and on past posts on the forum.... I just cannot get it to work...
I am still getting the same 2 errors above...

@Kofod95 I know you've been giving me a bunch of help, but is there anything else you can think of?
User avatar
Kofod95
Senior Contributor
Posts: 748
Joined: Sat Feb 06, 2021 7:38 pm
Phoenix Version: 1.0.8.20
Has thanked: 99 times
Been thanked: 179 times

Re: Redirect Customer if Minimum order not reached

Post by Kofod95 »

Just to be sure:
You have deleted the previous attempts, right?
After

Code: Select all

if($min_order > 0)
You should only have

Code: Select all

Guarantor::ensure_global('Href')::redirect($Linker->build('shopping_cart.php'));
If that's how you have it, try something like:

Code: Select all

$Href = Guarantor::ensure_global('Href');
$Linker = Guarantor::ensure_global('Linker');
$Href::redirect($Linker->build('shopping_cart.php'));
If it still fails, I will probably need to try myself to find out why, and I don't know when I'll find the time for that ;)
But hey! I'm glad you're trying things out and that makes me eager to help!

//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
User avatar
burt
Core Team
Posts: 4561
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 413 times

Re: Redirect Customer if Minimum order not reached

Post by burt »

All the pages in the checkout are in the checkout pipeline (other than, I think, checkout_success.php), so open one of these pages up and you'll find this line of code;

https://github.com/CE-PhoenixCart/Phoen ... nt.php#L15

Find the file this line refers [ https://github.com/CE-PhoenixCart/Phoen ... peline.php ] and open it up and you'll find some interesting hooks;

https://github.com/CE-PhoenixCart/Phoen ... hp#L13-L14

You can therefore make hooks that access the pipeline, as so;

Code: Select all

class hook_shop_checkout_minOrderRedirect {

  function listen_startCheckout() {
    //logic
  }
  
}
This single hook (saved at /includes/hooks/shop/checkout/minOrderRedirect.php - [side note: I prefer hooks outside of the template folder so that if you ever change templates, the hook will still be available and working]) will be active on ALL the checkout pages (other than checkout_success).

You should be able to use Href::redirect in this hook with no problems.

Interesting idea: access the messageStack prior to redirecting to show a message on the shopping_cart page, saying something like; "Spend just $x more to access our Checkout..." so that the customer is not left wondering why he is being redirected back to shopping cart.

All of the pieces of the jigsaw puzzle are scattered throughout the code of Phoenix, all you need to do is find some nice jigsaw pieces and make them into a coherent picture that suits your needs.
I am not here to build for you.
I am here to build with you. Let's help each other.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Redirect Customer if Minimum order not reached

Post by ecartz »

Kofod95 wrote: Mon Oct 09, 2023 11:01 am

Code: Select all

Guarantor::ensure_global('Href')::redirect($Linker->build('shopping_cart.php'));

Code: Select all

Href::redirect(Guarantor::ensure_global('Linker')->build('shopping_cart.php'));
Linker is a global variable and needs initialized before it is used. Href::redirect is a static method, so you can call it without anything prior.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply