Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is how I create a promo code. I'm only using percentage off, but you can set it up however you want. I'm getting $_POST['percentage'] and $_POST['coupon-code'] from a front-end form, along with the nonces
<?php
if ( isset( $_POST['action'] ) && 'add-coupon-code' == $_POST['action'] ) {
if ( empty( $_POST['add_coupon_code_nonce'] ) || ! wp_verify_nonce( $_POST['add_coupon_code_nonce'], 'add_coupon_code' ) ) {
fc_message( "You don't have permissions to add a coupon code.", 'error' );
} else {
$_POST['percentage'] = filter_percentage( $_POST['percentage'] );
$_POST['coupon-code'] = sanitize_key( $_POST['coupon-code'] );
$Promotion = new ShoppPromo();
$data = array(
'name' => $_POST['coupon-code'],
'status' => 'enabled',
'target' => 'Cart',
'starts' => 1,
'ends' => 1,
'search' => 'all',
'type' => 'Percentage Off',
'discount' => $_POST['percentage'] . '%',
'rules' => array(
1 => array(
'property' => 'Promo code',
'logic' => 'Is equal to',
'value' => $_POST['coupon-code'],
),
),
);
$Promotion->updates( $data );
$promo_id = $Promotion->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment