This commit is contained in:
Mike Jolley 2012-11-12 13:41:54 +00:00
parent 1db8c6158f
commit a1ad595822
9 changed files with 186 additions and 75 deletions

View File

@ -0,0 +1,44 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>
<tr class="fee" data-order_item_id="<?php echo $item_id; ?>">
<td class="check-column"><input type="checkbox" /></td>
<td class="thumb"></td>
<td class="name">
<input type="text" placeholder="<?php _e( 'Fee Name', 'woocommerce' ); ?>" name="order_item_name[<?php echo absint( $item_id ); ?>]" value="<?php if ( isset( $item['name'] ) ) echo esc_attr( $item['name'] ); ?>" />
<input type="hidden" class="order_item_id" name="order_item_id[]" value="<?php echo esc_attr( $item_id ); ?>" />
</td>
<td class="tax_class" width="1%">
<select class="tax_class" name="order_item_tax_class[<?php echo absint( $item_id ); ?>]" title="<?php _e( 'Tax class', 'woocommerce' ); ?>">
<?php
$item_value = isset( $item['tax_class'] ) ? sanitize_title( $item['tax_class'] ) : '';
$tax_classes = array_filter( array_map( 'trim', explode( "\n", get_option('woocommerce_tax_classes' ) ) ) );
$classes_options = array();
$classes_options[''] = __( 'Standard', 'woocommerce' );
if ( $tax_classes )
foreach ( $tax_classes as $class )
$classes_options[ sanitize_title( $class ) ] = $class;
foreach ( $classes_options as $value => $name )
echo '<option value="' . esc_attr( $value ) . '" ' . selected( $value, $item_value, false ) . '>'. esc_html( $name ) . '</option>';
?>
</select>
</td>
<td class="quantity" width="1%">1</td>
<td class="line_cost" width="1%">
<label><?php _e( 'Total', 'woocommerce' ); ?>: <input type="text" name="line_total[<?php echo absint( $item_id ); ?>]" placeholder="0.00" value="<?php if ( isset( $item['line_total'] ) ) echo esc_attr( $item['line_total'] ); ?>" class="line_total" /></label>
</td>
<td class="line_tax" width="1%">
<input type="text" name="line_tax[<?php echo absint( $item_id ); ?>]" placeholder="0.00" value="<?php if ( isset( $item['line_tax'] ) ) echo esc_attr( $item['line_tax'] ); ?>" class="line_tax" />
</td>
</tr>

View File

@ -103,9 +103,9 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
</td>
<td class="line_cost" width="1%">
<label><?php _e( 'Total', 'woocommerce' ); ?>: <input type="text" name="line_subtotal[<?php echo absint( $item_id ); ?>]" placeholder="0.00" value="<?php if ( isset( $item['line_subtotal'] ) ) echo esc_attr( $item['line_subtotal'] ); ?>" class="line_subtotal" /></label>
<label><?php _e( 'Total', 'woocommerce' ); ?>: <input type="text" name="line_total[<?php echo absint( $item_id ); ?>]" placeholder="0.00" value="<?php if ( isset( $item['line_total'] ) ) echo esc_attr( $item['line_total'] ); ?>" class="line_total" /></label>
<span class="subtotal"><label><?php _e( 'Subtotal', 'woocommerce' ); ?>: <input type="text" name="line_total[<?php echo absint( $item_id ); ?>]" placeholder="0.00" value="<?php if ( isset( $item['line_total'] ) ) echo esc_attr( $item['line_total'] ); ?>" class="line_total" /></label></span>
<span class="subtotal"><label><?php _e( 'Subtotal', 'woocommerce' ); ?>: <input type="text" name="line_subtotal[<?php echo absint( $item_id ); ?>]" placeholder="0.00" value="<?php if ( isset( $item['line_subtotal'] ) ) echo esc_attr( $item['line_subtotal'] ); ?>" class="line_subtotal" /></label></span>
</td>
<td class="line_tax" width="1%">

View File

