_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->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' => '',
'payment_method' => '',
'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
$formatted_address = array();
$country = ($this->billing_country && isset($woocommerce->countries->countries[$this->billing_country])) ? $woocommerce->countries->countries[$this->billing_country] : $this->billing_country;
$state = ($this->billing_country && $this->billing_state && isset($woocommerce->countries->states[$this->billing_country][$this->billing_state])) ? $woocommerce->countries->states[$this->billing_country][$this->billing_state] : $this->billing_state;
$address = array_map('trim', array(
$this->billing_address_1,
$this->billing_address_2,
$this->billing_city,
$state,
$this->billing_postcode,
$country
));
foreach ($address as $part) if (!empty($part)) $formatted_address[] = $part;
$this->formatted_billing_address = implode(', ', $formatted_address);
if ($this->shipping_address_1) :
$formatted_address = array();
$country = ($this->shipping_country && isset($woocommerce->countries->countries[$this->shipping_country])) ? $woocommerce->countries->countries[$this->shipping_country] : $this->shipping_country;
$state = ($this->shipping_country && $this->shipping_state && isset($woocommerce->countries->states[$this->shipping_country][$this->shipping_state])) ? $woocommerce->countries->states[$this->shipping_country][$this->shipping_state] : $this->shipping_state;
$address = array_map('trim', array(
$this->shipping_address_1,
$this->shipping_address_2,
$this->shipping_city,
$state,
$this->shipping_postcode,
$country
));
foreach ($address as $part) if (!empty($part)) $formatted_address[] = $part;
$this->formatted_shipping_address = implode(', ', $formatted_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;
}
/** 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) :
$subtotal += round(($item['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));
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 = '
' . $this->get_downloadable_file_url( $item['id'], $item['variation_id'] ) . '';
endif;
endif;
endif;
$return .= '