latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Programmatically Access Coupon Codes

Home Forums Community Forum Programmatically Access Coupon Codes

This topic contains 15 replies, has 3 voices. Last updated by  Jeffry Evans 4 years, 5 months ago.

Topic Author Topic
Posted: Monday Jul 9th, 2012 at 4:50 pm #18703

I would like to use php to grab the coupon code entered by a customer.

How do I do that?

Thanks,

Jeffry

List Of Topic Replies

Viewing 15 replies - 1 through 15 (of 15 total)
Author Replies
Author Replies
Posted: Monday Jul 9th, 2012 at 5:01 pm #18704

Actually, if possible, I want the net (after coupon) price of the cart to be accessible by php. All of our coupon codes only discount the very first payment, nothing after that, so I want the amount the customer pays upon their initial payment.

Please let me know.

Posted: Tuesday Jul 10th, 2012 at 1:58 am #18771
Raam Dev
Username: Raam
Staff Member

Hi Jeffry,

You could use Payment Notifications (see Dashboard -› s2Member® -› API / Notifications -› Payment Notifications) to retrieve the total payment amount for each transaction. Will that work?

Posted: Tuesday Jul 10th, 2012 at 12:26 pm #18818

No, I need this value before the payment is processed, because I am using it to generate the correct PayPal button url. I am using an s2Member Authorize.net pro form, coupled with a standard PayPal button on this checkout screen.

Posted: Wednesday Jul 11th, 2012 at 2:34 am #18877
Raam Dev
Username: Raam
Staff Member

Hi Jeffry,

I’m going to consult with Jason and see if he has any ideas. Thank you for your patience.

Posted: Friday Jul 13th, 2012 at 4:08 pm #19190

Any word on this?

Thanks,

Jeffry

Posted: Saturday Jul 14th, 2012 at 6:15 am #19232
Raam Dev
Username: Raam
Staff Member

Hi Jeffry,

Sorry, no response yet. I’ll let you know as soon as I hear something.

Posted: Tuesday Jul 17th, 2012 at 12:41 pm #19533

I really need to get a solution to this asap. Is there any way to speed up the response time?

Thanks,

Jeffry

Posted: Wednesday Jul 18th, 2012 at 6:01 am #19602

Hi Jeffry.

Looked into this and found [hilite mono]authnet_apply_coupon[/hilite]. s2member-pro/includes/classes/gateways/authnet/authnet-utilities.inc.php

* Checks to see if a Coupon Code was supplied, and if so; what does it provide?
*
* @package s2Member\AuthNet
* @since 1.5
*
* @param array $attr An array of Pro Form Attributes.
* @param str $coupon_code Optional. A possible Coupon Code supplied by the Customer.
* @param str $return Optional. Return type. One of `response|attr`. Defaults to `attr`.
* @param array $process Optional. An array of additional processing routines to run here.
* 	One or more of these values: `affiliates-1px-response|affiliates-silent-post|notifications`.
* @return array|str Original array, with prices and description modified when/if a Coupon Code is accepted.
* 	Or, if ``$return === "response"``, return a string response, indicating status.

You can see the function used in [hilite mono]authnet_checkout[/hilite]. s2member-pro/includes/classes/gateways/authnet/authnet-checkout-in.inc.php

[hilite pre_code]
$cp_attr = c_ws_plugin__s2member_pro_authnet_utilities::authnet_apply_coupon($post_vars["attr"], $post_vars["coupon"], "attr", array("affiliates-silent-post"));
[/hilite]

You can do a test and [hilite code]print_r($cp_attr)[/hilite] after that to see what you get. $post_vars are the [hilite mono]$_POST[/hilite] vars after some sanitization/formatting done in the previous lines. I recommend that you review the full code for the function to see what’s happening there. http://www.s2member.com/codex/stable/source/s2member-pro/includes/classes/gateways/authnet/authnet-checkout-in.inc.php/#src_doc_line_59

I hope that helps. :)

Posted: Thursday Jul 19th, 2012 at 6:49 pm #19788

I’m not sure where I am going wrong here, but i tried to use that code in my s2hacks.php file to grab the data, and I am just not able to do it.

Can you post some code for the s2hacks.php file to grab this info?

Thanks,

Jeffry

Posted: Friday Jul 20th, 2012 at 3:02 am #19801

Well, I thought about it some more and I’d probably not use that if I had to do it in my site…