@ -342,62 +342,19 @@ function woocommerce_order_items_meta_box( $post ) {
// List order items
$order_items = $order->get_items();
if ( ! empty( $order_items ) ) {
foreach ( $order_items as $item_id => $item ) {
if ( ! empty( $item['variation_id'] ) )
$_product = new WC_Product_Variation( $item['variation_id'] );
else
$_product = new WC_Product( $item['product_id'] );
$item_meta = $order->get_item_meta( $item_id );
include( 'order-item-html.php' );
}
foreach ( $order_items as $item_id => $item ) {
$_product = $order->get_product_from_item( $item );
$item_meta = $order->get_item_meta( $item_id );
include( 'order-item-html.php' );
}
?>
// List any fees
$order_fees = $order->get_fees();
<tr class="fee">
<td><input type="checkbox" /></td>
<th></th>
<td><input type="text" placeholder="<?php _e( 'Fee Name', 'woocommerce' ); ?>" name="fee_name[]" /></td>
<td class="tax_class" width="1%">
<select class="tax_class" name="fee_tax_class[<?php echo absint( $item_id ); ?>]" title="<?php _e( 'Tax class', 'woocommerce' ); ?>">
<?php
$item_value = isset( $item['tax_class'] ) ? sanitize_title( $item['tax_class'] ) : '';
$tax_classes = array_filter( array_map( 'trim', explode( "\n", get_option('woocommerce_tax_classes' ) ) ) );
$classes_options = array();
$classes_options[''] = __( 'Standard', 'woocommerce' );
if ( $tax_classes )
foreach ( $tax_classes as $class )
$classes_options[ sanitize_title( $class ) ] = $class;
foreach ( $classes_options as $value => $name )
echo '<option value="' . esc_attr( $value ) . '" ' . selected( $value, $item_value, false ) . '>'. esc_html( $name ) . '</option>';
?>
</select>
</td>
<td class="quantity" width="1%">
<input type="number" step="any" min="0" autocomplete="off" name="order_item_qty[<?php echo absint( $item_id ); ?>]" placeholder="0" value="<?php echo esc_attr( $item['qty'] ); ?>" size="4" class="quantity" />
</td>
<td class="line_cost" width="1%">
<label><?php _e( 'Total', 'woocommerce' ); ?>: <input type="text" name="line_total[<?php echo absint( $item_id ); ?>]" placeholder="0.00" value="<?php if ( isset( $item['line_total'] ) ) echo esc_attr( $item['line_total'] ); ?>" class="line_total" /></label>
</td>
<td class="line_tax" width="1%">
<input type="text" name="line_tax[<?php echo absint( $item_id ); ?>]" placeholder="0.00" value="<?php if ( isset( $item['line_tax'] ) ) echo esc_attr( $item['line_tax'] ); ?>" class="line_tax" /></label>
</td>
</tr>
<?php
foreach ( $order_fees as $item_id => $item ) {
include( 'order-fee-html.php' );
}
?>
</tbody>
</table>
@ -815,26 +772,44 @@ function woocommerce_process_shop_order_meta( $post_id, $post ) {
update_post_meta( $post_id, '_order_taxes', $order_taxes );
// Order items
// Order items + fees
if ( isset( $_POST['order_item_id'] ) ) {
$order_item_id = $_POST['order_item_id'];
$order_item_qty = $_POST['order_item_qty'];
$line_subtotal = $_POST['line_subtotal'];
$line_subtotal_tax = $_POST['line_subtotal_tax'];
$line_total = $_POST['line_total'];
$line_tax = $_POST['line_tax'];
$item_tax_class = $_POST['order_item_tax_class'];
$get_values = array( 'order_item_id', 'order_item_name', 'order_item_qty', 'line_subtotal', 'line_subtotal_tax', 'line_total', 'line_tax', 'order_item_tax_class' );
foreach( $get_values as $value )
$$value = isset( $_POST[ $value ] ) ? $_POST[ $value ] : array();
foreach ( $order_item_id as $item_id ) {
$item_id = absint( $item_id );
woocommerce_update_order_item_meta( $item_id, '_qty', absint( $order_item_qty[ $item_id ] ) );
woocommerce_update_order_item_meta( $item_id, '_tax_class', woocommerce_clean( $item_tax_class[ $item_id ] ) );
woocommerce_update_order_item_meta( $item_id, '_line_subtotal', woocommerce_clean( $line_subtotal[ $item_id ] ) );
woocommerce_update_order_item_meta( $item_id, '_line_subtotal_tax', woocommerce_clean( $line_subtotal_tax[ $item_id ] ) );
woocommerce_update_order_item_meta( $item_id, '_line_total', woocommerce_clean( $line_total[ $item_id ] ) );
woocommerce_update_order_item_meta( $item_id, '_line_tax', woocommerce_clean( $line_tax[ $item_id ] ) );
if ( isset( $order_item_name[ $item_id ] ) )
$wpdb->update(
$wpdb->prefix . "woocommerce_order_items",
array( 'order_item_name' => woocommerce_clean( $order_item_name[ $item_id ] ) ),
array( 'order_item_id' => $item_id ),
array( '%s' ),
array( '%d' )
);
if ( isset( $order_item_qty[ $item_id ] ) )
woocommerce_update_order_item_meta( $item_id, '_qty', absint( $order_item_qty[ $item_id ] ) );
if ( isset( $item_tax_class[ $item_id ] ) )
woocommerce_update_order_item_meta( $item_id, '_tax_class', woocommerce_clean( $item_tax_class[ $item_id ] ) );
if ( isset( $line_subtotal[ $item_id ] ) )
woocommerce_update_order_item_meta( $item_id, '_line_subtotal', woocommerce_clean( $line_subtotal[ $item_id ] ) );
if ( isset( $line_subtotal_tax[ $item_id ] ) )
woocommerce_update_order_item_meta( $item_id, '_line_subtotal_tax', woocommerce_clean( $line_subtotal_tax[ $item_id ] ) );
if ( isset( $line_total[ $item_id ] ) )
woocommerce_update_order_item_meta( $item_id, '_line_total', woocommerce_clean( $line_total[ $item_id ] ) );
if ( isset( $line_tax[ $item_id ] ) )
woocommerce_update_order_item_meta( $item_id, '_line_tax', woocommerce_clean( $line_tax[ $item_id ] ) );
}
}

File diff suppressed because one or more lines are too long

View File

@ -803,7 +803,7 @@ table.wc_status_table {
}
}
tr.fee {
th {
.thumb {
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAARklEQVQYGWP8//8/AzGACV3Rnj17/oMwujiGQnQFMD7RChlBbsRmFcwkEO3i4sJImonIumGmg0xBFifaRKIVgj2DbAUuNgCfThpracSKqwAAAABJRU5ErkJggg==) no-repeat center;
}
}

View File

@ -423,7 +423,8 @@ jQuery( function($){
}, function() {
$('#woocommerce-order-totals .calculated').css('background-color', '');
});
// Add a line item
$('#woocommerce-order-items button.add_order_item').click(function(){
var add_item_ids = $('select#add_item_id').val();
@ -467,9 +468,28 @@ jQuery( function($){
} else {
$('select#add_item_id, #add_item_id_chzn .chzn-choices').css('border-color', 'red');
}
return false;
});
// Add a fee
$('#woocommerce-order-items button.add_order_fee').click(function(){
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
var data = {
action: 'woocommerce_add_order_fee',
order_id: woocommerce_writepanel_params.post_id,
security: woocommerce_writepanel_params.order_item_nonce
};
$.post( woocommerce_writepanel_params.ajax_url, data, function( response ) {
$('table.woocommerce_order_items tbody#order_items_list').append( response );
$('table.woocommerce_order_items').unblock();
});
return false;
});
// Add some meta to a line item
$('#order_items_list button.add_order_item_meta').live('click', function(){
var $button = $(this);

File diff suppressed because one or more lines are too long

View File

@ -124,6 +124,9 @@ class WC_Order {
/** @var array Order line items array */
var $items;
/** @var array Order Fees array */
var $fees;
/** @var array Taxes array (tax rows) */
var $taxes;
@ -432,6 +435,40 @@ class WC_Order {
return $this->items;
}
/**
* Return an array of fees within this order.
*
* @access public
* @return array
*/
function get_fees() {
global $wpdb, $woocommerce;
if ( ! $this->fees ) {
$line_items = $wpdb->get_results( $wpdb->prepare( "
SELECT order_item_id, order_item_name
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_id = %d
AND order_item_type = 'fee'
ORDER BY order_item_id
", $this->id ) );
$this->fees = array();
foreach ( $line_items as $item ) {
$item_meta = $this->get_item_meta( $item->order_item_id );
$this->fees[ $item->order_item_id ] = array(
'name' => $item->order_item_name,
'tax_class' => $item_meta['_tax_class'][0],
'line_total' => $item_meta['_line_total'][0],
'line_tax' => $item_meta['_line_tax'][0],
);
}
}
return $this->fees;
}
/**
* has_meta function for order items.
*

View File

@ -885,6 +885,41 @@ function woocommerce_ajax_add_order_item() {
add_action('wp_ajax_woocommerce_add_order_item', 'woocommerce_ajax_add_order_item');
/**
* Add order fee via ajax
*
* @access public
* @return void
*/
function woocommerce_ajax_add_order_fee() {
global $woocommerce;
check_ajax_referer( 'order-item', 'security' );
$order_id = absint( $_POST['order_id'] );
$order = new WC_Order( $order_id );
// Add line item
$item_id = woocommerce_add_order_item( $order_id, array(
'order_item_name' => '',
'order_item_type' => 'fee'
) );
// Add line item meta
if ( $item_id ) {
woocommerce_add_order_item_meta( $item_id, '_tax_class', '' );
woocommerce_add_order_item_meta( $item_id, '_line_total', '' );
woocommerce_add_order_item_meta( $item_id, '_line_tax', '' );
}
include( 'admin/post-types/writepanels/order-fee-html.php' );
// Quit out
die();
}
add_action('wp_ajax_woocommerce_add_order_fee', 'woocommerce_ajax_add_order_fee');
/**
* woocommerce_ajax_remove_order_item function.
*