customer coupons. Ability to refresh the totals through code.

This commit is contained in:
Mike Jolley 2012-03-14 11:16:26 +00:00
parent bf63208b4e
commit da113b7589
8 changed files with 110 additions and 18 deletions

View File

@ -134,6 +134,11 @@ function woocommerce_coupon_data_meta_box($post) {
echo '</div><div class="options_group">';
// Customers
woocommerce_wp_text_input( array( 'id' => 'customer_email', 'label' => __('Customer Emails', 'woocommerce'), 'placeholder' => __('Any customer', 'woocommerce'), 'description' => __('Comma separate email addresses to restrict this coupon to specific billing and user emails.', 'woocommerce'), 'value' => implode(', ', (array) get_post_meta( $post->ID, 'customer_email', true )) ) );
echo '</div><div class="options_group">';
// Usage limit
woocommerce_wp_text_input( array( 'id' => 'usage_limit', 'label' => __('Usage limit', 'woocommerce'), 'placeholder' => _x('Unlimited usage', 'placeholder', 'woocommerce'), 'description' => __('How many times this coupon can be used before it is void', 'woocommerce') ) );
@ -170,6 +175,7 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
$apply_before_tax = isset($_POST['apply_before_tax']) ? 'yes' : 'no';
$free_shipping = isset($_POST['free_shipping']) ? 'yes' : 'no';
$minimum_amount = strip_tags(stripslashes( $_POST['minimum_amount'] ));
$customer_email = array_filter(array_map('trim', explode(',', strip_tags(stripslashes( $_POST['customer_email'] )))));
if (isset($_POST['product_ids'])) {
$product_ids = (array) $_POST['product_ids'];
@ -201,6 +207,7 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
update_post_meta( $post_id, 'product_categories', $product_categories );
update_post_meta( $post_id, 'exclude_product_categories', $exclude_product_categories );
update_post_meta( $post_id, 'minimum_amount', $minimum_amount );
update_post_meta( $post_id, 'customer_email', $customer_email );
do_action('woocommerce_coupon_options');

View File

@ -657,8 +657,24 @@ jQuery(document).ready(function($) {
success: function( code ) {
$('.woocommerce_error, .woocommerce_message').remove();
try {
success = $.parseJSON( code );
window.location = decodeURI(success.redirect);
result = $.parseJSON( code );
if (result.result=='success') {
window.location = decodeURI(result.redirect);
} else if (result.result=='failure') {
$form.prepend( result.messages );
$form.removeClass('processing').unblock();
if (result.refresh=='true') $('body').trigger('update_checkout');
$('html, body').animate({
scrollTop: ($('form.checkout').offset().top - 100)
}, 1000);
}
}
catch(err) {
$form.prepend( code );

File diff suppressed because one or more lines are too long

View File

@ -55,6 +55,7 @@ class WC_Cart {
$this->get_cart_from_session();
add_action('woocommerce_check_cart_items', array(&$this, 'check_cart_items'), 1);
add_action('woocommerce_after_checkout_validation', array(&$this, 'check_customer_coupons'), 1);
}
/*-----------------------------------------------------------------------------------*/
@ -227,6 +228,34 @@ class WC_Cart {
if (is_wp_error($result)) $woocommerce->add_error( $result->get_error_message() );
}
/**
* Check for user coupons (now we have billing email)
**/
function check_customer_coupons( $posted ) {
global $woocommerce;
if (!empty($this->applied_coupons)) foreach ($this->applied_coupons as $key => $code) {
$coupon = new WC_Coupon( $code );
if (is_array($coupon->customer_email) && sizeof($coupon->customer_email)>0) {
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
//$check_emails[] = $current_user->user_email;
}
$check_emails[] = $posted['billing_email'];
if (!in_array($check_emails, $coupon->customer_email)) {
$woocommerce->add_error( sprintf(__('Sorry, it seems the coupon "%s" is not yours - it has now been removed from your order.', 'woocommerce'), $code) );
// Remove the coupon
unset( $this->applied_coupons[$key] );
$_SESSION['coupons'] = $this->applied_coupons;
$_SESSION['refresh_totals'] = true;
}
}
}
}
/**
* looks through the cart to check each item is in stock
*/

View File

@ -563,7 +563,20 @@ class WC_Checkout {
// If we reached this point then there were errors
if (is_ajax()) :
ob_start();
$woocommerce->show_messages();
$messages = ob_get_clean();
echo json_encode(
array(
'result' => 'failure',
'messages' => $messages,
'refresh' => (isset($_SESSION['refresh_totals'])) ? 'true' : 'false'
)
);
unset($_SESSION['refresh_totals']);
exit;
endif;
}

View File

@ -25,6 +25,10 @@ class WC_Coupon {
var $product_categories;
var $exclude_product_categories;
var $minimum_amount;
var $customer_email;
var $coupon_custom_fields;
var $discount_type;
var $coupon_amount;
/** get coupon with $code */
function __construct( $code ) {
@ -49,6 +53,7 @@ class WC_Coupon {
$this->product_categories = $coupon_data['product_categories'];
$this->exclude_product_categories = $coupon_data['exclude_product_categories'];
$this->minimum_amount = $coupon_data['minimum_amount'];
$this->customer_email = $coupon_data['customer_email'];
return true;
else:
$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' ) );
@ -56,21 +61,42 @@ class WC_Coupon {
// Check titles match
if ($this->code!==$coupon->post_title) return false;
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);
$this->product_categories = array_filter(array_map('trim', (array) get_post_meta($coupon->ID, 'product_categories', true)));
$this->exclude_product_categories = array_filter(array_map('trim', (array) get_post_meta($coupon->ID, 'exclude_product_categories', true)));
$this->minimum_amount = get_post_meta($coupon->ID, 'minimum_amount', true);
$this->id = $coupon->ID;
$this->coupon_custom_fields = get_post_custom( $this->id );
$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()
);
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;
// 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)));
$this->customer_email = array_filter(array_map('trim', (array) maybe_unserialize($this->customer_email)));
return true;
endif;
endif;

View File

@ -170,6 +170,8 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* variation menu_order for organisation (drag and drop)
* Prevent ms-files.php accessing protected downloads when multisite is enabled
* is visible for variations - means you can hide out of stock variations
* Customer email field for coupons - limit to user/billing email
* Optimised coupon class loading
= 1.5.1 - 08/03/2012 =
* Persistent (logged-in) customer carts (thanks dominic-p)

View File

@ -539,7 +539,6 @@ function woocommerce_pay_action() {
endif;
}
/**
* Process the login form
**/