_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 */
function woocommerce_order( $id='' ) {
$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;
$this->display_cart_ex_tax = (get_option('woocommerce_display_cart_prices_excluding_tax')=='yes') ? true : false;
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 ) {
global $woocommerce;
// Standard post data
$this->id = $result->ID;
$this->status = $result->post_status;
$this->order_date = $result->post_date;
$this->modified_date = $result->post_modified;
$this->customer_note = $result->post_excerpt;
// Custom fields
$this->items = (array) get_post_meta( $this->id, '_order_items', true );
$this->user_id = (int) get_post_meta( $this->id, '_customer_user', true );
$this->completed_date = get_post_meta( $this->id, '_completed_date', true );
if (!$this->completed_date) $this->completed_date = $this->modified_date;
$order_custom_fields = get_post_custom( $this->id );
// 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' => '',
'shipping_method_title' => '',
'payment_method' => '',
'payment_method_title' => '',
'order_subtotal' => '',
'order_discount' => '',
'cart_discount' => '',
'order_tax' => '',
'order_shipping' => '',
'order_shipping_tax' => '',
'order_total' => ''
);
// 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;
// Formatted Addresses
$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 );
unset($address['first_name']);
unset($address['last_name']);
unset($address['company']);
$joined_address = array();
foreach ($address as $part) if (!empty($part)) $joined_address[] = $part;
$this->billing_address_only = implode(', ', $joined_address);
if ($this->shipping_address_1) :
$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 );
unset($address['first_name']);
unset($address['last_name']);
unset($address['company']);
$joined_address = array();
foreach ($address as $part) if (!empty($part)) $joined_address[] = $part;
$this->shipping_address_only = implode(', ', $joined_address);
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;
}
/**
* 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;
}
/**
* gets the total discount amount - both kinds
*/
function get_total_discount() {
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;
}
/** Calculate item cost - useful for gateways */
function get_item_cost( $item, $inc_tax = false ) {
if ($inc_tax) :
return number_format( $item['cost'] * (1 + ($item['taxrate']/100)) , 2, '.', '');
else :
return number_format( $item['cost'] , 2, '.', '');
endif;
}
/** Calculate row cost - useful for gateways */
function get_row_cost( $item, $inc_tax = false ) {
if ($inc_tax) :
return number_format( ($item['cost'] * $item['qty']) * (1 + ($item['taxrate']/100)) , 2, '.', '');
else :
return number_format( $item['cost'] * $item['qty'] , 2, '.', '');
endif;
}
/** Gets subtotal */
function get_subtotal_to_display() {
global $woocommerce;
if ($this->display_totals_ex_tax || !$this->prices_include_tax) :
$subtotal = woocommerce_price($this->order_subtotal);
if ($this->order_tax>0 && $this->prices_include_tax) :
$subtotal .= ' '.$woocommerce->countries->ex_tax_or_vat().'';
endif;
else :
// Calculate subtotal inc. tax
$subtotal = 0;
foreach ($this->items as $item) :
if (!isset($item['base_cost'])) $item['base_cost'] = $item['cost'];
$subtotal += round(($item['base_cost']*$item['qty']) * (($item['taxrate']/100) + 1), 2);
endforeach;
$subtotal = woocommerce_price( $subtotal );
if ($this->order_tax>0 && !$this->prices_include_tax) :
$subtotal .= ' '.$woocommerce->countries->inc_tax_or_vat().'';
endif;
endif;
return $subtotal;
}
/** Gets shipping (formatted) */
function get_shipping_to_display() {
global $woocommerce;
if ($this->order_shipping > 0) :
$tax_text = '';
if ($this->display_totals_ex_tax || !$this->prices_include_tax) :
// 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;
else :
// 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;
endif;
$shipping .= sprintf(__(' %svia %s', 'woothemes'), $tax_text, ucwords($this->shipping_method_title));
else :
$shipping = __('Free!', 'woothemes');
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) :
$_product = &new woocommerce_product_variation( $item['variation_id'] );
else :
$_product = &new woocommerce_product( $item['id'] );
endif;
return $_product;
}
/** Output items for display in emails */
function email_order_items_list( $show_download_links = false, $show_sku = false ) {
$return = '';
foreach($this->items as $item) :
$_product = $this->get_product_from_item( $item );
$return .= $item['qty'] . ' x ' . apply_filters('woocommerce_order_product_title', $item['name'], $_product);
if ($show_sku) :
$return .= ' (#' . $_product->sku . ')';
endif;
$return .= ' - ' . strip_tags(woocommerce_price( $item['cost']*$item['qty'], array('ex_tax_label' => 1 )));
$item_meta = &new order_item_meta( $item['item_meta'] );
$return .= PHP_EOL . $item_meta->display( true, true );
if ($show_download_links) :
if ($_product->exists) :
if ($_product->is_downloadable()) :
$return .= PHP_EOL . ' - ' . $this->get_downloadable_file_url( $item['id'], $item['variation_id'] ) . '';
endif;
endif;
endif;
$return .= PHP_EOL;
endforeach;
return $return;
}
/** 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;
$item_meta = &new order_item_meta( $item['item_meta'] );
$variation = '
' . $item_meta->display( true, true ) . '';
if ($show_download_links) :
if ($_product->exists) :
if ($_product->is_downloadable()) :
$file = '
'.__('Download:', 'woothemes').' ' . $this->get_downloadable_file_url( $item['id'], $item['variation_id'] ) . '';
endif;
endif;
endif;
$return .= '