woocommerce/classes/class-wc-coupon.php

324 lines
10 KiB
PHP
Raw Normal View History

2011-08-10 17:11:11 +00:00
<?php
/**
* WooCommerce coupons
2012-08-14 19:42:38 +00:00
*
2012-01-30 19:24:52 +00:00
* The WooCommerce coupons class gets coupon data from storage and checks coupon validity
2011-08-10 17:11:11 +00:00
*
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 {
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var string Coupon code. */
var $code;
2012-08-15 17:08:42 +00:00
/** @var int Coupon ID. */
var $id;
2012-08-15 17:08:42 +00:00
/** @var string Type of discount. */
var $type;
2012-08-15 17:08:42 +00:00
/** @var string Type of discount (alias). */
var $discount_type;
/** @var string Coupon amount. */
var $amount;
2012-08-15 17:08:42 +00:00
/** @var string "Yes" if for individual use. */
var $individual_use;
2012-08-15 17:08:42 +00:00
/** @var array Array of product IDs. */
var $product_ids;
2012-08-15 17:08:42 +00:00
/** @var int Coupon usage limit. */
var $usage_limit;
2012-08-15 17:08:42 +00:00
/** @var int Coupon usage count. */
var $usage_count;
2012-08-15 17:08:42 +00:00
/** @var string Expirey date. */
2011-09-20 15:05:07 +00:00
var $expiry_date;
2012-08-15 17:08:42 +00:00
/** @var string "yes" if applied before tax. */
var $apply_before_tax;
2012-08-15 17:08:42 +00:00
/** @var string "yes" if coupon grants free shipping. */
2011-11-28 16:10:31 +00:00
var $free_shipping;
2012-08-15 17:08:42 +00:00
/** @var array Array of category ids. */
var $product_categories;
2012-08-15 17:08:42 +00:00
/** @var array Array of category ids. */
var $exclude_product_categories;
2012-08-15 17:08:42 +00:00
/** @var string Minimum cart amount. */
2012-03-07 21:38:34 +00:00
var $minimum_amount;
2012-08-15 17:08:42 +00:00
/** @var string Coupon owner's email. */
var $customer_email;
2012-08-15 17:08:42 +00:00
/** @var array Post meta. */
var $coupon_custom_fields;
2012-08-15 17:08:42 +00:00
/** @var string How much the coupon is worth. */
var $coupon_amount;
2012-08-14 19:42:38 +00:00
/**
2012-08-15 17:08:42 +00:00
* Coupon constructor. Loads coupon data.
2012-08-14 19:42:38 +00:00
*
* @access public
* @param mixed $code code of the coupon to load
* @return bool whether or not the coupon was found
*/
2012-01-27 16:38:39 +00:00
function __construct( $code ) {
2012-02-08 15:55:02 +00:00
global $wpdb;
2012-08-14 19:42:38 +00:00
2012-02-08 15:55:02 +00:00
$this->code = esc_attr($code);
2012-08-14 19:42:38 +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'];
$this->product_categories = $coupon_data['product_categories'];
$this->exclude_product_categories = $coupon_data['exclude_product_categories'];
2012-03-07 21:38:34 +00:00
$this->minimum_amount = $coupon_data['minimum_amount'];
$this->customer_email = $coupon_data['customer_email'];
2012-01-02 12:48:56 +00:00
return true;
else:
2012-02-08 15:55:02 +00:00
$coupon_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE BINARY post_title = %s AND post_type= %s", $this->code, 'shop_coupon' ) );
if ( $coupon_id ) $coupon = get_page($coupon_id); else return false;
2012-08-14 19:42:38 +00:00
2012-02-08 15:55:02 +00:00
// Check titles match
if ($this->code!==$coupon->post_title) return false;
2012-08-14 19:42:38 +00:00
2012-01-02 12:48:56 +00:00
if ($coupon && $coupon->post_status == 'publish') :
$this->id = $coupon->ID;
$this->coupon_custom_fields = get_post_custom( $this->id );
2012-08-14 19:42:38 +00:00
$load_data = array(
'discount_type' => 'fixed_cart',
'coupon_amount' => 0,
'individual_use' => 'no',
'product_ids' => '',
'exclude_product_ids' => '',
'usage_limit' => '',
'usage_count' => '',
'expiry_date' => '',
'apply_before_tax' => 'yes',
'free_shipping' => 'no',
'product_categories' => array(),
'exclude_product_categories' => array(),
'minimum_amount' => '',
'customer_email' => array()
);
2012-08-14 19:42:38 +00:00
foreach ($load_data as $key => $default) $this->$key = (isset($this->coupon_custom_fields[$key][0]) && $this->coupon_custom_fields[$key][0]!=='') ? $this->coupon_custom_fields[$key][0] : $default;
// Alias
$this->type = $this->discount_type;
$this->amount = $this->coupon_amount;
2012-08-14 19:42:38 +00:00
// Formatting
$this->product_ids = array_filter(array_map('trim', explode(',', $this->product_ids)));
$this->exclude_product_ids = array_filter(array_map('trim', explode(',', $this->exclude_product_ids)));
$this->expiry_date = ($this->expiry_date) ? strtotime($this->expiry_date) : '';
$this->product_categories = array_filter(array_map('trim', (array) maybe_unserialize($this->product_categories)));
$this->exclude_product_categories = array_filter(array_map('trim', (array) maybe_unserialize($this->exclude_product_categories)));
2012-03-20 18:36:25 +00:00
$this->customer_email = array_filter(array_map('trim', array_map('strtolower', (array) maybe_unserialize($this->customer_email))));
2012-01-02 12:48:56 +00:00
return true;
endif;
endif;
return false;
2011-08-10 17:11:11 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Check if coupon needs applying before tax.
*
* @access public
* @return bool
*/
function apply_before_tax() {
2012-08-14 19:42:38 +00:00
return ($this->apply_before_tax=='yes') ? true : false;
}
2012-08-14 19:42:38 +00:00
/**
* Check if a coupon enables free shipping.
*
* @access public
* @return void
*/
2011-11-28 16:10:31 +00:00
function enable_free_shipping() {
2012-08-14 19:42:38 +00:00
return ($this->free_shipping=='yes') ? true : false;
2011-11-28 16:10:31 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Increase usage count fo current coupon.
*
* @access public
* @return void
*/
function inc_usage_count() {
$this->usage_count++;
update_post_meta( $this->id, 'usage_count', $this->usage_count );
}
2012-08-14 19:42:38 +00:00
/**
* Decrease usage count fo current coupon.
*
* @access public
* @return void
*/
function dcr_usage_count() {
$this->usage_count--;
update_post_meta( $this->id, 'usage_count', $this->usage_count );
2011-08-15 16:48:24 +00:00
}
2012-08-14 19:42:38 +00:00
2012-06-10 18:07:19 +00:00
/**
* is_valid function.
*
* Check if a coupon is valid. Return a reason code if invaid. Reason codes:
2012-08-14 19:42:38 +00:00
*
2012-06-10 18:07:19 +00:00
* @access public
2012-08-14 19:42:38 +00:00
* @return bool|WP_Error validity or a WP_Error if not valid
2012-06-10 18:07:19 +00:00
*/
2011-09-20 15:05:07 +00:00
function is_valid() {
global $woocommerce;
2012-08-14 19:42:38 +00:00
2012-07-31 12:28:03 +00:00
if ( $this->id ) {
2012-08-14 19:42:38 +00:00
2012-01-17 15:20:04 +00:00
$valid = true;
2012-06-10 18:07:19 +00:00
$error = false;
2012-08-14 19:42:38 +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;
2012-06-10 18:07:19 +00:00
$error = __( 'Coupon usage limit has been reached.', 'woocommerce' );
2011-11-15 22:20:59 +00:00
endif;
endif;
2012-08-14 19:42:38 +00:00
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;
2012-06-10 18:07:19 +00:00
$error = __( 'This coupon has expired.', 'woocommerce' );
2011-11-15 22:20:59 +00:00
endif;
endif;
2012-08-14 19:42:38 +00:00
2012-03-07 21:38:34 +00:00
// Minimum spend
if ($this->minimum_amount>0) :
2012-06-01 18:01:44 +00:00
if ( $this->minimum_amount > $woocommerce->cart->subtotal ) :
2012-03-07 21:38:34 +00:00
$valid = false;
2012-09-06 16:47:05 +00:00
$error = sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), woocommerce_price( $this->minimum_amount ) );
2012-03-07 21:38:34 +00:00
endif;
endif;
2012-08-14 19:42:38 +00:00
2011-11-15 22:20:59 +00:00
// Product ids - If a product included is found in the cart then its valid
if (sizeof( $this->product_ids )>0) :
2012-01-17 15:20:04 +00:00
$valid_for_cart = false;
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->product_ids) || in_array($cart_item['variation_id'], $this->product_ids) || in_array($cart_item['data']->get_parent(), $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-08-31 08:45:50 +00:00
if ( ! $valid_for_cart ) {
$valid = false;
2012-08-31 08:47:57 +00:00
$error = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
2012-08-31 08:45:50 +00:00
}
2011-08-15 16:48:24 +00:00
endif;
2012-08-14 19:42:38 +00:00
// Category ids - If a product included is found in the cart then its valid
if (sizeof( $this->product_categories )>0) :
$valid_for_cart = false;
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
2012-08-14 19:42:38 +00:00
$product_cats = wp_get_post_terms($cart_item['product_id'], 'product_cat', array("fields" => "ids"));
2012-08-14 19:42:38 +00:00
if ( sizeof( array_intersect( $product_cats, $this->product_categories ) ) > 0 ) $valid_for_cart = true;
2012-08-14 19:42:38 +00:00
endforeach; endif;
2012-08-31 08:45:50 +00:00
if ( ! $valid_for_cart ) {
$valid = false;
2012-08-31 08:47:57 +00:00
$error = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
2012-08-31 08:45:50 +00:00
}
endif;
2012-08-14 19:42:38 +00:00
2011-11-15 22:20:59 +00:00
// Cart discounts cannot be added if non-eligble product is found in cart
2012-08-14 19:42:38 +00:00
if ($this->type!='fixed_product' && $this->type!='percent_product') :
// Exclude Products
2011-11-15 22:20:59 +00:00
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) || in_array($cart_item['data']->get_parent(), $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-08-31 08:45:50 +00:00
if ( ! $valid_for_cart ) {
$valid = false;
2012-08-31 08:47:57 +00:00
$error = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
2012-08-31 08:45:50 +00:00
}
2011-08-10 17:11:11 +00:00
endif;
2012-08-14 19:42:38 +00:00
// Exclude Categories
if (sizeof( $this->exclude_product_categories )>0) :
$valid_for_cart = true;
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
2012-08-14 19:42:38 +00:00
$product_cats = wp_get_post_terms($cart_item['product_id'], 'product_cat', array("fields" => "ids"));
2012-08-14 19:42:38 +00:00
if ( sizeof( array_intersect( $product_cats, $this->exclude_product_categories ) ) > 0 ) $valid_for_cart = false;
endforeach; endif;
2012-08-31 08:45:50 +00:00
if ( ! $valid_for_cart ) {
$valid = false;
2012-08-31 08:47:57 +00:00
$error = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
2012-08-31 08:45:50 +00:00
}
endif;
2012-08-14 19:42:38 +00:00
2011-09-20 15:05:07 +00:00
endif;
2012-08-14 19:42:38 +00:00
2012-06-10 18:07:19 +00:00
$valid = apply_filters( 'woocommerce_coupon_is_valid', $valid, $this );
2012-08-14 19:42:38 +00:00
if ( $valid )
return true;
2012-08-14 19:42:38 +00:00
2012-07-31 12:28:03 +00:00
} else {
$error = __( 'Invalid coupon', 'woocommerce' );
}
2012-08-14 19:42:38 +00:00
2012-06-10 18:07:19 +00:00
return new WP_Error( 'coupon_error', apply_filters( 'woocommerce_coupon_error', $error, $this ) );
2011-08-10 17:11:11 +00:00
}
2011-08-15 16:48:24 +00:00
}
2012-01-27 16:38:39 +00:00
2012-08-14 19:42:38 +00:00
/**
* woocommerce_coupon class.
*
2012-08-15 17:08:42 +00:00
* @extends WC_Coupon
* @deprecated 1.4
* @package WooCommerce/Classes
2012-08-14 19:42:38 +00:00
*/
2012-01-27 16:38:39 +00:00
class woocommerce_coupon extends WC_Coupon {
2012-08-14 19:42:38 +00:00
public function __construct( $code ) {
2012-01-27 16:38:39 +00:00
_deprecated_function( 'woocommerce_coupon', '1.4', 'WC_Coupon()' );
2012-08-14 19:42:38 +00:00
parent::__construct( $code );
}
2012-01-27 16:38:39 +00:00
}