Store discount for products (pre-tax)

This commit is contained in:
Mike Jolley 2011-11-25 19:31:06 +00:00
parent 5fd566b47c
commit a55adaca3e
6 changed files with 59 additions and 24 deletions

View File

@ -168,7 +168,8 @@ function woocommerce_order_items_meta_box($post) {
<th class="meta" width="1%"><?php _e('Item&nbsp;Meta', 'woothemes'); ?></th>
<?php do_action('woocommerce_admin_order_item_headers'); ?>
<th class="quantity"><?php _e('Quantity', 'woothemes'); ?></th>
<th class="cost"><?php _e('Cost', 'woothemes'); ?></th>
<th class="cost"><?php _e('Cost', 'woothemes'); ?> <?php echo $woocommerce->countries->ex_tax_or_vat(); ?></th>
<th class="cost"><?php _e('Discount', 'woothemes'); ?></th>
<th class="tax"><?php _e('Tax Rate', 'woothemes'); ?></th>
<th class="center" width="1%"><?php _e('Remove', 'woothemes'); ?></th>
</tr>
@ -233,15 +234,19 @@ function woocommerce_order_items_meta_box($post) {
<?php do_action('woocommerce_admin_order_item_values', $_product, $item); ?>
<td class="quantity">
<input type="text" name="item_quantity[<?php echo $loop; ?>]" placeholder="<?php _e('Quantity e.g. 2', 'woothemes'); ?>" value="<?php echo esc_attr( $item['qty'] ); ?>" />
<input type="text" name="item_quantity[<?php echo $loop; ?>]" placeholder="<?php _e('0', 'woothemes'); ?>" value="<?php echo esc_attr( $item['qty'] ); ?>" />
</td>
<td class="cost">
<input type="text" name="item_cost[<?php echo $loop; ?>]" placeholder="<?php _e('Cost per unit ex. tax e.g. 2.99', 'woothemes'); ?>" value="<?php echo esc_attr( $item['cost'] ); ?>" />
<input type="text" name="item_cost[<?php echo $loop; ?>]" placeholder="<?php _e('0.00', 'woothemes'); ?>" value="<?php echo esc_attr( $item['cost'] ); ?>" />
</td>
<td class="discount">
<input type="text" name="item_cost[<?php echo $loop; ?>]" placeholder="<?php _e('0.00', 'woothemes'); ?>" value="<?php if (isset($item['discount'])) echo esc_attr( $item['discount'] ); ?>" />
</td>
<td class="tax">
<input type="text" name="item_tax_rate[<?php echo $loop; ?>]" placeholder="<?php _e('Tax Rate e.g. 20.0000', 'woothemes'); ?>" value="<?php echo esc_attr( $item['taxrate'] ); ?>" />
<input type="text" name="item_tax_rate[<?php echo $loop; ?>]" placeholder="<?php _e('0.0000', 'woothemes'); ?>" value="<?php echo esc_attr( $item['taxrate'] ); ?>" />
</td>
<td class="center">
@ -383,9 +388,9 @@ function woocommerce_order_totals_meta_box($post) {
</li>
<li class="left">
<label><?php _e('Product Discount:', 'woothemes'); ?></label>
<input type="text" id="_product_discount" name="_product_discount" placeholder="0.00" value="<?php
if (isset($data['_product_discount'][0])) echo $data['_product_discount'][0];
<label><?php _e('Cart Discount:', 'woothemes'); ?></label>
<input type="text" id="_cart_discount" name="_cart_discount" placeholder="0.00" value="<?php
if (isset($data['_cart_discount'][0])) echo $data['_cart_discount'][0];
?>" />
</li>

View File

@ -20,6 +20,21 @@ jQuery( function($){
});
// ORDERS
$('a.edit_address').click(function(){
$(this).hide();
$(this).closest('.order_data').find('div.address').hide();
$(this).closest('.order_data').find('div.edit_address').show();
});
// Chosen selects
jQuery("select.chosen_select").chosen();
jQuery("select.chosen_select_nostd").chosen({
allow_single_deselect: 'true'
});
$('#order_items_list button.remove_row').live('click', function(){
var answer = confirm(woocommerce_writepanel_params.remove_item_notice);
if (answer){

View File

@ -25,7 +25,7 @@ class woocommerce_cart {
var $subtotal;
var $subtotal_ex_tax;
var $tax_total;
var $discount_product;
var $discount_cart;
var $discount_total;
var $shipping_total;
var $shipping_tax_total;
@ -317,14 +317,14 @@ class woocommerce_cart {
$this->subtotal = 0;
$this->subtotal_ex_tax = 0;
$this->discount_total = 0;
$this->discount_product = 0;
$this->discount_cart = 0;
$this->shipping_total = 0;
}
/**
* Function to apply discounts to a product and get the discounted price (before tax is applied)
*/
function get_discounted_price( $values, $price ) {
function get_discounted_price( $values, $price, $add_totals = false ) {
if ($this->applied_coupons) foreach ($this->applied_coupons as $code) :
$coupon = &new woocommerce_coupon( $code );
@ -378,13 +378,15 @@ class woocommerce_cart {
if ($price<0) $price = 0;
$this->discount_product = $this->discount_product + ( $discount_amount * $values['quantity'] );
if ($add_totals) :
$this->discount_cart = $this->discount_cart + ( $discount_amount * $values['quantity'] );
endif;
elseif ($coupon->type=='percent_product') :
$percent_discount = ( $values['data']->get_price_excluding_tax( false ) / 100 ) * $coupon->amount;
$this->discount_product = $this->discount_product + ( $percent_discount * $values['quantity'] );
if ($add_totals) $this->discount_cart = $this->discount_cart + ( $percent_discount * $values['quantity'] );
$price = $price - $percent_discount;
@ -433,7 +435,7 @@ class woocommerce_cart {
if ($price<0) $price = 0;
// Add coupon to discount total (once, since this is a fixed cart discount and we don't want rounding issues)
$this->discount_product = $this->discount_product + (($discount_amount*$values['quantity']) / 100);
if ($add_totals) $this->discount_cart = $this->discount_cart + (($discount_amount*$values['quantity']) / 100);
break;
@ -442,7 +444,7 @@ class woocommerce_cart {
// Get % off each item - this works out the same as doing the whole cart
$percent_discount = ( $values['data']->get_price_excluding_tax( false ) / 100 ) * $coupon->amount;
$this->discount_product = $this->discount_product + ( $percent_discount * $values['quantity'] );
if ($add_totals) $this->discount_cart = $this->discount_cart + ( $percent_discount * $values['quantity'] );
$price = $price - $percent_discount;
@ -642,7 +644,7 @@ class woocommerce_cart {
$base_price = $_product->get_price();
// Discounted Price (price with any pre-tax discounts applied)
$discounted_price = $this->get_discounted_price( $values, $base_price );
$discounted_price = $this->get_discounted_price( $values, $base_price, true );
// Base Price Adjustment
if ( get_option('woocommerce_calc_taxes')=='yes' && $_product->is_taxable() ) :
@ -723,7 +725,7 @@ class woocommerce_cart {
$base_price = $_product->get_price_excluding_tax();
// Discounted Price (base price with any pre-tax discounts applied
$discounted_price = $this->get_discounted_price( $values, $base_price );
$discounted_price = $this->get_discounted_price( $values, $base_price, true );
// Tax Amount (For the line, based on discounted, ex.tax price)
if ( get_option('woocommerce_calc_taxes')=='yes' && $_product->is_taxable() ) :
@ -796,10 +798,17 @@ class woocommerce_cart {
/**
* Get the total of all discounts
* Get the total of all order discounts (after tax discounts)
*/
function get_discount_total() {
return $this->discount_total + $this->discount_product;
function get_order_discount_total() {
return $this->discount_total;
}
/**
* Get the total of all cart discounts (before tax discounts)
*/
function get_cart_discount_total() {
return $this->discount_cart;
}
/**
@ -1133,8 +1142,8 @@ class woocommerce_cart {
* gets the total (product) discount amount - these are applied before tax
*/
function get_discounts_before_tax() {
if ($this->discount_product) :
return woocommerce_price($this->discount_product);
if ($this->discount_cart) :
return woocommerce_price($this->discount_cart);
endif;
return false;
}
@ -1154,8 +1163,8 @@ class woocommerce_cart {
* gets the total discount amount - both kinds
*/
function get_total_discount() {
if ($this->discount_total || $this->discount_product) :
return woocommerce_price($this->discount_total + $this->discount_product);
if ($this->discount_total || $this->discount_cart) :
return woocommerce_price($this->discount_total + $this->discount_cart);
endif;
return false;
}

View File

@ -683,6 +683,9 @@ class woocommerce_checkout {
'name' => $_product->get_title(),
'qty' => (int) $values['quantity'],
'cost' => $_product->get_price_excluding_tax( false ),
'discount' => number_format(
($_product->get_price() - $woocommerce->cart->get_discounted_price( $values, $_product->get_price() )), 2, '.', ''
),
'taxrate' => $rate,
'item_meta' => $item_meta->meta
), $values);
@ -745,7 +748,8 @@ class woocommerce_checkout {
update_post_meta( $order_id, '_payment_method', $this->posted['payment_method']);
update_post_meta( $order_id, '_order_subtotal', number_format($woocommerce->cart->subtotal_ex_tax, 2, '.', ''));
update_post_meta( $order_id, '_order_shipping', number_format($woocommerce->cart->shipping_total, 2, '.', ''));
update_post_meta( $order_id, '_order_discount', number_format($woocommerce->cart->get_discount_total(), 2, '.', ''));
update_post_meta( $order_id, '_order_discount', number_format($woocommerce->cart->get_order_discount_total(), 2, '.', ''));
update_post_meta( $order_id, '_cart_discount', number_format($woocommerce->cart->get_cart_discount_total(), 2, '.', ''));
update_post_meta( $order_id, '_order_tax', number_format($woocommerce->cart->tax_total, 2, '.', ''));
update_post_meta( $order_id, '_order_shipping_tax', number_format($woocommerce->cart->shipping_tax_total, 2, '.', ''));
update_post_meta( $order_id, '_order_total', number_format($woocommerce->cart->total, 2, '.', ''));

View File

@ -82,6 +82,7 @@ class woocommerce_order {
'payment_method' => '',
'order_subtotal' => '',
'order_discount' => '',
'cart_discount' => '',
'order_tax' => '',
'order_shipping' => '',
'order_shipping_tax' => '',

View File

@ -97,6 +97,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
* Enhanced admin with http://harvesthq.github.com/chosen/
* Added ZAR currency
* Fixed product (single) visibility
* Improved orders interface
= 1.2.4 - 18/11/2011 =
* More sale price logic fixes for variations. Now correctly compares variation's prices.