根据Variable申请WooCommerce优惠券

时间:2014-08-28 19:18:40

标签: wordpress woocommerce

我在WooCommerce中设置了优惠券代码,名为COUPON100,其ID为200.

在结帐时,我想根据变量调整优惠券折扣金额。 e.g

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

1 个答案:

答案 0 :(得分:1)

未经测试,但以下过滤器应该有所帮助:

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;
}

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,
);