2011-08-10 17:11:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce coupons
|
|
|
|
*
|
|
|
|
* The WooCommerce coupons class gets coupon data from storage
|
|
|
|
*
|
2011-09-06 11:11:22 +00:00
|
|
|
* @class woocommerce_coupon
|
2011-08-10 17:11:11 +00:00
|
|
|
* @package WooCommerce
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2011-09-06 11:11:22 +00:00
|
|
|
class woocommerce_coupon {
|
|
|
|
|
|
|
|
var $code;
|
|
|
|
var $id;
|
|
|
|
var $type;
|
|
|
|
var $amount;
|
|
|
|
var $individual_use;
|
|
|
|
var $product_ids;
|
|
|
|
var $usage_limit;
|
|
|
|
var $usage_count;
|
2011-09-20 15:05:07 +00:00
|
|
|
var $expiry_date;
|
2011-08-10 17:11:11 +00:00
|
|
|
|
|
|
|
/** get coupon with $code */
|
2011-09-06 11:11:22 +00:00
|
|
|
function woocommerce_coupon( $code ) {
|
2011-08-15 16:48:24 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
$this->code = $code;
|
2011-08-15 16:48:24 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
$coupon = get_page_by_title( $this->code, 'OBJECT', 'shop_coupon' );
|
2011-08-15 16:48:24 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
if ($coupon && $coupon->post_status == 'publish') :
|
2011-08-15 16:48:24 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
$this->id = $coupon->ID;
|
|
|
|
$this->type = get_post_meta($coupon->ID, 'discount_type', true);
|
|
|
|
$this->amount = get_post_meta($coupon->ID, 'coupon_amount', true);
|
|
|
|
$this->individual_use = get_post_meta($coupon->ID, 'individual_use', true);
|
|
|
|
$this->product_ids = array_map('trim', explode(',', get_post_meta($coupon->ID, 'product_ids', true)));
|
|
|
|
$this->usage_limit = get_post_meta($coupon->ID, 'usage_limit', true);
|
|
|
|
$this->usage_count = (int) get_post_meta($coupon->ID, 'usage_count', true);
|
2011-09-20 15:05:07 +00:00
|
|
|
$this->expiry_date = ($expires = get_post_meta($coupon->ID, 'expiry_date', true)) ? strtotime($expires) : '';
|
2011-08-15 16:48:24 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
if (!$this->amount) return false;
|
|
|
|
|
|
|
|
return true;
|
2011-08-15 16:48:24 +00:00
|
|
|
|
|
|
|
endif;
|
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-15 16:48:24 +00:00
|
|
|
/** Increase usage count */
|
2011-09-06 11:11:22 +00:00
|
|
|
function inc_usage_count() {
|
|
|
|
$this->usage_count++;
|
|
|
|
update_post_meta($this->id, 'usage_count', $this->usage_count);
|
2011-08-15 16:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Check coupon is valid */
|
2011-09-20 15:05:07 +00:00
|
|
|
function is_valid() {
|
2011-08-15 16:48:24 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
global $woocommerce;
|
2011-10-04 09:59:13 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
if ($this->id) :
|
2011-08-15 16:48:24 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
if (sizeof( $this->product_ids )>0) :
|
2011-08-15 16:48:24 +00:00
|
|
|
$valid = false;
|
2011-10-03 11:21:01 +00:00
|
|
|
if (sizeof($woocommerce->cart->cart_contents)>0) : foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) :
|
|
|
|
if (in_array($cart_item['product_id'], $this->product_ids)) :
|
2011-08-15 16:48:24 +00:00
|
|
|
$valid = true;
|
|
|
|
endif;
|
|
|
|
endforeach; endif;
|
|
|
|
if (!$valid) return false;
|
|
|
|
endif;
|
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
if ($this->usage_limit>0) :
|
|
|
|
if ($this->usage_count>$this->usage_limit) :
|
2011-08-15 16:48:24 +00:00
|
|
|
return false;
|
2011-08-10 17:11:11 +00:00
|
|
|
endif;
|
2011-08-15 16:48:24 +00:00
|
|
|
endif;
|
|
|
|
|
2011-09-20 15:05:07 +00:00
|
|
|
if ($this->expiry_date) :
|
|
|
|
if (strtotime('NOW')>$this->expiry_date) :
|
|
|
|
return false;
|
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
2011-10-04 09:59:13 +00:00
|
|
|
$valid = apply_filters('woocommerce_coupon_is_valid', true, $this);
|
|
|
|
if (!$valid) return false;
|
|
|
|
|
2011-08-15 16:48:24 +00:00
|
|
|
return true;
|
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
endif;
|
2011-08-15 16:48:24 +00:00
|
|
|
|
|
|
|
return false;
|
2011-08-10 17:11:11 +00:00
|
|
|
}
|
2011-08-15 16:48:24 +00:00
|
|
|
}
|