Quantcast
Channel: Snippets Archives - Gravity Wiz
Viewing all articles
Browse latest Browse all 196

Creating Coupons for WooCommerce with Gravity Forms

$
0
0

Offering a coupon is a great way to incentivize users to fill out any form.

Want to provide a compelling incentive for visitors to sign up for your newsletter? Offer them a coupon code for a small discount at your shop.

Trying to spread the word about your online store? Offer customers a “Share this Product with a Friend” that will send that friend a discount to be used on their first purchase.

This snippet also supports creating coupons for Easy Digital Downloads and Gravity Forms. Keep an eye out for these awesome articles coming soon:

Getting Started

  1. Check requirements

  2. Install the snippet

    • Copy and paste the entire snippet into your theme’s functions.php file.
  3. Configure the snippet

    • The basic configuration only requires that you specify the which form should be used to generate coupons (form_id), which field’s value should be used as the coupon code (source_field_id), and the type and amount of the coupon.
    • See the Usage Examples and available Parameters below.

Usage Examples

WooCommerce Coupon with Flat Discount, Applied to Cart

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'fixed_cart'
) );

Creates a flat $10 discount that applies to the entire cart. Whenever form ID 608 is submitted, the value of field ID 1 is used to create a new coupon.


WooCommerce Coupon with Percentage Discount, Applied to Cart

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'percent'
) );

Creates a 10% discount that applies to the entire cart.


WooCommerce Coupon with Percentage Discount, Applied to Specific Product(s)

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'percent_product',
	'meta'            => array(
		'product_ids' => '123'
	)
) );

Creates a 10% discount that applies to only to product ID 123.


Stackable WooCommerce Coupon with Usage Limit and Expiration Date

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'fixed_cart',
	'meta'            => array(
		'individual_use' => 'no',
		'usage_limit'    => 5,
		'expiry_date'    => '12/31/2014'
	)
) );

Creates a flat $10 discount that applies to the entire cart. This coupon is can be used with other coupons (we set individual_use to 'no'). The coupon can be used up to 5 times (handled by the usage_limit) and will expire on December 31, 2014 (via the expiry_date).


WooCommerce Coupon with Name Set by Field Value

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'name_field_id'   => 20,
	'plugin'          => 'wc',
	'amount'          => 15,
	'type'            => 'fixed_cart', 
) );

Creates a 15% discount that applies to the entire cart. This coupon’s title is derived from the value in Field 20 (handled by name_field_id).


Parameters

Here is a full list of the available parameters and additional information on how each can be configured.

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'name_field_id'   => 20,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'fixed_cart', // accepts: 'fixed_cart', 'percent', 'fixed_product', 'percent_product'
	'meta'            => array(
		'apply_before_tax'           => 'no',
		'customer_email'             => '',
		'exclude_product_categories' => array(),
		'exclude_product_ids'        => '',
		'exclude_sale_items'         => 'no',
		'expiry_date'                => '',
		'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
	)
) );
  • form_id (integer) (required)

    The ID of the form which will be used to create coupons on submission.

  • source_field_id (integer) (required)

    The ID of the field whose value will be used as the coupon code.

  • name_field_id (integer) (optional)

    The ID of the field whose value will be used as the coupon title.

  • plugin (string) (required)

    The plugin for which you would like to generated a coupon. WooCommerce ('wc'), Easy Digital Downloads ('edd'), and Gravity Forms ('gf') are currently supported.

  • amount (integer|float) (required)

    The amount the generated coupon should discount.

  • type (string) (required)

    The type of coupon. Supported values are:

    'fixed_cart'Applies a flat discount to the entire cart.
    'percent'Applies a percentage discount to the entire cart.
    'fixed_product'Applies a flat discount to a specific product(s).
    'percent_product'Applies a percentage discount to a specific product(s).
  • meta (array) (optional)

    An array of additional options that can be used to customize the generated coupon.

    individual_useSet to 'yes' if the coupon cannot be used in conjunction with other coupons.
    product_idsA comma-delimited list of products (by ID) which need to be in the cart to use this coupon or, for “Product Discounts”, which products are discounted.
    exclude_product_idsA comma-delimited list of products (by ID) which must not be in the cart to use this coupon or, for “Product Discounts”, which products are not discounted.
    usage_limitSet how many times this coupon can be used before it is void. Default value is 1. Set to '' for unlimited usage.
    expiry_dateSpecify the date when the coupon expires. Format: 'YYYY-MM-DD'. Example: 2014-09-30
    apply_before_taxSet to 'yes' if the coupon should be applied before calculating cart tax.
    free_shippingSet to 'yes' if the coupon grants free shipping. The free shipping method must be enabled with the “must use coupon” setting.
    exclude_sale_itemsSet to 'yes' if the coupon should not apply to items on sale.
    product_categoriesA product must be in this category (use category ID) for the coupon to remain valid or, for “Product Discounts”, products in these categories will be discounted.
    exclude_product_categoriesProduct must not be in this category (use category ID) for the coupon to remain valid or, for “Product Discounts”, products in these categories will not be discounted.
    minimum_amountSet the minimum subtotal needed to use the coupon.
    maximum_amountSet the maximum subtotal allowed when using the coupon.
    customer_emailSpecify a list of emails to check against the customer’s billing email when an order is placed. Separate email addresses with commas.
    limit_usage_to_x_itemsSpecify the maximum number of individual items this coupon can apply to when using product discounts. Leave blank to apply to all qualifying items in cart.
    limit_usage_per_userSpecify how many times this coupon can be used by an invidual user. Uses billing email for guests, and user ID for logged in users.

* Parameter descriptions are modified versions of the default help tooltips available in the WooCommerce coupon edit page.

How’d we do?

If you use it and like it, let us know. We’d love to hear the different ways you found this code useful!


Viewing all articles
Browse latest Browse all 196

Trending Articles