Skip to content

Instantly share code, notes, and snippets.

@admcfajn
Created April 9, 2014 06:29
Show Gist options
  • Select an option

  • Save admcfajn/10231825 to your computer and use it in GitHub Desktop.

Select an option

Save admcfajn/10231825 to your computer and use it in GitHub Desktop.
PaidMembershipsPro Coupon Code on WooCommerce Checkout
/*
Add to your theme's functions.php or a plugin.
Requires PaidMembershipsPro, WooCommerce, & PMPro WooCommerce
This adds custom checkout fields to the woocommerce-checkout (The name & email of the person recieving the coupon-code)
& generates & emails a new coupon-code to the gift-recipient's email whenever a woo-commerce order is completed.
*/
/**
* Add the field to the checkout
**/
add_action('woocommerce_after_order_notes', 'my_custom_field');
function my_custom_field( $checkout ) {
echo '<div id="my_custom_field"><h2>'.__('Gift recipient name').'</h2>';
woocommerce_form_field( 'my_gift_recipient_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Gift recipient name'),
'placeholder' => __(' '),
), $checkout->get_value( 'my_gift_recipient_name' ));
echo '</div>';
echo '<div id="my_custom_field"><h2>'.__('Gift recipient email').'</h2>';
woocommerce_form_field( 'my_gift_recipient_email', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Gift recipient email'),
'placeholder' => __('someone@somewhere.com'),
), $checkout->get_value( 'my_gift_recipient_email' ));
echo '</div>';
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field');
function my_custom_checkout_field() {
global $woocommerce;
// Check if set, if its not set add an error.
if (!$_POST['my_gift_recipient_name'])
$woocommerce->add_error( __('Please enter something into the "Gift recipient name" field.') );
if (!$_POST['my_gift_recipient_email'])
$woocommerce->add_error( __('Please enter something into the "Gift recipient email" field.') );
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['my_gift_recipient_name']) update_post_meta( $order_id, 'Gift recipient name', esc_attr($_POST['my_gift_recipient_name']));
if ($_POST['my_gift_recipient_email']) update_post_meta( $order_id, 'Gift recipient email', esc_attr($_POST['my_gift_recipient_email']));
}
/**
* Display field value on the order edition page
**/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
//add_action('woocommerce_thankyou', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Gift recipient name').':</strong> ' . $order->order_custom_fields['Gift recipient name'][0] . '</p>';
echo '<p><strong>'.__('Gift recipient email').':</strong> ' . $order->order_custom_fields['Gift recipient email'][0] . '</p>';
}
/************ GENERATE DISCOUNT CODE WHEN PURCHASED ****************/
function my_add_new_discount_code($order_id){
global $wpdb, $woocommerce;
$code = pmpro_getDiscountCode();
$starts = date("Y-m-d");
$expires = date("Y-m-d", strtotime("+1 year"));
$uses = 1;
$sqlQuery = "INSERT INTO $wpdb->pmpro_discount_codes (code, starts, expires, uses) VALUES('" . esc_sql($code) . "', '" . $starts . "', '" . $expires . "', '" . intval($uses) . "')";
if($wpdb->query($sqlQuery) !== false)
{
$pmpro_msg = __("Discount code added successfully.", "pmpro");
$pmpro_msgt = "success";
$saved = true;
$edit = $wpdb->insert_id;
}
else
{
$pmpro_msg = __("Error adding discount code. That code may already be in use.", "pmpro") . $wpdb->last_error;
$pmpro_msgt = "error";
}
$level_id = 1;
$initial_payment = 0;
$billing_amount = 1.99;
$cycle_number = 1;
$cycle_period = 'Month';
$billing_limit = 0;
$trial_amount = 0;
$trial_limit = 11;
$expiration_number = 0;
$expiration_period = 'Year';
$sqlQuery = "INSERT INTO $wpdb->pmpro_discount_codes_levels (code_id, level_id, initial_payment, billing_amount, cycle_number, cycle_period, billing_limit, trial_amount, trial_limit, expiration_number, expiration_period) VALUES('" . esc_sql($edit) . "', '" . esc_sql($level_id) . "', '" . (double)esc_sql($initial_payment) . "', '" . (double)esc_sql($billing_amount) . "', '" . intval(esc_sql($cycle_number)) . "', '" . esc_sql($cycle_period) . "', '" . intval(esc_sql($billing_limit)) . "', '" . (double)esc_sql($trial_amount) . "', '" . intval(esc_sql($trial_limit)) . "', '" . intval(esc_sql($expiration_number)) . "', '" . esc_sql($expiration_period) . "')";
$wpdb->query($sqlQuery);
$gift_recipient_email = get_post_meta( $order_id, 'Gift recipient email', true );
$gift_recipient_name = get_post_meta( $order_id, 'Gift recipient name', true );
//print_r($woocommerce);
$headers='';$attachments='';
wp_mail( $gift_recipient_email, 'Your Gift Membership Code: '.$code,'Dear '.$gift_recipient_name.', Welcome! Use this coupon-code to get started! '.$code, $headers, $attachments );
}
add_action('woocommerce_checkout_order_processed', 'my_add_new_discount_code', 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment