2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Order
|
|
|
|
*
|
2011-08-10 17:11:11 +00:00
|
|
|
* The WooCommerce order class handles order data.
|
2011-08-09 15:16:18 +00:00
|
|
|
*
|
2011-08-10 17:11:11 +00:00
|
|
|
* @class woocommerce_order
|
|
|
|
* @package WooCommerce
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2011-08-10 17:11:11 +00:00
|
|
|
class woocommerce_order {
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
private $_data = array();
|
|
|
|
|
|
|
|
public function __get($variable) {
|
|
|
|
return isset($this->_data[$variable]) ? $this->_data[$variable] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __set($variable, $value) {
|
|
|
|
$this->_data[$variable] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the order if ID is passed, otherwise the order is new and empty */
|
2011-08-10 17:11:11 +00:00
|
|
|
function woocommerce_order( $id='' ) {
|
2011-12-23 18:07:44 +00:00
|
|
|
$this->calc_taxes = (get_option('woocommerce_calc_taxes')=='yes') ? true : false;
|
2011-11-26 23:33:57 +00:00
|
|
|
$this->prices_include_tax = (get_option('woocommerce_prices_include_tax')=='yes') ? true : false;
|
|
|
|
$this->display_totals_ex_tax = (get_option('woocommerce_display_totals_excluding_tax')=='yes') ? true : false;
|
2011-11-27 00:03:46 +00:00
|
|
|
$this->display_cart_ex_tax = (get_option('woocommerce_display_cart_prices_excluding_tax')=='yes') ? true : false;
|
2011-11-26 23:33:57 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
if ($id>0) $this->get_order( $id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets an order from the database */
|
|
|
|
function get_order( $id = 0 ) {
|
|
|
|
if (!$id) return false;
|
|
|
|
if ($result = get_post( $id )) :
|
|
|
|
$this->populate( $result );
|
|
|
|
return true;
|
|
|
|
endif;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Populates an order from the loaded post data */
|
|
|
|
function populate( $result ) {
|
2011-09-06 11:11:22 +00:00
|
|
|
global $woocommerce;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
// Standard post data
|
|
|
|
$this->id = $result->ID;
|
2011-12-12 17:39:31 +00:00
|
|
|
$this->status = $result->post_status;
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->order_date = $result->post_date;
|
|
|
|
$this->modified_date = $result->post_modified;
|
|
|
|
$this->customer_note = $result->post_excerpt;
|
|
|
|
|
2011-08-29 16:09:42 +00:00
|
|
|
// Custom fields
|
|
|
|
$this->items = (array) get_post_meta( $this->id, '_order_items', true );
|
2012-01-02 00:49:00 +00:00
|
|
|
$this->taxes = (array) get_post_meta( $this->id, '_order_taxes', true );
|
2011-08-29 16:09:42 +00:00
|
|
|
$this->user_id = (int) get_post_meta( $this->id, '_customer_user', true );
|
2011-10-04 11:55:05 +00:00
|
|
|
$this->completed_date = get_post_meta( $this->id, '_completed_date', true );
|
|
|
|
|
|
|
|
if (!$this->completed_date) $this->completed_date = $this->modified_date;
|
2011-08-29 16:09:42 +00:00
|
|
|
|
|
|
|
$order_custom_fields = get_post_custom( $this->id );
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-08-29 16:09:42 +00:00
|
|
|
// Define the data we're going to load: Key => Default value
|
|
|
|
$load_data = array(
|
|
|
|
'order_key' => '',
|
|
|
|
'billing_first_name' => '',
|
|
|
|
'billing_last_name' => '',
|
|
|
|
'billing_company' => '',
|
|
|
|
'billing_address_1' => '',
|
|
|
|
'billing_address_2' => '',
|
|
|
|
'billing_city' => '',
|
|
|
|
'billing_postcode' => '',
|
|
|
|
'billing_country' => '',
|
|
|
|
'billing_state' => '',
|
|
|
|
'billing_email' => '',
|
|
|
|
'billing_phone' => '',
|
|
|
|
'shipping_first_name' => '',
|
|
|
|
'shipping_last_name' => '',
|
|
|
|
'shipping_company' => '',
|
|
|
|
'shipping_address_1' => '',
|
|
|
|
'shipping_address_2' => '',
|
|
|
|
'shipping_city' => '',
|
|
|
|
'shipping_postcode' => '',
|
|
|
|
'shipping_country' => '',
|
|
|
|
'shipping_state' => '',
|
|
|
|
'shipping_method' => '',
|
2011-12-05 18:16:13 +00:00
|
|
|
'shipping_method_title' => '',
|
2011-08-29 16:09:42 +00:00
|
|
|
'payment_method' => '',
|
2011-12-05 18:16:13 +00:00
|
|
|
'payment_method_title' => '',
|
2011-08-29 16:09:42 +00:00
|
|
|
'order_subtotal' => '',
|
|
|
|
'order_discount' => '',
|
2011-11-25 19:31:06 +00:00
|
|
|
'cart_discount' => '',
|
2011-08-29 16:09:42 +00:00
|
|
|
'order_tax' => '',
|
2011-09-02 14:42:04 +00:00
|
|
|
'order_shipping' => '',
|
2011-08-29 16:09:42 +00:00
|
|
|
'order_shipping_tax' => '',
|
|
|
|
'order_total' => ''
|
|
|
|
);
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-08-29 16:09:42 +00:00
|
|
|
// Load the data from the custom fields
|
|
|
|
foreach ($load_data as $key => $default) :
|
|
|
|
if (isset($order_custom_fields[ '_' . $key ][0]) && $order_custom_fields[ '_' . $key ][0]!=='') :
|
|
|
|
$this->$key = $order_custom_fields[ '_' . $key ][0];
|
|
|
|
else :
|
|
|
|
$this->$key = $default;
|
|
|
|
endif;
|
|
|
|
endforeach;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
// Formatted Addresses
|
2011-12-18 13:41:42 +00:00
|
|
|
$address = array(
|
|
|
|
'first_name' => $this->billing_first_name,
|
|
|
|
'last_name' => $this->billing_last_name,
|
|
|
|
'company' => $this->billing_company,
|
|
|
|
'address_1' => $this->billing_address_1,
|
|
|
|
'address_2' => $this->billing_address_2,
|
|
|
|
'city' => $this->billing_city,
|
|
|
|
'state' => $this->billing_state,
|
|
|
|
'postcode' => $this->billing_postcode,
|
|
|
|
'country' => $this->billing_country
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->formatted_billing_address = $woocommerce->countries->get_formatted_address( $address );
|
2011-11-16 15:25:45 +00:00
|
|
|
|
2011-12-18 13:41:42 +00:00
|
|
|
unset($address['first_name']);
|
|
|
|
unset($address['last_name']);
|
|
|
|
unset($address['company']);
|
2011-12-19 17:50:41 +00:00
|
|
|
$joined_address = array();
|
2011-12-18 13:41:42 +00:00
|
|
|
foreach ($address as $part) if (!empty($part)) $joined_address[] = $part;
|
|
|
|
$this->billing_address_only = implode(', ', $joined_address);
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
if ($this->shipping_address_1) :
|
2011-12-18 13:41:42 +00:00
|
|
|
$address = array(
|
|
|
|
'first_name' => $this->shipping_first_name,
|
|
|
|
'last_name' => $this->shipping_last_name,
|
|
|
|
'company' => $this->shipping_company,
|
|
|
|
'address_1' => $this->shipping_address_1,
|
|
|
|
'address_2' => $this->shipping_address_2,
|
|
|
|
'city' => $this->shipping_city,
|
|
|
|
'state' => $this->shipping_state,
|
|
|
|
'postcode' => $this->shipping_postcode,
|
|
|
|
'country' => $this->shipping_country
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->formatted_shipping_address = $woocommerce->countries->get_formatted_address( $address );
|
2011-11-16 15:25:45 +00:00
|
|
|
|
2011-12-18 13:41:42 +00:00
|
|
|
unset($address['first_name']);
|
|
|
|
unset($address['last_name']);
|
|
|
|
unset($address['company']);
|
2011-12-19 17:50:41 +00:00
|
|
|
$joined_address = array();
|
2011-12-18 13:41:42 +00:00
|
|
|
foreach ($address as $part) if (!empty($part)) $joined_address[] = $part;
|
|
|
|
$this->shipping_address_only = implode(', ', $joined_address);
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
|
|
|
// Taxonomy data
|
|
|
|
$terms = wp_get_object_terms( $this->id, 'shop_order_status' );
|
|
|
|
if (!is_wp_error($terms) && $terms) :
|
|
|
|
$term = current($terms);
|
|
|
|
$this->status = $term->slug;
|
|
|
|
else :
|
|
|
|
$this->status = 'pending';
|
|
|
|
endif;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets shipping and product tax */
|
|
|
|
function get_total_tax() {
|
|
|
|
return $this->order_tax + $this->order_shipping_tax;
|
|
|
|
}
|
|
|
|
|
2011-11-26 23:33:57 +00:00
|
|
|
/**
|
|
|
|
* gets the total (product) discount amount - these are applied before tax
|
|
|
|
*/
|
|
|
|
function get_cart_discount() {
|
|
|
|
return $this->cart_discount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets the total (product) discount amount - these are applied before tax
|
|
|
|
*/
|
|
|
|
function get_order_discount() {
|
|
|
|
return $this->order_discount;
|
|
|
|
}
|
2011-11-19 20:59:16 +00:00
|
|
|
|
2011-11-26 23:33:57 +00:00
|
|
|
/**
|
|
|
|
* gets the total discount amount - both kinds
|
|
|
|
*/
|
2011-11-19 20:59:16 +00:00
|
|
|
function get_total_discount() {
|
2011-11-26 23:33:57 +00:00
|
|
|
if ($this->order_discount || $this->cart_discount) :
|
|
|
|
return $this->order_discount + $this->cart_discount;
|
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets shipping */
|
|
|
|
function get_shipping() {
|
|
|
|
return $this->order_shipping;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets order total */
|
|
|
|
function get_order_total() {
|
|
|
|
return $this->order_total;
|
2011-11-19 20:59:16 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 00:52:57 +00:00
|
|
|
/** Calculate item cost - useful for gateways */
|
|
|
|
function get_item_cost( $item, $inc_tax = false ) {
|
|
|
|
if ($inc_tax) :
|
2012-01-02 00:49:00 +00:00
|
|
|
return number_format( ($item['line_cost'] + $item['line_tax']) / $item['qty'] , 2, '.', '');
|
2011-11-29 00:52:57 +00:00
|
|
|
else :
|
2012-01-02 00:49:00 +00:00
|
|
|
return number_format( $item['line_cost'] / $item['qty'] , 2, '.', '');
|
2011-11-29 00:52:57 +00:00
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
2011-11-28 17:03:53 +00:00
|
|
|
/** Calculate row cost - useful for gateways */
|
|
|
|
function get_row_cost( $item, $inc_tax = false ) {
|
|
|
|
if ($inc_tax) :
|
2011-12-31 19:03:41 +00:00
|
|
|
return number_format( $item['line_cost'] + $item['line_tax'] , 2, '.', '');
|
2011-11-28 17:03:53 +00:00
|
|
|
else :
|
2011-12-31 19:03:41 +00:00
|
|
|
return number_format( $item['line_cost'] , 2, '.', '');
|
2011-11-28 17:03:53 +00:00
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/** Gets subtotal */
|
2012-01-02 00:49:00 +00:00
|
|
|
function get_subtotal_to_display( $compound = false ) {
|
2011-10-31 10:54:49 +00:00
|
|
|
global $woocommerce;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-01-02 00:49:00 +00:00
|
|
|
$subtotal = 0;
|
|
|
|
|
|
|
|
foreach ($this->items as $item) :
|
|
|
|
|
|
|
|
$subtotal += $item['base_cost'] * $item['qty'];
|
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
// If showing compound taxes subtotal, add shipping + non-compound taxes
|
|
|
|
if ($compound && is_array($this->taxes)) :
|
|
|
|
|
|
|
|
$subtotal += $this->get_shipping();
|
|
|
|
|
|
|
|
foreach ($this->taxes as $tax) :
|
|
|
|
|
|
|
|
if (isset($tax['compound']) && $tax['compound']) continue;
|
|
|
|
|
|
|
|
$subtotal += $tax['total'];
|
2011-11-26 23:33:57 +00:00
|
|
|
|
2012-01-02 00:49:00 +00:00
|
|
|
endforeach;
|
2011-11-26 23:33:57 +00:00
|
|
|
|
2012-01-02 00:49:00 +00:00
|
|
|
$subtotal = woocommerce_price($subtotal);
|
2011-11-26 23:33:57 +00:00
|
|
|
|
2012-01-02 00:49:00 +00:00
|
|
|
// If this is the cart subtotal, and we want to display prices inc tax, add it
|
|
|
|
elseif (!$this->display_totals_ex_tax && $this->prices_include_tax) :
|
2011-11-26 23:33:57 +00:00
|
|
|
|
2012-01-02 00:49:00 +00:00
|
|
|
// Add tax to subtotal
|
|
|
|
$subtotal += $this->order_tax;
|
2011-11-26 23:33:57 +00:00
|
|
|
|
2012-01-02 00:49:00 +00:00
|
|
|
$subtotal = woocommerce_price($subtotal);
|
2011-11-26 23:33:57 +00:00
|
|
|
|
|
|
|
if ($this->order_tax>0 && !$this->prices_include_tax) :
|
|
|
|
$subtotal .= ' <small>'.$woocommerce->countries->inc_tax_or_vat().'</small>';
|
|
|
|
endif;
|
2012-01-02 00:49:00 +00:00
|
|
|
|
|
|
|
else :
|
|
|
|
|
|
|
|
$subtotal = woocommerce_price($subtotal);
|
|
|
|
|
|
|
|
if ($this->order_tax>0 && $this->prices_include_tax) :
|
|
|
|
$subtotal .= ' <small>'.$woocommerce->countries->ex_tax_or_vat().'</small>';
|
|
|
|
endif;
|
2011-08-29 16:09:42 +00:00
|
|
|
|
|
|
|
endif;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
return $subtotal;
|
|
|
|
}
|
2011-11-26 23:33:57 +00:00
|
|
|
|
|
|
|
/** Gets shipping (formatted) */
|
2011-08-09 15:16:18 +00:00
|
|
|
function get_shipping_to_display() {
|
2011-10-31 10:54:49 +00:00
|
|
|
global $woocommerce;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
if ($this->order_shipping > 0) :
|
2011-11-27 00:03:46 +00:00
|
|
|
|
|
|
|
$tax_text = '';
|
|
|
|
|
|
|
|
if ($this->display_totals_ex_tax || !$this->prices_include_tax) :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-11-27 00:03:46 +00:00
|
|
|
// Show shipping excluding tax
|
|
|
|
$shipping = woocommerce_price($this->order_shipping);
|
|
|
|
if ($this->order_shipping_tax > 0 && $this->prices_include_tax) :
|
|
|
|
$tax_text = $woocommerce->countries->ex_tax_or_vat() . ' ';
|
|
|
|
endif;
|
|
|
|
|
2011-09-02 14:42:04 +00:00
|
|
|
else :
|
2011-11-27 00:03:46 +00:00
|
|
|
|
|
|
|
// Show shipping including tax
|
|
|
|
$shipping = woocommerce_price($this->order_shipping + $this->order_shipping_tax);
|
|
|
|
if ($this->order_shipping_tax > 0 && !$this->prices_include_tax) :
|
|
|
|
$tax_text = $woocommerce->countries->inc_tax_or_vat() . ' ';
|
|
|
|
endif;
|
|
|
|
|
2011-08-29 16:09:42 +00:00
|
|
|
endif;
|
2011-11-27 00:03:46 +00:00
|
|
|
|
2011-12-05 18:16:13 +00:00
|
|
|
$shipping .= sprintf(__(' <small>%svia %s</small>', 'woothemes'), $tax_text, ucwords($this->shipping_method_title));
|
2011-11-27 00:03:46 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
else :
|
2011-08-10 17:11:11 +00:00
|
|
|
$shipping = __('Free!', 'woothemes');
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
|
|
|
return $shipping;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get a product (either product or variation) */
|
|
|
|
function get_product_from_item( $item ) {
|
|
|
|
|
|
|
|
if (isset($item['variation_id']) && $item['variation_id']>0) :
|
2011-08-10 17:11:11 +00:00
|
|
|
$_product = &new woocommerce_product_variation( $item['variation_id'] );
|
2011-08-09 15:16:18 +00:00
|
|
|
else :
|
2011-08-10 17:11:11 +00:00
|
|
|
$_product = &new woocommerce_product( $item['id'] );
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
|
|
|
return $_product;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-08-16 14:06:08 +00:00
|
|
|
/** Output items for display in html emails */
|
|
|
|
function email_order_items_table( $show_download_links = false, $show_sku = false ) {
|
|
|
|
|
|
|
|
$return = '';
|
|
|
|
|
|
|
|
foreach($this->items as $item) :
|
|
|
|
|
|
|
|
$_product = $this->get_product_from_item( $item );
|
|
|
|
|
|
|
|
$file = $sku = $variation = '';
|
|
|
|
|
|
|
|
if ($show_sku) :
|
|
|
|
$sku = ' (#' . $_product->sku . ')';
|
|
|
|
endif;
|
|
|
|
|
2011-11-06 15:45:22 +00:00
|
|
|
$item_meta = &new order_item_meta( $item['item_meta'] );
|
2011-11-26 23:33:57 +00:00
|
|
|
$variation = '<br/><small>' . $item_meta->display( true, true ) . '</small>';
|
2011-08-16 14:06:08 +00:00
|
|
|
|
|
|
|
if ($show_download_links) :
|
|
|
|
|
|
|
|
if ($_product->exists) :
|
|
|
|
|
2011-11-05 19:03:03 +00:00
|
|
|
if ($_product->is_downloadable()) :
|
2011-12-19 17:50:41 +00:00
|
|
|
$file = '<br/><small>'.__('Download:', 'woothemes').' <a href="' . $this->get_downloadable_file_url( $item['id'], $item['variation_id'] ) . '">' . $this->get_downloadable_file_url( $item['id'], $item['variation_id'] ) . '</a></small>';
|
2011-08-16 14:06:08 +00:00
|
|
|
endif;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
2011-10-05 00:02:13 +00:00
|
|
|
$return .= '<tr>
|
2011-12-02 14:04:26 +00:00
|
|
|
<td style="text-align:left; border: 1px solid #eee;">' . apply_filters('woocommerce_order_product_title', $item['name'], $_product) . $sku . $file . $variation . '</td>
|
|
|
|
<td style="text-align:left; border: 1px solid #eee;">'.$item['qty'].'</td>
|
|
|
|
<td style="text-align:left; border: 1px solid #eee;">';
|
2011-11-26 23:33:57 +00:00
|
|
|
|
2011-12-23 18:07:44 +00:00
|
|
|
if ( $this->display_cart_ex_tax || !$this->prices_include_tax ) :
|
|
|
|
|
|
|
|
$ex_tax_label = ( $this->prices_include_tax ) ? 1 : 0;
|
|
|
|
|
2011-11-30 00:30:25 +00:00
|
|
|
$return .= woocommerce_price( $item['base_cost']*$item['qty'], array('ex_tax_label' => $ex_tax_label ));
|
2011-12-23 18:07:44 +00:00
|
|
|
|
2011-11-30 00:30:25 +00:00
|
|
|
else :
|
|
|
|
$return .= woocommerce_price( round(($item['base_cost']*$item['qty']) * (($item['taxrate']/100) + 1), 2) );
|
|
|
|
endif;
|
2011-11-26 23:33:57 +00:00
|
|
|
|
|
|
|
$return .= '
|
|
|
|
</td>
|
2011-08-16 14:06:08 +00:00
|
|
|
</tr>';
|
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
return $return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-10-12 17:32:30 +00:00
|
|
|
/** Returns true if the order contains a downloadable product */
|
|
|
|
function has_downloadable_item() {
|
|
|
|
$has_downloadable_item = false;
|
|
|
|
|
|
|
|
foreach($this->items as $item) :
|
|
|
|
|
|
|
|
$_product = $this->get_product_from_item( $item );
|
|
|
|
|
2011-11-05 19:03:03 +00:00
|
|
|
if ($_product->exists && $_product->is_downloadable()) :
|
2011-10-12 17:32:30 +00:00
|
|
|
$has_downloadable_item = true;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
return $has_downloadable_item;
|
|
|
|
}
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/** Generates a URL so that a customer can checkout/pay for their (unpaid - pending) order via a link */
|
|
|
|
function get_checkout_payment_url() {
|
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
$payment_page = get_permalink(get_option('woocommerce_pay_page_id'));
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
if (get_option('woocommerce_force_ssl_checkout')=='yes' || is_ssl()) $payment_page = str_replace('http:', 'https:', $payment_page);
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
return add_query_arg('pay_for_order', 'true', add_query_arg('order', $this->order_key, add_query_arg('order_id', $this->id, $payment_page)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Generates a URL so that a customer can cancel their (unpaid - pending) order */
|
|
|
|
function get_cancel_order_url() {
|
2011-09-06 11:11:22 +00:00
|
|
|
global $woocommerce;
|
2011-09-07 10:01:47 +00:00
|
|
|
return $woocommerce->nonce_url( 'cancel_order', add_query_arg('cancel_order', 'true', add_query_arg('order', $this->order_key, add_query_arg('order_id', $this->id, trailingslashit( home_url() )))));
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Gets a downloadable products file url */
|
2011-11-09 23:06:17 +00:00
|
|
|
function get_downloadable_file_url( $item_id, $variation_id ) {
|
|
|
|
|
|
|
|
$download_id = ($variation_id>0) ? $variation_id : $item_id;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
$user_email = $this->billing_email;
|
|
|
|
|
|
|
|
if ($this->user_id>0) :
|
|
|
|
$user_info = get_userdata($this->user_id);
|
|
|
|
if ($user_info->user_email) :
|
|
|
|
$user_email = $user_info->user_email;
|
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
2011-11-09 23:06:17 +00:00
|
|
|
return add_query_arg('download_file', $download_id, add_query_arg('order', $this->order_key, add_query_arg('email', $user_email, trailingslashit( home_url() ))));
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a note (comment) to the order
|
|
|
|
*
|
|
|
|
* @param string $note Note to add
|
2011-09-23 14:22:21 +00:00
|
|
|
* @param int $is_customer_note Is this a note for the customer?
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2011-09-23 14:22:21 +00:00
|
|
|
function add_order_note( $note, $is_customer_note = 0 ) {
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
$comment_post_ID = $this->id;
|
2011-08-10 17:11:11 +00:00
|
|
|
$comment_author = 'WooCommerce';
|
|
|
|
$comment_author_email = 'woocommerce@' . str_replace('www.', '', str_replace('http://', '', site_url()));
|
2011-08-09 15:16:18 +00:00
|
|
|
$comment_author_url = '';
|
|
|
|
$comment_content = $note;
|
|
|
|
$comment_type = '';
|
|
|
|
$comment_parent = 0;
|
|
|
|
|
|
|
|
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
|
|
|
|
|
2011-09-22 19:50:58 +00:00
|
|
|
$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
|
2011-08-09 15:16:18 +00:00
|
|
|
$commentdata['comment_agent'] = substr($_SERVER['HTTP_USER_AGENT'], 0, 254);
|
|
|
|
|
|
|
|
$commentdata['comment_date'] = current_time('mysql');
|
|
|
|
$commentdata['comment_date_gmt'] = current_time('mysql', 1);
|
|
|
|
|
|
|
|
$comment_id = wp_insert_comment( $commentdata );
|
|
|
|
|
2011-09-23 14:22:21 +00:00
|
|
|
add_comment_meta($comment_id, 'is_customer_note', $is_customer_note);
|
|
|
|
|
2011-11-17 01:06:39 +00:00
|
|
|
if ($is_customer_note) :
|
2011-12-10 17:28:32 +00:00
|
|
|
do_action( 'woocommerce_new_customer_note', array( 'order_id' => $this->id, 'customer_note' => $note ) );
|
2011-11-17 01:06:39 +00:00
|
|
|
endif;
|
|
|
|
|
2011-09-23 14:22:21 +00:00
|
|
|
return $comment_id;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a note (comment) to the order
|
|
|
|
*
|
|
|
|
* @param string $new_status Status to change the order to
|
|
|
|
* @param string $note Optional note to add
|
|
|
|
*/
|
|
|
|
function update_status( $new_status, $note = '' ) {
|
|
|
|
|
|
|
|
if ($note) $note .= ' ';
|
|
|
|
|
|
|
|
$new_status = get_term_by( 'slug', sanitize_title( $new_status ), 'shop_order_status');
|
|
|
|
if ($new_status) :
|
|
|
|
|
|
|
|
wp_set_object_terms($this->id, $new_status->slug, 'shop_order_status');
|
|
|
|
|
|
|
|
if ( $this->status != $new_status->slug ) :
|
|
|
|
// Status was changed
|
2011-09-21 15:43:37 +00:00
|
|
|
do_action( 'woocommerce_order_status_'.$new_status->slug, $this->id );
|
|
|
|
do_action( 'woocommerce_order_status_'.$this->status.'_to_'.$new_status->slug, $this->id );
|
2011-08-10 17:11:11 +00:00
|
|
|
$this->add_order_note( $note . sprintf( __('Order status changed from %s to %s.', 'woothemes'), $this->status, $new_status->slug ) );
|
2011-08-09 15:16:18 +00:00
|
|
|
clean_term_cache( '', 'shop_order_status' );
|
2011-10-04 11:55:05 +00:00
|
|
|
|
|
|
|
// Date
|
2011-11-01 15:41:47 +00:00
|
|
|
if ($new_status->slug=='completed') :
|
|
|
|
update_post_meta( $this->id, '_completed_date', current_time('mysql') );
|
|
|
|
endif;
|
|
|
|
|
|
|
|
// Sales
|
|
|
|
if ($this->status == 'on-hold' && ($new_status->slug=='processing' || $new_status->slug=='completed')) :
|
|
|
|
$this->record_product_sales();
|
|
|
|
endif;
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel the order and restore the cart (before payment)
|
|
|
|
*
|
|
|
|
* @param string $note Optional note to add
|
|
|
|
*/
|
|
|
|
function cancel_order( $note = '' ) {
|
|
|
|
|
|
|
|
unset($_SESSION['order_awaiting_payment']);
|
|
|
|
|
|
|
|
$this->update_status('cancelled', $note);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When a payment is complete this function is called
|
|
|
|
*
|
|
|
|
* Most of the time this should mark an order as 'processing' so that admin can process/post the items
|
|
|
|
* If the cart contains only downloadable items then the order is 'complete' since the admin needs to take no action
|
|
|
|
* Stock levels are reduced at this point
|
2011-11-01 15:41:47 +00:00
|
|
|
* Sales are also recorded for products
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
|
|
|
function payment_complete() {
|
|
|
|
|
|
|
|
unset($_SESSION['order_awaiting_payment']);
|
|
|
|
|
2011-11-18 11:29:55 +00:00
|
|
|
if ( $this->status=='on-hold' || $this->status=='pending' ) :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-11-18 11:29:55 +00:00
|
|
|
$downloadable_order = false;
|
2011-11-01 15:41:47 +00:00
|
|
|
|
2011-11-18 11:29:55 +00:00
|
|
|
if (sizeof($this->items)>0) foreach ($this->items as $item) :
|
|
|
|
|
|
|
|
if ($item['id']>0) :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-11-18 11:29:55 +00:00
|
|
|
$_product = $this->get_product_from_item( $item );
|
|
|
|
|
|
|
|
if ( $_product->exists && $_product->is_downloadable() && $_product->is_virtual() ) :
|
|
|
|
$downloadable_order = true;
|
|
|
|
continue;
|
|
|
|
endif;
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
2011-11-18 11:29:55 +00:00
|
|
|
$downloadable_order = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
if ($downloadable_order) :
|
|
|
|
$new_order_status = 'completed';
|
|
|
|
else :
|
|
|
|
$new_order_status = 'processing';
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
2011-11-18 11:29:55 +00:00
|
|
|
$new_order_status = apply_filters('woocommerce_payment_complete_order_status', $new_order_status, $this->id);
|
|
|
|
|
|
|
|
$this->update_status($new_order_status);
|
|
|
|
|
|
|
|
// Payment is complete so reduce stock levels
|
|
|
|
$this->reduce_order_stock();
|
|
|
|
|
|
|
|
do_action( 'woocommerce_payment_complete', $this->id );
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
2011-11-01 15:41:47 +00:00
|
|
|
/**
|
|
|
|
* Record sales
|
|
|
|
*/
|
|
|
|
function record_product_sales() {
|
2011-11-18 11:29:55 +00:00
|
|
|
|
|
|
|
if ( get_post_meta( $this->id, '_recorded_sales', true )=='yes' ) return;
|
|
|
|
|
2011-11-01 15:41:47 +00:00
|
|
|
if (sizeof($this->items)>0) foreach ($this->items as $item) :
|
|
|
|
if ($item['id']>0) :
|
|
|
|
$sales = (int) get_post_meta( $item['id'], 'total_sales', true );
|
|
|
|
$sales += (int) $item['qty'];
|
|
|
|
if ($sales) update_post_meta( $item['id'], 'total_sales', $sales );
|
|
|
|
endif;
|
|
|
|
endforeach;
|
|
|
|
|
2011-11-18 11:29:55 +00:00
|
|
|
update_post_meta( $this->id, '_recorded_sales', 'yes' );
|
|
|
|
|
2011-11-01 15:41:47 +00:00
|
|
|
}
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
|
|
|
* Reduce stock levels
|
|
|
|
*/
|
|
|
|
function reduce_order_stock() {
|
|
|
|
|
2011-10-30 11:18:12 +00:00
|
|
|
if (get_option('woocommerce_manage_stock')=='yes' && sizeof($this->items)>0) :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-10-30 11:18:12 +00:00
|
|
|
// Reduce stock levels and do any other actions with products in the cart
|
|
|
|
foreach ($this->items as $item) :
|
|
|
|
|
|
|
|
if ($item['id']>0) :
|
|
|
|
$_product = $this->get_product_from_item( $item );
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-10-30 11:18:12 +00:00
|
|
|
if ( $_product->exists && $_product->managing_stock() ) :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-10-30 11:18:12 +00:00
|
|
|
$old_stock = $_product->stock;
|
|
|
|
|
|
|
|
$new_quantity = $_product->reduce_stock( $item['qty'] );
|
|
|
|
|
|
|
|
$this->add_order_note( sprintf( __('Item #%s stock reduced from %s to %s.', 'woothemes'), $item['id'], $old_stock, $new_quantity) );
|
|
|
|
|
|
|
|
if ($new_quantity<0) :
|
2011-12-23 19:46:27 +00:00
|
|
|
do_action('woocommerce_product_on_backorder', array( 'product' => $order_item['id'], 'order_id' => $this->id, 'quantity' => $values['quantity']));
|
2011-10-30 11:18:12 +00:00
|
|
|
endif;
|
|
|
|
|
|
|
|
// stock status notifications
|
|
|
|
if (get_option('woocommerce_notify_no_stock_amount') && get_option('woocommerce_notify_no_stock_amount')>=$new_quantity) :
|
2011-12-10 17:28:32 +00:00
|
|
|
do_action('woocommerce_no_stock', $item['id']);
|
2011-10-30 11:18:12 +00:00
|
|
|
elseif (get_option('woocommerce_notify_low_stock_amount') && get_option('woocommerce_notify_low_stock_amount')>=$new_quantity) :
|
2011-12-10 17:28:32 +00:00
|
|
|
do_action('woocommerce_low_stock', $item['id']);
|
2011-10-30 11:18:12 +00:00
|
|
|
endif;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
endif;
|
|
|
|
|
|
|
|
endif;
|
2011-10-30 11:18:12 +00:00
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
$this->add_order_note( __('Order item stock reduced successfully.', 'woothemes') );
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-10-30 11:18:12 +00:00
|
|
|
endif;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-23 14:22:21 +00:00
|
|
|
/**
|
|
|
|
* List order notes (public) for the customer
|
|
|
|
*/
|
|
|
|
function get_customer_order_notes() {
|
|
|
|
|
|
|
|
$notes = array();
|
|
|
|
|
|
|
|
$args = array(
|
|
|
|
'post_id' => $this->id,
|
|
|
|
'approve' => 'approve',
|
|
|
|
'type' => ''
|
|
|
|
);
|
|
|
|
|
|
|
|
remove_filter('comments_clauses', 'woocommerce_exclude_order_comments');
|
|
|
|
|
|
|
|
$comments = get_comments( $args );
|
|
|
|
|
|
|
|
foreach ($comments as $comment) :
|
|
|
|
$is_customer_note = get_comment_meta($comment->comment_ID, 'is_customer_note', true);
|
|
|
|
if ($is_customer_note) $notes[] = $comment;
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
add_filter('comments_clauses', 'woocommerce_exclude_order_comments');
|
|
|
|
|
|
|
|
return (array) $notes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-06 15:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Order Item Meta
|
|
|
|
*
|
|
|
|
* A Simple class for managing order item meta so plugins add it in the correct format
|
|
|
|
*/
|
|
|
|
class order_item_meta {
|
|
|
|
|
|
|
|
var $meta;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
function __construct( $item_meta = '' ) {
|
|
|
|
$this->meta = array();
|
|
|
|
|
|
|
|
if ($item_meta) $this->meta = $item_meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load item meta
|
|
|
|
*/
|
|
|
|
function new_order_item( $item ) {
|
|
|
|
if ($item) :
|
2011-11-16 10:08:22 +00:00
|
|
|
do_action('woocommerce_order_item_meta', $this, $item);
|
2011-11-06 15:45:22 +00:00
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add meta
|
|
|
|
*/
|
|
|
|
function add( $name, $value ) {
|
|
|
|
$this->meta[] = array(
|
|
|
|
'meta_name' => $name,
|
|
|
|
'meta_value' => $value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display meta in a formatted list
|
|
|
|
*/
|
|
|
|
function display( $flat = false, $return = false ) {
|
|
|
|
global $woocommerce;
|
|
|
|
|
|
|
|
if ($this->meta && is_array($this->meta)) :
|
|
|
|
|
|
|
|
if (!$flat) $output = '<dl class="variation">'; else $output = '';
|
|
|
|
|
|
|
|
$meta_list = array();
|
|
|
|
|
|
|
|
foreach ($this->meta as $meta) :
|
|
|
|
|
|
|
|
$name = $meta['meta_name'];
|
|
|
|
$value = $meta['meta_value'];
|
|
|
|
|
|
|
|
if (!$value) continue;
|
|
|
|
|
|
|
|
// If this is a term slug, get the term's nice name
|
|
|
|
if (taxonomy_exists(esc_attr(str_replace('attribute_', '', $name)))) :
|
|
|
|
$term = get_term_by('slug', $value, esc_attr(str_replace('attribute_', '', $name)));
|
|
|
|
if (!is_wp_error($term) && $term->name) :
|
|
|
|
$value = $term->name;
|
|
|
|
endif;
|
|
|
|
else :
|
|
|
|
$value = ucfirst($value);
|
|
|
|
endif;
|
|
|
|
|
|
|
|
if ($flat) :
|
|
|
|
$meta_list[] = $woocommerce->attribute_label(str_replace('attribute_', '', $name)).': '.$value;
|
|
|
|
else :
|
|
|
|
$meta_list[] = '<dt>'.$woocommerce->attribute_label(str_replace('attribute_', '', $name)).':</dt><dd>'.$value.'</dd>';
|
|
|
|
endif;
|
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
if ($flat) :
|
|
|
|
$output .= implode(', ', $meta_list);
|
|
|
|
else :
|
|
|
|
$output .= implode('', $meta_list);
|
|
|
|
endif;
|
|
|
|
|
|
|
|
if (!$flat) $output .= '</dl>';
|
|
|
|
|
|
|
|
if ($return) return $output; else echo $output;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|