Er wordt gebruik gemaakt van 2 functies. 1 om een uniek ID te genereren en 1 om de coupon aan te maken. De expiry date kan worden aangepast naar een datum die wenselijk is zoals in het voorbeeld. Het email adres kan opgehaald worden vanuit een gforms veld zodat de coupon kan worden ingeleverd door dezelfde persoon die het formulier verzend.

In functions.php

// Genereer coupon code by verzenden Gforms.

class GW_Create_Coupon {

    public function __construct( $args = array() ) {

        // set our default arguments, parse against the provided arguments, and store for use throughout the class
        $this->_args = wp_parse_args( $args, array(
            'form_id'         => false,
            'source_field_id' => false,
            'plugin'          => 'wc', // accepts: 'gf', 'wc', 'edd'
            'amount'          => 0,
            'type'            => '',
            'meta'            => array()
        ) );

        // do version check in the init to make sure if GF is going to be loaded, it is already loaded
        add_action( 'init', array( $this, 'init' ) );

    }

    public function init() {

        // make sure we're running the required minimum version of Gravity Forms and that WooCommerce is active
        if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
            return;
        }

        add_action( 'gform_after_submission', array( $this, 'create_coupon' ), 10, 2 );

    }

    public function create_coupon( $entry, $form ) {

        if( ! $this->is_applicable_form( $form ) ) {
            return;
        }

        $coupon_code = rgar( $entry, $this->_args['source_field_id'] );
        $email_addr = rgar( $entry, $this->_args['email_field_id'] );
        global $email_addr;
        $amount      = $this->_args['amount'];
        $type        = $this->_args['type'];

	    if( ! $coupon_code ) {
		    return;
	    }

        $plugin_func = array( $this, sprintf( 'create_coupon_%s', $this->_args['plugin'] ) );

        if( is_callable( $plugin_func ) ) {
            call_user_func( $plugin_func, $coupon_code, $amount, $type, $email_addr );
        }

    }

    public function create_coupon_wc( $coupon_code, $amount, $type, $email_addr ) {

        $coupon = array(
            'post_title'   => $coupon_code,
            'post_content' => '',
            'post_status'  => 'publish',
            'post_author'  => 1,
            'post_type'	   => 'shop_coupon'
        );

        $new_coupon_id = wp_insert_post( $coupon );
		
        $meta = wp_parse_args( $this->_args['meta'], array(
            'discount_type'              => $type,
            'coupon_amount'              => $amount,
            'individual_use'             => 'yes',
            'product_ids'                => '',
            'exclude_product_ids'        => '',
            'usage_limit'                => '1',
            'expiry_date'                => '',
            'apply_before_tax'           => 'no',
            'free_shipping'              => 'no',
            'exclude_sale_items'         => 'no',
            'product_categories'         => '',
            'exclude_product_categories' => '',
            'minimum_amount'             => '',
            'customer_email'             => $email_addr
        ) );

        foreach( $meta as $meta_key => $meta_value ) {
            update_post_meta( $new_coupon_id, $meta_key, $meta_value );
        }

    }

    function is_applicable_form( $form ) {

        $form_id = isset( $form['id'] ) ? $form['id'] : $form;

        return $form_id == $this->_args['form_id'];
    }

}

# Configuration

new GW_Create_Coupon( array(
 'form_id' => 2,
 'source_field_id' => 1,
 'email_field_id' => 4,
 'plugin' => 'wc',
 'amount' => 10,
 'type' => 'fixed_cart', // accepts: 'fixed_cart', 'percent', 'fixed_product', 'percent_product'
 'meta' => array(
 'apply_before_tax' => 'no',
 'customer_email' => rgpost( 'input_4', true ),
 'exclude_product_categories' => array(),
 'exclude_product_ids' => '',
 'exclude_sale_items' => 'no',
 'expiry_date' => date( 'm/d/Y', strtotime( '+1 week' )),
 'free_shipping' => 'no',
 'individual_use' => 'yes',
 'limit_usage_per_customer' => '',
 'limit_usage_to_x_items' => '',
 'maximum_amount' => '',
 'minimum_amount' => '',
 'product_categories' => array(),
 'product_ids' => '',
 'usage_limit' => 1
 )
) );

add_filter("gform_field_value_uuid", "get_unique");
function get_unique(){
    $prefix = "FR"; // pas de prefix aan indien gewenst
    do {
        $unique = mt_rand();
        $unique = substr($unique, 0, 8); // wijzig dit indien er meer of minder karakters moeten worden gebruikt.
        $unique = $prefix . $unique;
    } while (!check_unique($unique));
    return $unique;
}
function check_unique($unique) {
    global $wpdb;
    $table = $wpdb->prefix . 'rg_lead_detail';
    $form_id = 2; // Voer hier het form ID in
    $field_id = 6; // Voer hier het field ID in
    $result = $wpdb->get_var("SELECT value FROM $table WHERE form_id = '$form_id' AND field_number = '$field_id' AND value = '$unique'");
    if(empty($result))
        return true;
    return false;
}

 

Geef een reactie 0