_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='' ) {
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 ) {
// 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 );
$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' => '',
'order_tax' => '',
'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;
$address = array_map('trim', array(
$this->billing_address_1,
$this->billing_address_2,
$this->billing_city,
$this->billing_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;
$address = array_map('trim', array(
$this->shipping_address_1,
$this->shipping_address_2,
$this->shipping_city,
$this->shipping_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 subtotal */
function get_subtotal_to_display() {
$subtotal = woocommerce_price($this->order_subtotal);
if ($this->order_tax>0) :
$subtotal .= __(' (ex. tax)', 'woothemes');
endif;
return $subtotal;
}
/** Gets shipping */
function get_shipping_to_display() {
if ($this->order_shipping > 0) :
$shipping = woocommerce_price($this->order_shipping);
if ($this->order_shipping_tax > 0) :
$shipping .= sprintf(__(' (ex. tax) via %s', 'woothemes'), ucwords($this->shipping_method));
endif;
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 )));
if (isset($_product->variation_data)) :
$return .= PHP_EOL . woocommerce_get_formatted_variation( $_product->variation_data, true );
endif;
if ($show_download_links) :
if ($_product->exists) :
if ($_product->is_type('downloadable')) :
$return .= PHP_EOL . ' - ' . $this->get_downloadable_file_url( $item['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;
if (isset($_product->variation_data)) :
$variation = '
' . woocommerce_get_formatted_variation( $_product->variation_data, true );
endif;
if ($show_download_links) :
if ($_product->exists) :
if ($_product->is_type('downloadable')) :
$file = '
' . $this->get_downloadable_file_url( $item['id'] ) . '';
endif;
endif;
endif;
$return = '