Scott
Scott

Reputation: 25

Apply WooCommerce coupon based on Variable

I have setup a Coupon code in WooCommerce, called COUPON100, it has an ID of 200.

During the checkout, I'd like to adjust the coupon discount amount depending on a variable. e.g

if ($username == 'John') && ($code == 'COUPON100'){
$amount = 50;
}
else if ($username == 'John') && ($code == 'COUPON100'){
$amount = 70;
else { $amount = 30}
}

Upvotes: 0

Views: 731

Answers (1)

brasofilo
brasofilo

Reputation: 26075

Untested, but the following filter should help:

add_filter( 'woocommerce_api_coupon_response', 'coupon_so_25556432', 10, 4 );

function coupon_so_25556432( $coupon_data, $coupon, $fields, $server )
{
    $user = wp_get_current_user();
    // modify coupon data
    return $coupon_data;
}

The parameter $coupon_data contains:

$coupon_data = array(
    'id'                           => $coupon->id,
    'code'                         => $coupon->code,
    'type'                         => $coupon->type,
    'created_at'                   => $this->server->format_datetime( $coupon_post->post_date_gmt ),
    'updated_at'                   => $this->server->format_datetime( $coupon_post->post_modified_gmt ),
    'amount'                       => wc_format_decimal( $coupon->amount, 2 ),
    'individual_use'               => ( 'yes' === $coupon->individual_use ),
    'product_ids'                  => array_map( 'absint', (array) $coupon->product_ids ),
    'exclude_product_ids'          => array_map( 'absint', (array) $coupon->exclude_product_ids ),
    'usage_limit'                  => ( ! empty( $coupon->usage_limit ) ) ? $coupon->usage_limit : null,
    'usage_limit_per_user'         => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null,
    'limit_usage_to_x_items'       => (int) $coupon->limit_usage_to_x_items,
    'usage_count'                  => (int) $coupon->usage_count,
    'expiry_date'                  => $this->server->format_datetime( $coupon->expiry_date ),
    'apply_before_tax'             => $coupon->apply_before_tax(),
    'enable_free_shipping'         => $coupon->enable_free_shipping(),
    'product_category_ids'         => array_map( 'absint', (array) $coupon->product_categories ),
    'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->exclude_product_categories ),
    'exclude_sale_items'           => $coupon->exclude_sale_items(),
    'minimum_amount'               => wc_format_decimal( $coupon->minimum_amount, 2 ),
    'customer_emails'              => $coupon->customer_email,
);

Upvotes: 1

Related Questions