_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 field data
$this->order_key = (string) get_post_meta( $this->id, 'order_key', true );
$this->user_id = (int) get_post_meta( $this->id, 'customer_user', true );
$this->items = (array) get_post_meta( $this->id, 'order_items', true );
$this->order_data = (array) maybe_unserialize( get_post_meta( $this->id, 'order_data', true ) );
$this->billing_first_name = (string) $this->get_value_from_data('billing_first_name');
$this->billing_last_name = (string) $this->get_value_from_data('billing_last_name');
$this->billing_company = (string) $this->get_value_from_data('billing_company');
$this->billing_address_1 = (string) $this->get_value_from_data('billing_address_1');
$this->billing_address_2 = (string) $this->get_value_from_data('billing_address_2');
$this->billing_city = (string) $this->get_value_from_data('billing_city');
$this->billing_postcode = (string) $this->get_value_from_data('billing_postcode');
$this->billing_country = (string) $this->get_value_from_data('billing_country');
$this->billing_state = (string) $this->get_value_from_data('billing_state');
$this->billing_email = (string) $this->get_value_from_data('billing_email');
$this->billing_phone = (string) $this->get_value_from_data('billing_phone');
$this->shipping_first_name = (string) $this->get_value_from_data('shipping_first_name');
$this->shipping_last_name = (string) $this->get_value_from_data('shipping_last_name');
$this->shipping_company = (string) $this->get_value_from_data('shipping_company');
$this->shipping_address_1 = (string) $this->get_value_from_data('shipping_address_1');
$this->shipping_address_2 = (string) $this->get_value_from_data('shipping_address_2');
$this->shipping_city = (string) $this->get_value_from_data('shipping_city');
$this->shipping_postcode = (string) $this->get_value_from_data('shipping_postcode');
$this->shipping_country = (string) $this->get_value_from_data('shipping_country');
$this->shipping_state = (string) $this->get_value_from_data('shipping_state');
$this->shipping_method = (string) $this->get_value_from_data('shipping_method');
$this->payment_method = (string) $this->get_value_from_data('payment_method');
$this->order_subtotal = (string) $this->get_value_from_data('order_subtotal');
$this->order_shipping = (string) $this->get_value_from_data('order_shipping');
$this->order_discount = (string) $this->get_value_from_data('order_discount');
$this->order_tax = (string) $this->get_value_from_data('order_tax');
$this->order_shipping_tax = (string) $this->get_value_from_data('order_shipping_tax');
$this->order_total = (string) $this->get_value_from_data('order_total');
// 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;
}
function get_value_from_data( $key ) {
if (isset($this->order_data[$key])) return $this->order_data[$key]; else return '';
}
/** 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 = '