What I’d most probably do is this: check [hilite mono]$_POST[s2member_pro_paypal_checkout][coupon][/hilite] and if there, apply the coupon to my button with my own code, instead of using the s2Member coupon definitions and processing. This would be a bit impractical if I had a ton of coupons, but I’d most likely just have a few and this wouldn’t be a problem.

You can do it in several ways, but here’s an example:

[hilite pre_code]

[/hilite]

I hope that helps.

Posted: Friday Jul 20th, 2012 at 3:40 am #19804

I was already able to extract the coupon code itself.

We have several coupons, and many more are likely to be added in the future. I don’t want to have to add additional if/else code to the pages every time we add a new coupon code.

I don’t understand this…the s2member system already computes and displays the revised order total on the screen after a coupon is entered. All I want to do is to be able to take that computation, and add it to the paypal short code.

Is that possible?

Posted: Friday Jul 20th, 2012 at 6:29 am #19814

The updated values are not available in a WP hook to use in your hack.

Also, the HTML form you get in after parsing the shortcode and applying the coupon, doesn’t have the changed amounts available because that info seems to be encrypted in the [hilite mono]s2member_pro_paypal_checkout[attr][/hilite] field.

Anyway, I spent some time studying the code and how you could do it, and here’s what I ended up with to get the amount updated by the coupon:

Move/copy the authnet-utilities.inc.php file to the /wp-content/mu-plugins/ directory. Open it in your text editor and search for the line:

[hilite pre_code]
return ( /* Returning ``$response``? */$return === "response") ? $response : $attr;
[/hilite]

and right before it add:

[hilite pre_code]
do_action('s2hacks_after_authnet_apply_coupon', $attr);
[/hilite]

Then in /wp-content/mu-plugins/s2hacks.php add this:

[hilite pre_code]
add_action('s2hacks_after_authnet_apply_coupon', 's2hacks_after_authnet_apply_coupon');
function s2hacks_after_authnet_apply_coupon($attr) {
print_r($attr);
}
[/hilite]

Save all files and submit a coupon code in your Auth.Net pro-form. You’ll see the [hilite mono]$attr[/hilite] array above the pro-form. You can take it from there to complete your hack.

I hope that helps.

Posted: Friday Jul 20th, 2012 at 4:51 pm #19854

Okay, cool.

I used wp_redirected to extract the values for ta and ra, and pass them back to the page in the form of url parameters, then I just did a simple $_GET to insert them into my paypal short code.

This is the appropriate code I added to my s2hacks.php file:

add_action('s2hacks_after_authnet_apply_coupon', 's2hacks_after_authnet_apply_coupon');
function s2hacks_after_authnet_apply_coupon($attr) {
	if ((!isset($_GET['ta']) && !isset($_GET['ra']) && strlen($attr[coupon]) > 0) || $attr[coupon] != $_GET['s2p-coupon']) {
		$urlRedirect = get_home_url().$_SERVER['REQUEST_URI'];
		if (strpos($urlRedirect, "?") > 0) {
			$urlRedirect .= '&s2p-coupon='.$attr[coupon].'&ta='.$attr[ta].'&ra='.$attr[ra];
		} else {
			$urlRedirect .= '?s2p-coupon='.$attr[coupon].'&ta='.$attr[ta].'&ra='.$attr[ra];
		}
		wp_redirect( $urlRedirect );
		exit;
	}
}

Here’s the code I used to update the ta value in the paypal short code:

<?php if (isset($_GET['ta'])) echo $_GET['ta']; else echo 27; ?>

This did the trick! Thank you so much for the help!

  • This reply was modified 4 years, 5 months ago by  Jeffry Evans.
  • This reply was modified 4 years, 5 months ago by  Jeffry Evans.
Posted: Friday Jul 20th, 2012 at 10:42 pm #19869

Hey, that’s great! You’re welcome. Thanks for the update. :)

I was thinking that something like this in s2hacks.php would be enough:

[hilite pre_code]

[/hilite]

And it’s possible that it may be better to not use [hilite mono]$_GET[/hilite] to avoid someone giving a different value for in the URL.

Posted: Saturday Jul 21st, 2012 at 1:17 am #19875

That’s definitely more secure, but it doesn’t work. For some reason, trying to set a global variable inside a function in s2hacks.php just doesn’t work.

Viewing 15 replies - 1 through 15 (of 15 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.