Last active
March 27, 2016 14:22
-
-
Save ThomasLeCoz/e3b155be83e3735fb781 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** PHP Script to generate WooCommerce temporary coupon from ActiveCampaign **/ | |
/** By Thomas Le Coz - http://thomaslecoz.com **/ | |
/** Configuration **/ | |
$key = "YOURSECRETKEY"; // Secret key you should add to the webhook URL too: http://yourwebsite.com/coupon-gen/coupon-generator.php?key=YOURSECRETKEY | |
$ck_woocommerce = 'YOUR_WOOCOMEMRCE_API_CLIENT_KEY'; // WooCommerce API client key | |
$cs_woocommerce = 'YOUR_WOOCOMEMRCE_API_SECRET_KEY'; // WooCommerce API secret key | |
// Catch Hook from ActiveCampaign & basic security check | |
if ($_POST['contact'] && ($_GET['key'] == $key)) { | |
$user_email = $_POST['contact']['email']; | |
// Create expiry date for the coupon set to 5 days | |
$expiry_date = strtotime("+5 day"); | |
$coupon_expiry = date('Y-m-dTh:m:s', $expiry_date); | |
// Use WooCommerce REST API to create a coupon code to the user | |
require_once( 'lib/woocommerce-api.php' ); | |
$options = array( | |
'debug' => false, | |
'return_as_array' => false, | |
'validate_url' => false, | |
'timeout' => 30, | |
'ssl_verify' => false, | |
); | |
try { | |
$client = new WC_API_Client( 'https://www.YOURWEBSITE.com', $ck_woocommerce, $cs_woocommerce, $options ); | |
print_r( $client->coupons->create( array( 'code' => $user_email, 'type' => 'percent', 'amount' => 20, 'individual_use' => true, 'product_ids' => [3639,3637,3486], 'usage_limit' => 1, 'usage_limit_per_user' => 1, 'expiry_date' => $coupon_expiry, 'description' => 'Temporary coupon generated for ' . $user_email, 'customer_emails' => $user_email) ) ); | |
} catch ( WC_API_Client_Exception $e ) { | |
echo $e->getMessage() . PHP_EOL; | |
echo $e->getCode() . PHP_EOL; | |
if ( $e instanceof WC_API_Client_HTTP_Exception ) { | |
print_r( $e->get_request() ); | |
print_r( $e->get_response() ); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment