This coupon code example is created by the Community/Developer Evangelism group. Please file any issues through the Github project.
You’ll need to login to www.paypal.com to create your hosted buttons.
- Create a buy now button at full price and use that as your default hostedButtonId by assigning it to the $hostedButtonId variable.
- Set The default full price by assigning it to the $amount variable this should match the price for the button you just created at paypal.com
- Create additional buttons at www.paypal.com and apply discounts through the HTML Variables. See our screencast on how to apply discounts with HTML variables.
- Add the hostedButtonIds for the discount button to the array. Also, add the discount amount.
- Note, this example is using a fixed discount amount.
- If you want to use a percentage discount, update this code $amount = ($amount – $discount) to $amount = ($amount – ($amount * .$discount));
<?php $hostedButtonId = ""; $amount = 0; $arr = array( "take5" => array("hostedButtonId" => "", "discount" => "5"), "take10" => array("hostedButtonId" => "", "discount" => "10") ); foreach ($arr as $key => $value) { if ($key == $_POST["couponCode"]) { $hostedButtonId = $value["hostedButtonId"]; $discount = $value["discount"]; $amount = ($amount - $discount); break; } } ?>
This example requires the following features to be available on a hosted web server:
- [PHP Installtation] – PHP 5.2.x or above.
Copyright 2011 X.commerce
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.