Fixed product id check and improved write panel

This commit is contained in:
Mike Jolley 2011-10-03 12:21:01 +01:00
parent a9d46213ba
commit 3d325f676e
4 changed files with 21 additions and 48 deletions

View File

@ -26,60 +26,27 @@ function woocommerce_coupon_data_meta_box($post) {
<?php
// Type
$value = get_post_meta($post->ID, 'discount_type', true);
$field = array( 'id' => 'discount_type', 'label' => __('Discount type', 'woothemes') );
echo '<p class="form-field"><label for="'.$field['id'].'">'.$field['label'].':</label><select name="'.$field['id'].'" id="'.$field['id'].'">';
$discount_types = apply_filters('woocommerce_coupon_discount_types', array(
'fixed_cart' => __('Cart Discount', 'woothemes'),
'percent' => __('Cart % Discount', 'woothemes'),
'fixed_product' => __('Product Discount', 'woothemes')
));
foreach ($discount_types as $type => $label) :
echo '<option value="'.$type.'" ';
selected($type, $value);
echo '>'.$label.'</option>';
endforeach;
echo '</select></p>';
woocommerce_wp_select( array( 'id' => 'discount_type', 'label' => __('Discount type', 'woothemes'), 'options' => $discount_types ) );
// Amount
$value = get_post_meta($post->ID, 'coupon_amount', true);
$field = array( 'id' => 'coupon_amount', 'label' => __('Coupon amount', 'woothemes') );
echo '<p class="form-field">
<label for="'.$field['id'].'">'.$field['label'].':</label>
<input type="text" class="short" name="'.$field['id'].'" id="'.$field['id'].'" value="'.esc_attr( $value ).'" /> <span class="description">' . __('Enter an amount e.g. 2.99 or an integer for percentages e.g. 20', 'woothemes') . '</span></p>';
woocommerce_wp_text_input( array( 'id' => 'coupon_amount', 'label' => __('Coupon amount', 'woothemes'), 'placeholder' => __('0.00', 'woothemes'), 'description' => __('Enter an amount e.g. 2.99 or an integer for percentages e.g. 20%', 'woothemes') ) );
// Individual use
$value = get_post_meta($post->ID, 'individual_use', true);
$field = array( 'id' => 'individual_use', 'label' => __('Individual use', 'woothemes') );
echo '<p class="form-field"><label for="'.$field['id'].'">'.$field['label'].':</label>';
echo '<input type="checkbox" class="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ';
checked($value, 1);
echo ' /> <span class="description">' . __('Check this box if the coupon cannot be used in conjuction with other coupons', 'woothemes');
echo '</span></p>';
woocommerce_wp_checkbox( array( 'id' => 'individual_use', 'label' => __('Individual use', 'woothemes'), 'description' => __('Check this box if the coupon cannot be used in conjunction with other coupons', 'woothemes') ) );
// Product ids
$value = get_post_meta($post->ID, 'product_ids', true);
$field = array( 'id' => 'product_ids', 'label' => __('Product IDs', 'woothemes') );
echo '<p class="form-field">
<label for="'.$field['id'].'">'.$field['label'].':</label>
<input type="text" class="short" name="'.$field['id'].'" id="'.$field['id'].'" value="'.esc_attr( $value ).'" /> <span class="description">' . __('(optional) Comma separate product IDs which are required for this coupon to work', 'woothemes') . '</span></p>';
woocommerce_wp_text_input( array( 'id' => 'product_ids', 'label' => __('Product IDs', 'woothemes'), 'placeholder' => __('N/A', 'woothemes'), 'description' => __('(optional) Comma separate product IDs which need to be in the cart to use this coupon, or for "Product Discounts" are discounted.', 'woothemes') ) );
// Usage limit
$value = get_post_meta($post->ID, 'usage_limit', true);
$field = array( 'id' => 'usage_limit', 'label' => __('Usage limit', 'woothemes') );
echo '<p class="form-field">
<label for="'.$field['id'].'">'.$field['label'].':</label>
<input type="text" class="short" name="'.$field['id'].'" id="'.$field['id'].'" value="'.esc_attr( $value ).'" /> <span class="description">' . __('(optional) How many times this coupon can be used before it is void', 'woothemes') . '</span></p>';
woocommerce_wp_text_input( array( 'id' => 'usage_limit', 'label' => __('Usage limit', 'woothemes'), 'placeholder' => __('Unlimited usage', 'woothemes'), 'description' => __('(optional) How many times this coupon can be used before it is void', 'woothemes') ) );
// Expiry date
$value = get_post_meta($post->ID, 'expiry_date', true);
$field = array( 'id' => 'expiry_date', 'label' => __('Expiry date', 'woothemes') );
echo '<p class="form-field">
<label for="'.$field['id'].'">'.$field['label'].':</label>
<input type="text" class="short date-picker" name="'.$field['id'].'" id="'.$field['id'].'" value="'.esc_attr( $value ).'" /> <span class="description">' . __('(optional) The date this coupon will expire, <code>YYYY-MM-DD</code>', 'woothemes') . '</span></p>';
woocommerce_wp_text_input( array( 'id' => 'expiry_date', 'label' => __('Expiry date', 'woothemes'), 'placeholder' => __('Never expire', 'woothemes'), 'description' => __('(optional) The date this coupon will expire, <code>YYYY-MM-DD</code>', 'woothemes'), 'class' => 'short date-picker' ) );
do_action('woocommerce_coupon_options');
@ -113,14 +80,15 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
$woocommerce_errors = array();
if (!$_POST['coupon_amount']) $woocommerce_errors[] = __('Coupon amount is required', 'woothemes');
if ($_POST['discount_type']=='fixed_product' && !$_POST['product_ids']) $woocommerce_errors[] = __('Product discount coupons require you to set "Product IDs" to work.', 'woothemes');
// Add/Replace data to array
$type = strip_tags(stripslashes( $_POST['discount_type'] ));
$amount = strip_tags(stripslashes( $_POST['coupon_amount'] ));
$product_ids = strip_tags(stripslashes( $_POST['product_ids'] ));
$usage_limit = (isset($_POST['usage_limit']) && $_POST['usage_limit']>0) ? (int) $_POST['usage_limit'] : '';
$individual_use = isset($_POST['individual_use']) ? 1 : 0;
$expiry_date = strip_tags(stripslashes( $_POST['expiry_date'] ));
$individual_use = isset($_POST['individual_use']) ? 'yes' : 'no';
$expiry_date = strip_tags(stripslashes( $_POST['expiry_date'] ));
// Save
update_post_meta( $post_id, 'discount_type', $type );
@ -129,6 +97,8 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
update_post_meta( $post_id, 'product_ids', $product_ids );
update_post_meta( $post_id, 'usage_limit', $usage_limit );
update_post_meta( $post_id, 'expiry_date', $expiry_date );
do_action('woocommerce_coupon_options');
// Error Handling
if (sizeof($woocommerce_errors)>0) update_option('woocommerce_errors', $woocommerce_errors);

View File

@ -205,8 +205,9 @@ function woocommerce_meta_scripts() {
* Output write panel form elements
*/
function woocommerce_wp_text_input( $field ) {
global $thepostid;
global $thepostid, $post;
if (!$thepostid) $thepostid = $post->ID;
if (!isset($field['placeholder'])) $field['placeholder'] = '';
if (!isset($field['class'])) $field['class'] = 'short';
if (!isset($field['value'])) $field['value'] = get_post_meta($thepostid, $field['id'], true);
@ -219,8 +220,9 @@ function woocommerce_wp_text_input( $field ) {
}
function woocommerce_wp_checkbox( $field ) {
global $thepostid;
global $thepostid, $post;
if (!$thepostid) $thepostid = $post->ID;
if (!isset($field['class'])) $field['class'] = 'checkbox';
if (!isset($field['value'])) $field['value'] = get_post_meta($thepostid, $field['id'], true);
@ -236,8 +238,9 @@ function woocommerce_wp_checkbox( $field ) {
}
function woocommerce_wp_select( $field ) {
global $thepostid;
global $thepostid, $post;
if (!$thepostid) $thepostid = $post->ID;
if (!isset($field['class'])) $field['class'] = 'select short';
if (!isset($field['value'])) $field['value'] = get_post_meta($thepostid, $field['id'], true);

View File

@ -565,13 +565,13 @@ class woocommerce_cart {
endif;
// If its individual use then remove other coupons
if ($the_coupon->individual_use==1) :
if ($the_coupon->individual_use=='yes') :
$this->applied_coupons = array();
endif;
foreach ($this->applied_coupons as $code) :
$coupon = &new woocommerce_coupon($code);
if ($coupon->individual_use==1) :
if ($coupon->individual_use=='yes') :
$this->applied_coupons = array();
endif;
endforeach;

View File

@ -63,8 +63,8 @@ class woocommerce_coupon {
if (sizeof( $this->product_ids )>0) :
$valid = false;
if (sizeof($woocommerce->cart->cart_contents)>0) : foreach ($woocommerce->cart->cart_contents as $item_id => $values) :
if (in_array($item_id, $this->product_ids)) :
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)) :
$valid = true;
endif;
endforeach; endif;