• 1.
    <?php
  • 2.
    // Exit if accessed directly
  • 3.
    if ( !defined( 'ABSPATH' ) ) exit;
  • 4.
     
  • 5.
    // BEGIN ENQUEUE PARENT ACTION
  • 6.
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
  • 7.
     
  • 8.
    if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
  • 9.
        function chld_thm_cfg_locale_css( $uri ){
  • 10.
            if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
  • 11.
                $uri = get_template_directory_uri() . '/rtl.css';
  • 12.
            return $uri;
  • 13.
        }
  • 14.
    endif;
  • 15.
    add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
  • 16.
     
  • 17.
    if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
  • 18.
        function chld_thm_cfg_parent_css() {
  • 19.
            wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array(  ) );
  • 20.
            
  • 21.
        }
  • 22.
    endif;
  • 23.
    add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
  • 24.
             
  • 25.
    if ( !function_exists( 'child_theme_configurator_css' ) ):
  • 26.
        function child_theme_configurator_css() {
  • 27.
            wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'chld_thm_cfg_parent' ) );
  • 28.
        }
  • 29.
    endif;
  • 30.
    add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 );
  • 31.
     
  • 32.
    // END ENQUEUE PARENT ACTION
  • 33.
    // 
  • 34.
    // add_action( 'woocommerce_thankyou', 'bbloomer_redirectcustom');
  • 35.
      
  • 36.
    function bbloomer_redirectcustom( $order_id ){
  • 37.
        $order = wc_get_order( $order_id );
  • 38.
        $url = 'http://oxy.wpguru.fi/tilaus-valmis/';
  • 39.
        if ( ! $order->has_status( 'failed' ) ) {
  • 40.
            wp_safe_redirect( $url );
  • 41.
            exit;
  • 42.
        }
  • 43.
    }
  • 44.
     
  • 45.
    add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
  • 46.
     
  • 47.
    function woocommerce_header_add_to_cart_fragment( $fragments ) {
  • 48.
        global $woocommerce;
  • 49.
     
  • 50.
        ob_start();
  • 51.
     
  • 52.
        ?>
  • 53.
        <a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>"><?php WC()->cart->get_cart_contents_count(); ?></a>
  • 54.
     
  • 55.
        <?php
  • 56.
     
  • 57.
        $fragments['a.cart-customlocation'] = ob_get_clean();
  • 58.
     
  • 59.
        return $fragments;
  • 60.
     
  • 61.
    }
  • 62.
     
  • 63.
    /** Disable Ajax Call from WooCommerce */
  • 64.
    add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments', 11); 
  • 65.
    function dequeue_woocommerce_cart_fragments() { if (is_front_page()) wp_dequeue_script('wc-cart-fragments'); } 
  • 66.
     
  • 67.
    /**
  • 68.
     * Gravity Wiz // Gravity Forms // Create Coupons with Gravity Forms for Gravity Forms, WooCommerce, or Easy Digital Downloads
  • 69.
     *
  • 70.
     * Create coupons via Gravity Forms submissions. Map the coupon code to a field on the GF form and voila!
  • 71.
     *
  • 72.
     * @version 1.2
  • 73.
     * @author  David Smith <[email protected]>
  • 74.
     * @license GPL-2.0+
  • 75.
     * @link    WooCommerce:   http://gravitywiz.com/creating-coupons-woocommerce-gravity-forms/
  • 76.
     * @link    Gravity Forms: http://gravitywiz.com/creating-coupons-for-gf-coupons-add-on-with-gravity-forms/
  • 77.
     */
  • 78.
    class GW_Create_Coupon {
  • 79.
     
  • 80.
        public function __construct( $args = array() ) {
  • 81.
     
  • 82.
            // set our default arguments, parse against the provided arguments, and store for use throughout the class
  • 83.
            $this->_args = wp_parse_args( $args, array(
  • 84.
                'form_id'         => false,
  • 85.
                'source_field_id' => false,
  • 86.
                'plugin'          => 'gf', // accepts: 'gf', 'wc', 'edd'
  • 87.
                'amount'          => 0,
  • 88.
                'type'            => '', // accepts: 'fixed_cart', 'percent', 'fixed_product', 'percent_product'
  • 89.
                'meta'            => array()
  • 90.
            ) );
  • 91.
     
  • 92.
            // do version check in the init to make sure if GF is going to be loaded, it is already loaded
  • 93.
            add_action( 'init', array( $this, 'init' ) );
  • 94.
     
  • 95.
        }
  • 96.
     
  • 97.
        public function init() {
  • 98.
     
  • 99.
            // make sure we're running the required minimum version of Gravity Forms and that WooCommerce is active
  • 100.
            if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
  • 101.
                return;
  • 102.
            }
  • 103.
     
  • 104.
            add_action( 'gform_after_submission', array( $this, 'create_coupon' ), 10, 2 );
  • 105.
     
  • 106.
        }
  • 107.
     
  • 108.
        public function create_coupon( $entry, $form ) {
  • 109.
     
  • 110.
            if( ! $this->is_applicable_form( $form ) ) {
  • 111.
                return;
  • 112.
            }
  • 113.
     
  • 114.
            $coupon_code = rgar( $entry, $this->_args['source_field_id'] );
  • 115.
            $amount      = $this->_args['amount'];
  • 116.
            $type        = $this->_args['type'];
  • 117.
     
  • 118.
    	    if( ! $coupon_code ) {
  • 119.
    		    return;
  • 120.
    	    }
  • 121.
     
  • 122.
    	    if( is_callable( $amount ) ) {
  • 123.
    	    	$amount = call_user_func( $amount );
  • 124.
    	    }
  • 125.
     
  • 126.
            $plugin_func = array( $this, sprintf( 'create_coupon_%s', $this->_args['plugin'] ) );
  • 127.
     
  • 128.
            if( is_callable( $plugin_func ) ) {
  • 129.
                call_user_func( $plugin_func, $coupon_code, $amount, $type, $entry, $form );
  • 130.
            }
  • 131.
     
  • 132.
        }
  • 133.
     
  • 134.
        public function create_coupon_edd( $coupon_code, $amount, $type, $entry, $form ) {
  • 135.
     
  • 136.
            if( ! is_callable( 'edd_store_discount' ) ) {
  • 137.
                return;
  • 138.
            }
  • 139.
     
  • 140.
            $meta = wp_parse_args( $this->_args['meta'], array(
  • 141.
                'name'              => $coupon_code,
  • 142.
                'code'              => $coupon_code,
  • 143.
                'type'              => $type,
  • 144.
                'amount'            => $amount,
  • 145.
                'excluded_products' => array(),
  • 146.
                'expiration'        => '',
  • 147.
                'is_not_global'     => false,
  • 148.
                'is_single_use'     => false,
  • 149.
                'max_uses'          => '',
  • 150.
                'min_price'         => '',
  • 151.
                'product_condition' => '',
  • 152.
                'product_reqs'      => array(),
  • 153.
                'start'             => '',
  • 154.
                'uses'              => '',
  • 155.
            ) );
  • 156.
     
  • 157.
            // EDD will set it's own defaults in the edd_store_discount() so let's filter out our own empty defaults (they're just here for easier reference)
  • 158.
            $meta = array_filter( $meta );
  • 159.
     
  • 160.
            // EDD takes a $details array which has some different keys than the meta, let's map the keys to the expected format
  • 161.
            $edd_post_keys = array(
  • 162.
                'max_uses'          => 'max',
  • 163.
                'product_reqs'      => 'products',
  • 164.
                'excluded_products' => 'excluded-products',
  • 165.
                'is_not_global'     => 'not_global',
  • 166.
                'is_single_use'     => 'use_once'
  • 167.
            );
  • 168.
     
  • 169.
            foreach( $meta as $key => $value ) {
  • 170.
                $mod_key = rgar( $edd_post_keys, $key );
  • 171.
                if( $mod_key ) {
  • 172.
                    $meta[$mod_key] = $value;
  • 173.
                }
  • 174.
            }
  • 175.
     
  • 176.
            edd_store_discount( $meta );
  • 177.
     
  • 178.
        }
  • 179.
     
  • 180.
        public function create_coupon_gf( $coupon_code, $amount, $type, $entry, $form ) {
  • 181.
     
  • 182.
            if( ! class_exists( 'GFCoupons' ) ) {
  • 183.
                return;
  • 184.
            }
  • 185.
     
  • 186.
            // hack to load GF Coupons data.php file
  • 187.
    	    if( is_callable( 'gf_coupons' ) ) {
  • 188.
    		    gf_coupons()->get_config( array( 'id' => 0 ), false );
  • 189.
    	    } else {
  • 190.
    		    GFCoupons::get_config( array( 'id' => 0 ), false );
  • 191.
    	    }
  • 192.
     
  • 193.
            $meta = wp_parse_args( $this->_args['meta'], array(
  • 194.
                'form_id'           => false,
  • 195.
                'coupon_name'       => $coupon_code,
  • 196.
                'coupon_code'       => strtoupper( $coupon_code ),
  • 197.
                'coupon_type'       => $type, // 'flat', 'percentage'
  • 198.
                'coupon_amount'     => $amount,
  • 199.
                'coupon_start'      => '', // MM/DD/YYYY
  • 200.
                'coupon_expiration' => '', // MM/DD/YYYY
  • 201.
                'coupon_limit'      => false,
  • 202.
                'coupon_stackable'  => false
  • 203.
            ) );
  • 204.
     
  • 205.
            $form_id = $meta['form_id'] ? $meta['form_id'] : 0;
  • 206.
            unset( $meta['form_id'] );
  • 207.
     
  • 208.
            foreach ( $meta as $key => $value ) {
  • 209.
            	if ( $value instanceof Closure && is_callable( $value ) ) {
  • 210.
            		$meta[ $key ] = call_user_func( $value, $entry, $form, $this );
  • 211.
    			}
  • 212.
    		}
  • 213.
     
  • 214.
    	    if( is_callable( 'gf_coupons' ) ) {
  • 215.
    		    $meta['gravityForm']      = $form_id ? $form_id : 0;
  • 216.
    		    $meta['couponName']       = $meta['coupon_name'];
  • 217.
    		    $meta['couponCode']       = $meta['coupon_code'];
  • 218.
    		    $meta['couponAmountType'] = $meta['coupon_type'];
  • 219.
    		    $meta['couponAmount']     = $meta['coupon_amount'];
  • 220.
    		    $meta['startDate']        = $meta['coupon_start'];
  • 221.
    		    $meta['endDate']          = $meta['coupon_expiration'];
  • 222.
    		    $meta['usageLimit']       = $meta['coupon_limit'];
  • 223.
    		    $meta['isStackable']      = $meta['coupon_stackable'];
  • 224.
    		    $meta['usageCount']       = 0;
  • 225.
    		    gf_coupons()->insert_feed( $form_id, true, $meta );
  • 226.
    	    } else {
  • 227.
    		    GFCouponsData::update_feed( 0, $form_id, true, $meta );
  • 228.
    	    }
  • 229.
     
  • 230.
        }
  • 231.
     
  • 232.
        /**
  • 233.
         * Create a WooCommerce coupon.
  • 234.
         *
  • 235.
         * @link https://gist.github.com/mikejolley/3969579#file-gistfile1-txt
  • 236.
         */
  • 237.
        public function create_coupon_wc( $coupon_code, $amount, $type, $entry, $form ) {
  • 238.
     
  • 239.
            $coupon = array(
  • 240.
                'post_title'   => $coupon_code,
  • 241.
                'post_content' => '',
  • 242.
                'post_status'  => 'publish',
  • 243.
                'post_author'  => 1,
  • 244.
                'post_type'	   => 'shop_coupon'
  • 245.
            );
  • 246.
     
  • 247.
            $new_coupon_id = wp_insert_post( $coupon );
  • 248.
     
  • 249.
            $meta = wp_parse_args( $this->_args['meta'], array(
  • 250.
                'discount_type'              => $type,
  • 251.
                'coupon_amount'              => $amount,
  • 252.
                'individual_use'             => 'yes',
  • 253.
                'product_ids'                => '',
  • 254.
                'exclude_product_ids'        => '',
  • 255.
                'usage_limit'                => '1',
  • 256.
                'expiry_date'                => '',
  • 257.
                'apply_before_tax'           => 'no',
  • 258.
                'free_shipping'              => 'no',
  • 259.
                'exclude_sale_items'         => 'no',
  • 260.
                'product_categories'         => '',
  • 261.
                'exclude_product_categories' => '',
  • 262.
                'minimum_amount'             => '',
  • 263.
                'customer_email'             => ''
  • 264.
            ) );
  • 265.
     
  • 266.
            foreach( $meta as $meta_key => $meta_value ) {
  • 267.
                update_post_meta( $new_coupon_id, $meta_key, $meta_value );
  • 268.
            }
  • 269.
     
  • 270.
        }
  • 271.
     
  • 272.
        function is_applicable_form( $form ) {
  • 273.
     
  • 274.
            $form_id = isset( $form['id'] ) ? $form['id'] : $form;
  • 275.
     
  • 276.
            return $form_id == $this->_args['form_id'];
  • 277.
        }
  • 278.
     
  • 279.
    }
  • 280.
     
  • 281.
    # Configuration
  • 282.
     
  • 283.
    new GW_Create_Coupon( array(
  • 284.
        // ID of the form which will be used to create coupons
  • 285.
        'form_id'         => 1,
  • 286.
        // ID of the field whose value will be used as the coupon code
  • 287.
        'source_field_id' => 3,
  • 288.
        // which plugin the coupon should be created for (i.e. WooCommerce = 'wc')
  • 289.
        'plugin'          => 'wc', // accepts: 'gf', 'wc', 'edd'
  • 290.
        // type of coupon code to be created, available types will differ depending on the plugin
  • 291.
        'type'            => 'percent',
  • 292.
        // amount of the coupon discount
  • 293.
        'amount'          => 10
  • 294.
    ) );
  • 295.
     
  • 296.
     
  • 297.
     
  • 298.
     
  • 299.
     
