Skip to content

Instantly share code, notes, and snippets.

@bradt
Last active August 29, 2015 14:17

Revisions

  1. bradt revised this gist Mar 19, 2015. 1 changed file with 47 additions and 0 deletions.
    47 changes: 47 additions & 0 deletions url-coupons.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    $( document ).ready( function() {

    var cookie_name = 'dbrains-coupon';
    var cookie_name_error = 'dbrains-coupon-error';

    if ( 'undefined' === typeof $.cookie( cookie_name ) ) {
    return;
    }

    var message = 'The coupon code ' + $.cookie( cookie_name ).toUpperCase() + ' has been successfully applied.';
    var notice_class = 'dbrains-wc-coupons-notice';

    if ( 'undefined' !== typeof $.cookie( cookie_name_error ) ) {
    message = $.cookie( cookie_name_error );
    notice_class += ' error';
    }

    var $coupon_notice = $( '<div></div>', {
    'class': notice_class
    } );

    var $coupon_message = $( '<p></p>', {
    'class': 'dbrains-wc-coupons-notice-message'
    } );

    $coupon_message.html( message );
    $coupon_notice.prepend( $coupon_message )
    $( 'body' ).prepend( $coupon_notice );

    var coupon_height = $coupon_notice.outerHeight();
    $coupon_notice
    .css( 'margin-top', -1 * coupon_height )
    .show()
    .animate( { 'margin-top' : 0 }, 800 );

    setTimeout( function() {
    $coupon_notice
    .animate( { 'margin-top' : -1 * coupon_height }, 800, function() {
    $( this ).remove();
    } );
    }, 4000 );

    // clear the cookies
    $.removeCookie( cookie_name, { path: '/' } );
    $.removeCookie( cookie_name_error, { path: '/' } );

    } );
  2. bradt created this gist Mar 19, 2015.
    152 changes: 152 additions & 0 deletions url-coupons.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,152 @@
    <?php
    class DBrains_URL_Coupons {

    protected $cookie_prefix = 'dbrains';
    protected $cookie_expires_in;

    protected $code;
    protected $error_messsage;

    function __construct() {
    $this->cookie_expires_in = DAY_IN_SECONDS;

    add_action( 'wp', array( $this, 'process_coupon' ), 1 );

    //add_filter( 'woocommerce_coupons_enabled', array( $this, 'remove_coupon_input_from_checkout' ) );
    }

    /**
    * Set the coupon code
    *
    * @param string $code
    */
    protected function set_code( $code ) {
    $this->code = strtolower( $code );
    }

    /**
    * Get the name we are using for the cookie
    *
    * @return string
    */
    protected function get_cookie_name() {
    return $this->cookie_prefix . '-coupon';
    }

    /**
    * Get the name we are using for the error flag cookie
    *
    * @return string
    */
    protected function get_cookie_name_error() {
    return $this->cookie_prefix . '-coupon-error';
    }

    /**
    * Apply the coupon code to the WooCommerce cart
    *
    * @return bool
    */
    protected function apply_coupon() {
    $coupon = new WC_Coupon( $this->code );

    // Since we're already displaying notices for adding the coupon,
    // we don't want any of them showing up on the checkout
    // so we save the notices and overwrite later
    $notices_before = wc_get_notices();

    if ( ! empty( $notices_before['error'] ) ) {
    $errors_before_count = count( $notices_before['error'] );
    }
    else {
    $errors_before_count = 0;
    }

    // If the cart is empty, the coupon code will succeed in being added but
    // will then disappear when they add an item to their cart
    $cart = WC()->cart->get_cart();
    if ( empty ( $cart ) ) {
    WC()->cart->add_to_cart( 21 );
    }

    $result = WC()->cart->add_discount( $coupon->code );

    $notices = wc_get_notices();

    WC()->session->set( 'wc_notices', $notices_before );

    if ( $result ) {
    return true;
    }

    $error_messages = array();
    for ( $i = $errors_before_count; $i < count( $notices['error'] ); $i++ ) {
    $error_messages[] = $notices['error'][$i];
    }

    return new WP_Error( 'apply_coupon_error', implode( ' ', $error_messages ) );
    }

    /**
    * Clear the coupon cookies
    */
    protected function clear_cookies() {
    setcookie( $this->get_cookie_name(), '', time() - $this->cookie_expires_in, COOKIEPATH, COOKIE_DOMAIN );
    setcookie( $this->get_cookie_name_error(), '', time() - $this->cookie_expires_in, COOKIEPATH, COOKIE_DOMAIN );
    }

    /**
    * Handle the processing of the coupon and redirect of the page
    */
    public function process_coupon() {

    if ( empty( $_GET['coupon'] ) ) {
    return;
    }

    if ( '{coupon}' == $_GET['coupon'] ) {
    wp_redirect( remove_query_arg( 'coupon' ) );
    exit;
    }

    $this->set_code( $_GET['coupon'] );

    $result = $this->apply_coupon();

    $this->clear_cookies();

    setcookie( $this->get_cookie_name(), $this->code, time() + $this->cookie_expires_in, COOKIEPATH, COOKIE_DOMAIN );

    if ( is_wp_error( $result ) ) {
    setcookie( $this->get_cookie_name_error(), $result->get_error_message(), time() + $this->cookie_expires_in, COOKIEPATH, COOKIE_DOMAIN );
    }

    $redirect_url = remove_query_arg( 'coupon' );

    wp_redirect( $redirect_url );
    exit;
    }

    /**
    * Remove the coupon input from the checkout page
    *
    * @param bool $enable_coupons
    *
    * @return bool
    */
    public function remove_coupon_input_from_checkout( $enable_coupons ) {
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
    return $enable_coupons;
    }

    global $post;
    if ( isset( $post->ID ) && $post->ID === wc_get_page_id( 'checkout' ) ) {
    // we want to use coupons but just hide them on the checkout page
    return false;
    }

    return $enable_coupons;
    }
    }

    new DBrains_URL_Coupons();