function ym_render_coupons() { $this_page = YM_ADMIN_URL . '&ym_page=' . ym_get('ym_page'); echo '<script> function ym_confirm_coupon_delete(id) { if (confirm("' . __('Are you sure you want to delete this coupon?', 'ym') . '")) { document.location="' . $this_page . '&delete_coupon="+id; } } </script>'; echo '<div style="margin-bottom: 10px;"> <table style="width:100%;" class="widefat"> <thead>'; echo ' <tr> <th>ID</th> <th>' . __('Code', 'ym') . '</th> <th>' . __('Value', 'ym') . '</th> <th>' . __('Description', 'ym') . '</th> <th>' . __('New Subscriber', 'ym') . '</th> <th>' . __('Subscription Upgrade', 'ym') . '</th> <th>' . __('Post Purchase', 'ym') . '</th> <th>' . __('Pack Purchase', 'ym') . '</th> <th>' . __('Usage Limit/Uses Left', 'ym') . '</th> <th>' . __('Date Created', 'ym') . '</th> <th style="width: 180px;">' . __('Action', 'ym') . '</th> </tr> </thead>'; if ($coupons = ym_get_coupons()) { foreach ($coupons as $i => $coupon) { echo '<tr ' . ($i % 2 ? 'class="alternate"' : '') . '> <td>' . $coupon->id . '</td> <td>' . $coupon->name . '</td> <td>' . $coupon->value . '</td> <td>' . $coupon->description . '</td> '; $uses = str_split($coupon->allowed); foreach ($uses as $use) { echo '<td>' . $use . '</td>'; } $left = ym_coupon_get_uses_left($coupon->id, FALSE); echo ' <td>' . $coupon->usage_limit . '/' . $left . ' '; if ($coupon->usage_limit == 0 && $left == 1) { echo __('(Unlimited Usage)', 'ym'); } echo ' </td> <td>' . date(YM_DATE, $coupon->unixtime + get_option('gmt_offset') * 3600) . '</td> <td> <form method="POST" action="' . $this_page . '&coupon_id=' . $coupon->id . '" style="margin: 0px; padding: 0px;"> <input class="button" name="edit" type="submit" value="' . __('Edit', 'ym') . '" /> <input onclick="ym_confirm_coupon_delete(' . $coupon->id . ');" class="button" type="button" value="' . __('Delete', 'ym') . '" /> <input class="button" name="view" type="submit" value="' . __('Users', 'ym') . '" /> </form> </td> </tr>'; } } else { echo '<tr> <td colspan="5">' . __('There are currently no coupons in the database.', 'ym') . '</td> </tr>'; } echo '</table></div>'; echo '<div> <form method="POST" style="margin: 0px; pading: 0px;"> <table class="widefat form-table"> <thead> <tr> <th colspan="4">' . __('Add a new Coupon', 'ym') . '</th> </tr> </thead> <tr> <td>' . __('Code', 'ym') . ': <input name="name" style="width: 240px;" /></td> <td>' . __('Value', 'ym') . ': <input name="value" style="width: 50px;" /></td> <td>' . __('Description', 'ym') . ': <input name="description" style="width: 400px;" /></td> <td style="text-align: right;"> <input class="button" type="submit" name="save_coupon" value="' . __('Save', 'ym') . '" /> </td> </tr> <tr> <td style="text-align: right;">' . __('Coupon is Valid for', 'ym') . '</td> <td colspan="2"> <label> ' . __('New Subscriber', 'ym') . ' <input type="checkbox" name="new_sub" /> </label> <label> ' . __('Subscription Upgrade', 'ym') . ' <input type="checkbox" name="upgrade" /> </label> <label> ' . __('Post Purchase', 'ym') . ' <input type="checkbox" name="post" /> </label> <label> ' . __('Pack Purchase', 'ym') . ' <input type="checkbox" name="pack" /> </label> </td> <td> <label> ' . __('Usage Limit, (0 for Unlimited)', 'ym') . '<input type="text" name="usage_limit" size="2" value="0" /> </label> </td> </tr> </table> <p> ' . __('In value you can specify a percentage, fixed value or hidden package.<br />To specify a percentage append % to the end of your value, eg 22%, for a hidden package prepend # to the value eg #1.<br />The package id can be found on the packages tab.', 'ym') . ' </p> </form> </div>'; }
private function sync_coupons($coupon_name = FALSE, $type = 'name') { $r = TRUE; $coupons = array(); if ($coupon_name && $type == 'name') { if ($id = ym_get_coupon_id_by_name($coupon_name)) { $coupons = array(ym_get_coupon($id)); } } else { if ($coupon_name && $type == 'id') { $coupons = array(ym_get_coupon($coupon_name)); } else { $coupons = ym_get_coupons(); } } foreach ($coupons as $coupon) { $value = $coupon->value; if (ym_get_coupon_type($value) == 'percent') { // only support percentages $allowed = str_split($coupon->allowed); if ($allowed[0] == 1 || $allowed[1] == 1) { // sub enabled $value = str_replace('%', '', $value); $create = FALSE; $id = 'ym_' . $coupon->id; $mycoupon = array('id' => $id, 'percent_off' => $value, 'duration' => 'once'); list($r_code, $response) = $this->stripe_api_request('coupons/' . $id); if ($r_code == 200) { // check ok if ($response->percent_off != $value || $response->duration != 'once') { // update needed list($r_code, $response) = $this->stripe_api_request('coupons', 'DELETE', array('id' => $id)); if ($r_code == 200) { $create = TRUE; } else { $r = FALSE; } } } else { $create = TRUE; } if ($create) { // doesn't exist list($r_code, $response) = $this->stripe_api_request('coupons', 'POST', $mycoupon); if ($r_code != 200) { $r = FALSE; } } } } } return $r; }