<?php // Exit if accessed directly if ( !defined( 'ABSPATH' ) ) exit; // BEGIN ENQUEUE PARENT ACTION // AUTO GENERATED - Do not modify or remove comment markers above or below: if ( !function_exists( 'chld_thm_cfg_locale_css' ) ): function chld_thm_cfg_locale_css( $uri ){ if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) ) $uri = get_template_directory_uri() . '/rtl.css'; return $uri; } endif; add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' ); if ( !function_exists( 'chld_thm_cfg_parent_css' ) ): function chld_thm_cfg_parent_css() { wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( ) ); } endif; add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 ); if ( !function_exists( 'child_theme_configurator_css' ) ): function child_theme_configurator_css() { wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'chld_thm_cfg_parent' ) ); } endif; add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 ); // END ENQUEUE PARENT ACTION // // add_action( 'woocommerce_thankyou', 'bbloomer_redirectcustom'); function bbloomer_redirectcustom( $order_id ){ $order = wc_get_order( $order_id ); $url = 'http://oxy.wpguru.fi/tilaus-valmis/'; if ( ! $order->has_status( 'failed' ) ) { wp_safe_redirect( $url ); exit; } } add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment'); function woocommerce_header_add_to_cart_fragment( $fragments ) { global $woocommerce; ob_start(); ?> <a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>"><?php WC()->cart->get_cart_contents_count(); ?></a> <?php $fragments['a.cart-customlocation'] = ob_get_clean(); return $fragments; } /** Disable Ajax Call from WooCommerce */ add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments', 11); function dequeue_woocommerce_cart_fragments() { if (is_front_page()) wp_dequeue_script('wc-cart-fragments'); } /** * Gravity Wiz // Gravity Forms // Create Coupons with Gravity Forms for Gravity Forms, WooCommerce, or Easy Digital Downloads * * Create coupons via Gravity Forms submissions. Map the coupon code to a field on the GF form and voila! * * @version 1.2 * @author David Smith <[email protected]> * @license GPL-2.0+ * @link WooCommerce: http://gravitywiz.com/creating-coupons-woocommerce-gravity-forms/ * @link Gravity Forms: http://gravitywiz.com/creating-coupons-for-gf-coupons-add-on-with-gravity-forms/ */ 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' => 'gf', // accepts: 'gf', 'wc', 'edd' 'amount' => 0, 'type' => '', // accepts: 'fixed_cart', 'percent', 'fixed_product', 'percent_product' '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'] ); $amount = $this->_args['amount']; $type = $this->_args['type']; if( ! $coupon_code ) { return; } if( is_callable( $amount ) ) { $amount = call_user_func( $amount ); } $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, $entry, $form ); } } public function create_coupon_edd( $coupon_code, $amount, $type, $entry, $form ) { if( ! is_callable( 'edd_store_discount' ) ) { return; } $meta = wp_parse_args( $this->_args['meta'], array( 'name' => $coupon_code, 'code' => $coupon_code, 'type' => $type, 'amount' => $amount, 'excluded_products' => array(), 'expiration' => '', 'is_not_global' => false, 'is_single_use' => false, 'max_uses' => '', 'min_price' => '', 'product_condition' => '', 'product_reqs' => array(), 'start' => '', 'uses' => '', ) ); // EDD will set it's own defaults in the edd_store_discount() so let's filter out our own empty defaults (they're just here for easier reference) $meta = array_filter( $meta ); // EDD takes a $details array which has some different keys than the meta, let's map the keys to the expected format $edd_post_keys = array( 'max_uses' => 'max', 'product_reqs' => 'products', 'excluded_products' => 'excluded-products', 'is_not_global' => 'not_global', 'is_single_use' => 'use_once' ); foreach( $meta as $key => $value ) { $mod_key = rgar( $edd_post_keys, $key ); if( $mod_key ) { $meta[$mod_key] = $value; } } edd_store_discount( $meta ); } public function create_coupon_gf( $coupon_code, $amount, $type, $entry, $form ) { if( ! class_exists( 'GFCoupons' ) ) { return; } // hack to load GF Coupons data.php file if( is_callable( 'gf_coupons' ) ) { gf_coupons()->get_config( array( 'id' => 0 ), false ); } else { GFCoupons::get_config( array( 'id' => 0 ), false ); } $meta = wp_parse_args( $this->_args['meta'], array( 'form_id' => false, 'coupon_name' => $coupon_code, 'coupon_code' => strtoupper( $coupon_code ), 'coupon_type' => $type, // 'flat', 'percentage' 'coupon_amount' => $amount, 'coupon_start' => '', // MM/DD/YYYY 'coupon_expiration' => '', // MM/DD/YYYY 'coupon_limit' => false, 'coupon_stackable' => false ) ); $form_id = $meta['form_id'] ? $meta['form_id'] : 0; unset( $meta['form_id'] ); foreach ( $meta as $key => $value ) { if ( $value instanceof Closure && is_callable( $value ) ) { $meta[ $key ] = call_user_func( $value, $entry, $form, $this ); } } if( is_callable( 'gf_coupons' ) ) { $meta['gravityForm'] = $form_id ? $form_id : 0; $meta['couponName'] = $meta['coupon_name']; $meta['couponCode'] = $meta['coupon_code']; $meta['couponAmountType'] = $meta['coupon_type']; $meta['couponAmount'] = $meta['coupon_amount']; $meta['startDate'] = $meta['coupon_start']; $meta['endDate'] = $meta['coupon_expiration']; $meta['usageLimit'] = $meta['coupon_limit']; $meta['isStackable'] = $meta['coupon_stackable']; $meta['usageCount'] = 0; gf_coupons()->insert_feed( $form_id, true, $meta ); } else { GFCouponsData::update_feed( 0, $form_id, true, $meta ); } } /** * Create a WooCommerce coupon. * * @link https://gist.github.com/mikejolley/3969579#file-gistfile1-txt */ public function create_coupon_wc( $coupon_code, $amount, $type, $entry, $form ) { $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' => '' ) ); 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( // ID of the form which will be used to create coupons 'form_id' => 1, // ID of the field whose value will be used as the coupon code 'source_field_id' => 3, // which plugin the coupon should be created for (i.e. WooCommerce = 'wc') 'plugin' => 'wc', // accepts: 'gf', 'wc', 'edd' // type of coupon code to be created, available types will differ depending on the plugin 'type' => 'percent', // amount of the coupon discount 'amount' => 10 ) );

Copy Code to Clipboard Raw

Sign-in or Create a Free Account.

Snippi uses Twitter or Facebook to register and create accounts. Registration will enable you to keep track of your snippets. We never spam your timeline or wall. Ever.

Sign-in with Facebook Sign-in with Twitter