2011-08-10 17:11:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce coupons
|
|
|
|
*
|
|
|
|
* The WooCommerce coupons class gets coupon data from storage
|
|
|
|
*
|
2012-01-27 16:38:39 +00:00
|
|
|
* @class WC_Coupon
|
2011-08-10 17:11:11 +00:00
|
|
|
* @package WooCommerce
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2012-01-27 16:38:39 +00:00
|
|
|
class WC_Coupon {
|
2011-09-06 11:11:22 +00:00
|
|
|
|
|
|
|
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-11-19 20:59:16 +00:00
|
|
|
var $apply_before_tax;
|
2011-11-28 16:10:31 +00:00
|
|
|
var $free_shipping;
|
2011-08-10 17:11:11 +00:00
|
|
|
|
|
|
|
/** get coupon with $code */
|
2012-01-27 16:38:39 +00:00
|
|
|
function __construct( $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-12-23 19:20:27 +00:00
|
|
|
$coupon_data = apply_filters('woocommerce_get_shop_coupon_data', false, $code);
|
|
|
|
|
2012-01-02 12:48:56 +00:00
|
|
|
if ($coupon_data) :
|
|
|
|
$this->id = $coupon_data['id'];
|
|
|
|
$this->type = $coupon_data['type'];
|
|
|
|
$this->amount = $coupon_data['amount'];
|
|
|
|
$this->individual_use = $coupon_data['individual_use'];
|
|
|
|
$this->product_ids = $coupon_data['product_ids'];
|
|
|
|
$this->exclude_product_ids = $coupon_data['exclude_product_ids'];
|
|
|
|
$this->usage_limit = $coupon_data['usage_limit'];
|
|
|
|
$this->usage_count = $coupon_data['usage_count'];
|
|
|
|
$this->expiry_date = $coupon_data['expiry_date'];
|
|
|
|
$this->apply_before_tax = $coupon_data['apply_before_tax'];
|
|
|
|
$this->free_shipping = $coupon_data['free_shipping'];
|
|
|
|
return true;
|
|
|
|
else:
|
|
|
|
$coupon = get_page_by_title($this->code, 'OBJECT', 'shop_coupon');
|
|
|
|
if ($coupon && $coupon->post_status == 'publish') :
|
|
|
|
$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_filter(array_map('trim', explode(',', get_post_meta($coupon->ID, 'product_ids', true))));
|
|
|
|
$this->exclude_product_ids = array_filter(array_map('trim', explode(',', get_post_meta($coupon->ID, 'exclude_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);
|
|
|
|
$this->expiry_date = ($expires = get_post_meta($coupon->ID, 'expiry_date', true)) ? strtotime($expires) : '';
|
|
|
|
$this->apply_before_tax = get_post_meta($coupon->ID, 'apply_before_tax', true);
|
|
|
|
$this->free_shipping = get_post_meta($coupon->ID, 'free_shipping', true);
|
|
|
|
return true;
|
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
return false;
|
2011-08-10 17:11:11 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 20:59:16 +00:00
|
|
|
/** Check if coupon needs applying before tax **/
|
|
|
|
function apply_before_tax() {
|
|
|
|
if ($this->apply_before_tax=='yes') return true; else return false;
|
|
|
|
}
|
|
|
|
|
2011-11-28 16:10:31 +00:00
|
|
|
function enable_free_shipping() {
|
|
|
|
if ($this->free_shipping=='yes') return true; else 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) :
|
2012-01-17 15:20:04 +00:00
|
|
|
|
|
|
|
$valid = true;
|
2011-11-01 16:25:54 +00:00
|
|
|
|
2012-01-17 15:20:04 +00:00
|
|
|
// Usage Limit
|
2011-11-15 22:20:59 +00:00
|
|
|
if ($this->usage_limit>0) :
|
2011-12-07 16:22:48 +00:00
|
|
|
if ($this->usage_count>=$this->usage_limit) :
|
2012-01-17 15:20:04 +00:00
|
|
|
$valid = false;
|
2011-11-15 22:20:59 +00:00
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
2012-01-17 15:20:04 +00:00
|
|
|
// Expired
|
2011-11-15 22:20:59 +00:00
|
|
|
if ($this->expiry_date) :
|
|
|
|
if (strtotime('NOW')>$this->expiry_date) :
|
2012-01-17 15:20:04 +00:00
|
|
|
$valid = false;
|
2011-11-15 22:20:59 +00:00
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
// Product ids - If a product included is found in the cart then its valid
|
2011-09-06 11:11:22 +00:00
|
|
|
if (sizeof( $this->product_ids )>0) :
|
2012-01-17 15:20:04 +00:00
|
|
|
$valid_for_cart = false;
|
2011-11-06 13:45:18 +00:00
|
|
|
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
|
2011-10-21 23:01:33 +00:00
|
|
|
if (in_array($cart_item['product_id'], $this->product_ids) || in_array($cart_item['variation_id'], $this->product_ids)) :
|
2012-01-17 15:20:04 +00:00
|
|
|
$valid_for_cart = true;
|
2011-08-15 16:48:24 +00:00
|
|
|
endif;
|
|
|
|
endforeach; endif;
|
2012-01-17 15:20:04 +00:00
|
|
|
if (!$valid_for_cart) $valid = false;
|
2011-08-15 16:48:24 +00:00
|
|
|
endif;
|
|
|
|
|
2011-11-15 22:20:59 +00:00
|
|
|
// Cart discounts cannot be added if non-eligble product is found in cart
|
|
|
|
if ($this->type!='fixed_product' && $this->type!='percent_product') :
|
|
|
|
|
|
|
|
if (sizeof( $this->exclude_product_ids )>0) :
|
2012-01-17 15:20:04 +00:00
|
|
|
$valid_for_cart = true;
|
2011-11-15 22:20:59 +00:00
|
|
|
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
|
|
|
|
if (in_array($cart_item['product_id'], $this->exclude_product_ids) || in_array($cart_item['variation_id'], $this->exclude_product_ids)) :
|
2012-01-17 15:20:04 +00:00
|
|
|
$valid_for_cart = false;
|
2011-11-15 22:20:59 +00:00
|
|
|
endif;
|
|
|
|
endforeach; endif;
|
2012-01-17 15:20:04 +00:00
|
|
|
if (!$valid_for_cart) $valid = false;
|
2011-08-10 17:11:11 +00:00
|
|
|
endif;
|
2011-08-15 16:48:24 +00:00
|
|
|
|
2011-09-20 15:05:07 +00:00
|
|
|
endif;
|
|
|
|
|
2012-01-17 15:20:04 +00:00
|
|
|
$valid = apply_filters('woocommerce_coupon_is_valid', $valid, $this);
|
2011-10-04 09:59:13 +00:00
|
|
|
|
2012-01-17 15:20:04 +00:00
|
|
|
if ($valid) return true;
|
2011-08-15 16:48:24 +00:00
|
|
|
|
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
|
|
|
}
|
2012-01-27 16:38:39 +00:00
|
|
|
|
|
|
|
/** Depreciated */
|
|
|
|
class woocommerce_coupon extends WC_Coupon {
|
|
|
|
public function __construct( $code ) {
|
|
|
|
_deprecated_function( 'woocommerce_coupon', '1.4', 'WC_Coupon()' );
|
|
|
|
parent::__construct( $code );
|
|
|
|
}
|
|
|
|
}
|