2011-08-10 17:11:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce coupons
|
|
|
|
*
|
|
|
|
* The WooCommerce coupons class gets coupon data from storage
|
|
|
|
*
|
|
|
|
* @class woocommerce_coupons
|
|
|
|
* @package WooCommerce
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class woocommerce_coupons {
|
|
|
|
|
|
|
|
/** get coupon with $code */
|
2011-08-15 16:48:24 +00:00
|
|
|
function get_coupon( $code ) {
|
|
|
|
|
|
|
|
$coupon = get_page_by_title( $code, 'OBJECT', 'shop_coupon' );
|
|
|
|
|
|
|
|
if ($coupon && $coupon->post_status == 'publish') :
|
|
|
|
|
|
|
|
$type = get_post_meta($coupon->ID, 'discount_type', true);
|
|
|
|
$amount = get_post_meta($coupon->ID, 'coupon_amount', true);
|
|
|
|
$individual_use = get_post_meta($coupon->ID, 'individual_use', true);
|
|
|
|
$product_ids = array_map('trim', explode(',', get_post_meta($coupon->ID, 'product_ids', true)));
|
|
|
|
$usage_limit = get_post_meta($coupon->ID, 'usage_limit', true);
|
|
|
|
$usage_count = (int) get_post_meta($coupon->ID, 'usage_count', true);
|
|
|
|
|
|
|
|
if (!$amount) return false;
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'id' => $coupon->ID,
|
|
|
|
'code' => $code,
|
|
|
|
'type' => $type,
|
|
|
|
'amount' => $amount,
|
|
|
|
'individual_use' => $individual_use,
|
|
|
|
'products' => $product_ids,
|
|
|
|
'usage_limit' => $usage_limit,
|
|
|
|
'usage_count' => $usage_count
|
|
|
|
);
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-15 16:48:24 +00:00
|
|
|
/** Increase usage count */
|
|
|
|
function inc_usage_count( $code ) {
|
|
|
|
$coupon = get_page_by_title( $code, 'OBJECT', 'shop_coupon' );
|
|
|
|
if ($coupon) :
|
|
|
|
$usage_count = (int) get_post_meta($coupon->ID, 'usage_count', true);
|
|
|
|
$usage_count++;
|
|
|
|
update_post_meta($coupon->ID, 'usage_count', $usage_count);
|
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check coupon is valid */
|
2011-08-10 17:11:11 +00:00
|
|
|
function is_valid($code) {
|
|
|
|
$coupon = self::get_coupon($code);
|
2011-08-15 16:48:24 +00:00
|
|
|
|
|
|
|
if ($coupon) :
|
|
|
|
|
|
|
|
if (sizeof($coupon['products'])>0) :
|
|
|
|
$valid = false;
|
|
|
|
if (sizeof(woocommerce_cart::$cart_contents)>0) : foreach (woocommerce_cart::$cart_contents as $item_id => $values) :
|
|
|
|
if (in_array($item_id, $coupon['products'])) :
|
|
|
|
$valid = true;
|
|
|
|
endif;
|
|
|
|
endforeach; endif;
|
|
|
|
if (!$valid) return false;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
if ($coupon['usage_limit']>0) :
|
|
|
|
if ($coupon['usage_count']>$coupon['usage_limit']) :
|
|
|
|
return false;
|
2011-08-10 17:11:11 +00:00
|
|
|
endif;
|
2011-08-15 16:48:24 +00:00
|
|
|
endif;
|
|
|
|
|
|
|
|
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
|
|
|
}
|