Merge pull request #5678 from coenjacobs/order-factory-refactor

Order factory refactor implementation (take two)
This commit is contained in:
Mike Jolley 2014-07-03 15:14:43 +01:00
commit abe0d0004a
35 changed files with 2238 additions and 2129 deletions

File diff suppressed because it is too large Load Diff

View File

@ -100,7 +100,7 @@ abstract class WC_Payment_Gateway extends WC_Settings_API {
// Gets order total from "pay for order" page. // Gets order total from "pay for order" page.
if ( 0 < $order_id ) { if ( 0 < $order_id ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$total = (float) $order->get_total(); $total = (float) $order->get_total();
// Gets order total from cart/checkout. // Gets order total from cart/checkout.

View File

@ -439,7 +439,7 @@ class WC_Admin_Post_Types {
global $post, $woocommerce, $the_order; global $post, $woocommerce, $the_order;
if ( empty( $the_order ) || $the_order->id != $post->ID ) { if ( empty( $the_order ) || $the_order->id != $post->ID ) {
$the_order = new WC_Order( $post->ID ); $the_order = get_order( $post->ID );
} }
switch ( $column ) { switch ( $column ) {
@ -1180,7 +1180,7 @@ class WC_Admin_Post_Types {
$post_ids = array_map( 'absint', (array) $_REQUEST['post'] ); $post_ids = array_map( 'absint', (array) $_REQUEST['post'] );
foreach ( $post_ids as $post_id ) { foreach ( $post_ids as $post_id ) {
$order = new WC_Order( $post_id ); $order = get_order( $post_id );
$order->update_status( $new_status, __( 'Order status changed by bulk edit:', 'woocommerce' ) ); $order->update_status( $new_status, __( 'Order status changed by bulk edit:', 'woocommerce' ) );
$changed++; $changed++;
} }
@ -2015,7 +2015,7 @@ class WC_Admin_Post_Types {
$existing_permissions = $wpdb->get_results( $wpdb->prepare( "SELECT * from {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE product_id = %d GROUP BY order_id", $product_id ) ); $existing_permissions = $wpdb->get_results( $wpdb->prepare( "SELECT * from {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE product_id = %d GROUP BY order_id", $product_id ) );
foreach ( $existing_permissions as $existing_permission ) { foreach ( $existing_permissions as $existing_permission ) {
$order = new WC_Order( $existing_permission->order_id ); $order = get_order( $existing_permission->order_id );
if ( $order->id ) { if ( $order->id ) {
// Remove permissions // Remove permissions

View File

@ -21,7 +21,7 @@ class WC_Meta_Box_Order_Actions {
global $theorder; global $theorder;
if ( ! is_object( $theorder ) ) if ( ! is_object( $theorder ) )
$theorder = new WC_Order( $post->ID ); $theorder = get_order( $post->ID );
$order = $theorder; $order = $theorder;
?> ?>
@ -82,7 +82,7 @@ class WC_Meta_Box_Order_Actions {
*/ */
public static function save( $post_id, $post ) { public static function save( $post_id, $post ) {
// Order data saved, now get it so we can manipulate status // Order data saved, now get it so we can manipulate status
$order = new WC_Order( $post_id ); $order = get_order( $post_id );
// Handle button actions // Handle button actions
if ( ! empty( $_POST['wc_order_action'] ) ) { if ( ! empty( $_POST['wc_order_action'] ) ) {

View File

@ -133,7 +133,7 @@ class WC_Meta_Box_Order_Data {
global $theorder; global $theorder;
if ( ! is_object( $theorder ) ) { if ( ! is_object( $theorder ) ) {
$theorder = new WC_Order( $post->ID ); $theorder = get_order( $post->ID );
} }
$order = $theorder; $order = $theorder;
@ -441,7 +441,7 @@ class WC_Meta_Box_Order_Data {
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_date = %s, post_date_gmt = %s WHERE ID = %s", $date, get_gmt_from_date( $date ), $post_id ) ); $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_date = %s, post_date_gmt = %s WHERE ID = %s", $date, get_gmt_from_date( $date ), $post_id ) );
// Order data saved, now get it so we can manipulate status // Order data saved, now get it so we can manipulate status
$order = new WC_Order( $post_id ); $order = get_order( $post_id );
// Order status // Order status
$order->update_status( $_POST['order_status'] ); $order->update_status( $_POST['order_status'] );

View File

@ -21,7 +21,7 @@ class WC_Meta_Box_Order_Items {
global $thepostid, $theorder; global $thepostid, $theorder;
if ( ! is_object( $theorder ) ) if ( ! is_object( $theorder ) )
$theorder = new WC_Order( $thepostid ); $theorder = get_order( $thepostid );
$order = $theorder; $order = $theorder;
?> ?>

View File

@ -21,7 +21,7 @@ class WC_Meta_Box_Order_Totals {
global $theorder, $wpdb, $post; global $theorder, $wpdb, $post;
if ( ! is_object( $theorder ) ) if ( ! is_object( $theorder ) )
$theorder = new WC_Order( $post->ID ); $theorder = get_order( $post->ID );
$order = $theorder; $order = $theorder;

View File

@ -163,7 +163,7 @@ class WC_Report_Customer_List extends WP_List_Table {
) ); ) );
if ( $order_ids ) { if ( $order_ids ) {
$order = new WC_Order( $order_ids[0] ); $order = get_order( $order_ids[0] );
echo '<a href="' . admin_url( 'post.php?post=' . $order->id . '&action=edit' ) . '">' . $order->get_order_number() . '</a> &ndash; ' . date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); echo '<a href="' . admin_url( 'post.php?post=' . $order->id . '&action=edit' ) . '">' . $order->get_order_number() . '</a> &ndash; ' . date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) );
} else echo '-'; } else echo '-';

View File

@ -106,7 +106,7 @@ class WC_API_Orders extends WC_API_Resource {
if ( is_wp_error( $id ) ) if ( is_wp_error( $id ) )
return $id; return $id;
$order = new WC_Order( $id ); $order = get_order( $id );
$order_post = get_post( $id ); $order_post = get_post( $id );
@ -275,7 +275,7 @@ class WC_API_Orders extends WC_API_Resource {
if ( is_wp_error( $id ) ) if ( is_wp_error( $id ) )
return $id; return $id;
$order = new WC_Order( $id ); $order = get_order( $id );
if ( ! empty( $data['status'] ) ) { if ( ! empty( $data['status'] ) ) {

View File

@ -351,7 +351,7 @@ class WC_AJAX {
die(); die();
} }
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$order->update_status( 'completed' ); $order->update_status( 'completed' );
wp_safe_redirect( wp_get_referer() ); wp_safe_redirect( wp_get_referer() );
@ -376,7 +376,7 @@ class WC_AJAX {
die(); die();
} }
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$order->update_status( 'processing' ); $order->update_status( 'processing' );
wp_safe_redirect( wp_get_referer() ); wp_safe_redirect( wp_get_referer() );
@ -847,7 +847,7 @@ class WC_AJAX {
$product_ids = $_POST['product_ids']; $product_ids = $_POST['product_ids'];
$loop = intval( $_POST['loop'] ); $loop = intval( $_POST['loop'] );
$file_counter = 0; $file_counter = 0;
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( ! is_array( $product_ids ) ) { if ( ! is_array( $product_ids ) ) {
$product_ids = array( $product_ids ); $product_ids = array( $product_ids );
@ -936,7 +936,7 @@ class WC_AJAX {
} }
$_product = get_product( $post->ID ); $_product = get_product( $post->ID );
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$class = 'new_row'; $class = 'new_row';
// Set values // Set values
@ -996,7 +996,7 @@ class WC_AJAX {
check_ajax_referer( 'order-item', 'security' ); check_ajax_referer( 'order-item', 'security' );
$order_id = absint( $_POST['order_id'] ); $order_id = absint( $_POST['order_id'] );
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
// Add line item // Add line item
$item_id = wc_add_order_item( $order_id, array( $item_id = wc_add_order_item( $order_id, array(
@ -1043,7 +1043,7 @@ class WC_AJAX {
$order_id = absint( $_POST['order_id'] ); $order_id = absint( $_POST['order_id'] );
$order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array(); $order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array();
$order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array(); $order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array();
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$order_items = $order->get_items(); $order_items = $order->get_items();
$return = array(); $return = array();
@ -1089,7 +1089,7 @@ class WC_AJAX {
$order_id = absint( $_POST['order_id'] ); $order_id = absint( $_POST['order_id'] );
$order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array(); $order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array();
$order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array(); $order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array();
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$order_items = $order->get_items(); $order_items = $order->get_items();
$return = array(); $return = array();
@ -1169,7 +1169,7 @@ class WC_AJAX {
$taxes = $tax_rows = $item_taxes = $shipping_taxes = array(); $taxes = $tax_rows = $item_taxes = $shipping_taxes = array();
$order_id = absint( $_POST['order_id'] ); $order_id = absint( $_POST['order_id'] );
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$country = strtoupper( esc_attr( $_POST['country'] ) ); $country = strtoupper( esc_attr( $_POST['country'] ) );
$state = strtoupper( esc_attr( $_POST['state'] ) ); $state = strtoupper( esc_attr( $_POST['state'] ) );
$postcode = strtoupper( esc_attr( $_POST['postcode'] ) ); $postcode = strtoupper( esc_attr( $_POST['postcode'] ) );
@ -1285,7 +1285,7 @@ class WC_AJAX {
$is_customer_note = $note_type == 'customer' ? 1 : 0; $is_customer_note = $note_type == 'customer' ? 1 : 0;
if ( $post_id > 0 ) { if ( $post_id > 0 ) {
$order = new WC_Order( $post_id ); $order = get_order( $post_id );
$comment_id = $order->add_order_note( $note, $is_customer_note ); $comment_id = $order->add_order_note( $note, $is_customer_note );
echo '<li rel="' . esc_attr( $comment_id ) . '" class="note '; echo '<li rel="' . esc_attr( $comment_id ) . '" class="note ';

View File

@ -629,7 +629,7 @@ class WC_Checkout {
} else { } else {
if ( empty( $order ) ) if ( empty( $order ) )
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
// No payment was required for order // No payment was required for order
$order->payment_complete(); $order->payment_complete();

View File

@ -89,7 +89,7 @@ class WC_Download_Handler {
} }
if ( $order_id ) { if ( $order_id ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( ! $order->is_download_permitted() || $order->post_status != 'publish' ) { if ( ! $order->is_download_permitted() || $order->post_status != 'publish' ) {
wp_die( __( 'Invalid order.', 'woocommerce' ) . ' <a href="' . esc_url( home_url() ) . '" class="wc-forward">' . __( 'Go to homepage', 'woocommerce' ) . '</a>', '', array( 'response' => 404 ) ); wp_die( __( 'Invalid order.', 'woocommerce' ) . ' <a href="' . esc_url( home_url() ) . '" class="wc-forward">' . __( 'Go to homepage', 'woocommerce' ) . '</a>', '', array( 'response' => 404 ) );

View File

@ -412,7 +412,7 @@ class WC_Emails {
else else
$title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . $sku; $title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . $sku;
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$message = sprintf(__( '%s units of %s have been backordered in order %s.', 'woocommerce' ), $quantity, $title, $order->get_order_number() ); $message = sprintf(__( '%s units of %s have been backordered in order %s.', 'woocommerce' ), $quantity, $title, $order->get_order_number() );
// CC, BCC, additional headers // CC, BCC, additional headers

View File

@ -264,7 +264,7 @@ class WC_Form_Handler {
// Pay for existing order // Pay for existing order
$order_key = $_GET['key']; $order_key = $_GET['key'];
$order_id = absint( $wp->query_vars['order-pay'] ); $order_id = absint( $wp->query_vars['order-pay'] );
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order ); $valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order );
@ -462,7 +462,7 @@ class WC_Form_Handler {
WC()->cart->empty_cart(); WC()->cart->empty_cart();
// Load the previous order - Stop if the order does not exist // Load the previous order - Stop if the order does not exist
$order = new WC_Order( absint( $_GET['order_again'] ) ); $order = get_order( absint( $_GET['order_again'] ) );
if ( empty( $order->id ) ) { if ( empty( $order->id ) ) {
return; return;
@ -519,7 +519,7 @@ class WC_Form_Handler {
$order_key = $_GET['order']; $order_key = $_GET['order'];
$order_id = absint( $_GET['order_id'] ); $order_id = absint( $_GET['order_id'] );
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$user_can_cancel = current_user_can( 'cancel_order', $order_id ); $user_can_cancel = current_user_can( 'cancel_order', $order_id );
$order_can_cancel = $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ) ) ); $order_can_cancel = $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ) ) );
$redirect = $_GET['redirect']; $redirect = $_GET['redirect'];

View File

@ -0,0 +1,57 @@
<?php
/**
* Order Factory Class
*
* The WooCommerce order factory creating the right order objects
*
* @class WC_Order_Factory
* @version 2.2.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
class WC_Order_Factory {
/**
* get_order function.
*
* @access public
* @param bool $the_order (default: false)
* @param array $args (default: array())
* @return WC_Order
*/
public function get_order( $the_order = false, $args = array() ) {
global $post;
if ( false === $the_order ) {
$the_order = $post;
} elseif ( is_numeric( $the_order ) ) {
$the_order = get_post( $the_order );
}
if ( ! $the_order )
return false;
if ( is_object ( $the_order ) ) {
$order_id = absint( $the_order->ID );
$post_type = $the_order->post_type;
}
if ( 'shop_order' == $post_type ) {
$classname = 'WC_Order';
$order_type = 'simple';
} else {
$classname = false;
$order_type = false;
}
// Filter classname so that the class can be overridden if extended.
$classname = apply_filters( 'woocommerce_order_class', $classname, $order_type, $post_type, $order_id );
if ( ! class_exists( $classname ) )
$classname = 'WC_Order';
return new $classname( $the_order, $args );
}
}

File diff suppressed because it is too large Load Diff

View File

@ -52,7 +52,7 @@ class WC_Email_Customer_Completed_Order extends WC_Email {
function trigger( $order_id ) { function trigger( $order_id ) {
if ( $order_id ) { if ( $order_id ) {
$this->object = new WC_Order( $order_id ); $this->object = get_order( $order_id );
$this->recipient = $this->object->billing_email; $this->recipient = $this->object->billing_email;
$this->find['order-date'] = '{order_date}'; $this->find['order-date'] = '{order_date}';

View File

@ -54,7 +54,7 @@ class WC_Email_Customer_Invoice extends WC_Email {
function trigger( $order ) { function trigger( $order ) {
if ( ! is_object( $order ) ) { if ( ! is_object( $order ) ) {
$order = new WC_Order( absint( $order ) ); $order = get_order( absint( $order ) );
} }
if ( $order ) { if ( $order ) {

View File

@ -63,7 +63,7 @@ class WC_Email_Customer_Note extends WC_Email {
extract( $args ); extract( $args );
$this->object = new WC_Order( $order_id ); $this->object = get_order( $order_id );
$this->recipient = $this->object->billing_email; $this->recipient = $this->object->billing_email;
$this->customer_note = $customer_note; $this->customer_note = $customer_note;

View File

@ -49,7 +49,7 @@ class WC_Email_Customer_Processing_Order extends WC_Email {
function trigger( $order_id ) { function trigger( $order_id ) {
if ( $order_id ) { if ( $order_id ) {
$this->object = new WC_Order( $order_id ); $this->object = get_order( $order_id );
$this->recipient = $this->object->billing_email; $this->recipient = $this->object->billing_email;
$this->find['order-date'] = '{order_date}'; $this->find['order-date'] = '{order_date}';

View File

@ -59,7 +59,7 @@ class WC_Email_New_Order extends WC_Email {
function trigger( $order_id ) { function trigger( $order_id ) {
if ( $order_id ) { if ( $order_id ) {
$this->object = new WC_Order( $order_id ); $this->object = get_order( $order_id );
$this->find['order-date'] = '{order_date}'; $this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}'; $this->find['order-number'] = '{order_number}';

View File

@ -291,7 +291,7 @@ class WC_Gateway_BACS extends WC_Payment_Gateway {
*/ */
public function process_payment( $order_id ) { public function process_payment( $order_id ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
// Mark as on-hold (we're awaiting the payment) // Mark as on-hold (we're awaiting the payment)
$order->update_status( 'on-hold', __( 'Awaiting BACS payment', 'woocommerce' ) ); $order->update_status( 'on-hold', __( 'Awaiting BACS payment', 'woocommerce' ) );

View File

@ -108,7 +108,7 @@ class WC_Gateway_Cheque extends WC_Payment_Gateway {
*/ */
public function process_payment( $order_id ) { public function process_payment( $order_id ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
// Mark as on-hold (we're awaiting the cheque) // Mark as on-hold (we're awaiting the cheque)
$order->update_status( 'on-hold', __( 'Awaiting cheque payment', 'woocommerce' ) ); $order->update_status( 'on-hold', __( 'Awaiting cheque payment', 'woocommerce' ) );

View File

@ -120,7 +120,7 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
if ( is_page( wc_get_page_id( 'checkout' ) ) && 0 < get_query_var( 'order-pay' ) ) { if ( is_page( wc_get_page_id( 'checkout' ) ) && 0 < get_query_var( 'order-pay' ) ) {
$order_id = absint( get_query_var( 'order-pay' ) ); $order_id = absint( get_query_var( 'order-pay' ) );
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
// Test if order needs shipping. // Test if order needs shipping.
$needs_shipping = false; $needs_shipping = false;
@ -198,7 +198,7 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
*/ */
public function process_payment( $order_id ) { public function process_payment( $order_id ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
// Mark as processing (payment won't be taken until delivery) // Mark as processing (payment won't be taken until delivery)
$order->update_status( 'processing', __( 'Payment to be made upon delivery.', 'woocommerce' ) ); $order->update_status( 'processing', __( 'Payment to be made upon delivery.', 'woocommerce' ) );

View File

@ -88,7 +88,7 @@ class WC_Gateway_Mijireh extends WC_Payment_Gateway {
try { try {
$mj_order = new Mijireh_Order( esc_attr( $_GET['order_number'] ) ); $mj_order = new Mijireh_Order( esc_attr( $_GET['order_number'] ) );
$wc_order_id = $mj_order->get_meta_value( 'wc_order_id' ); $wc_order_id = $mj_order->get_meta_value( 'wc_order_id' );
$wc_order = new WC_Order( absint( $wc_order_id ) ); $wc_order = get_order( absint( $wc_order_id ) );
// Mark order complete // Mark order complete
$wc_order->payment_complete(); $wc_order->payment_complete();
@ -188,7 +188,7 @@ class WC_Gateway_Mijireh extends WC_Payment_Gateway {
$this->init_mijireh(); $this->init_mijireh();
$mj_order = new Mijireh_Order(); $mj_order = new Mijireh_Order();
$wc_order = new WC_Order( $order_id ); $wc_order = get_order( $order_id );
// Avoid rounding issues altogether by sending the order as one lump // Avoid rounding issues altogether by sending the order as one lump
if ( get_option( 'woocommerce_prices_include_tax' ) == 'yes' ) { if ( get_option( 'woocommerce_prices_include_tax' ) == 'yes' ) {

View File

@ -445,7 +445,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
*/ */
function generate_paypal_form( $order_id ) { function generate_paypal_form( $order_id ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( 'yes' == $this->testmode ) { if ( 'yes' == $this->testmode ) {
$paypal_adr = $this->testurl . '?test_ipn=1&'; $paypal_adr = $this->testurl . '?test_ipn=1&';
@ -506,7 +506,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
*/ */
function process_payment( $order_id ) { function process_payment( $order_id ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( ! $this->form_submission_method ) { if ( ! $this->form_submission_method ) {
@ -912,12 +912,12 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
list( $order_id, $order_key ) = $custom; list( $order_id, $order_key ) = $custom;
} }
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( ! isset( $order->id ) ) { if ( ! isset( $order->id ) ) {
// We have an invalid $order_id, probably because invoice_prefix has changed // We have an invalid $order_id, probably because invoice_prefix has changed
$order_id = wc_get_order_id_by_order_key( $order_key ); $order_id = wc_get_order_id_by_order_key( $order_key );
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
} }
// Validate key // Validate key

View File

@ -44,7 +44,7 @@ class WC_Shortcode_Checkout {
// Get the order to work out what we are showing // Get the order to work out what we are showing
$order_id = absint( $_GET['order'] ); $order_id = absint( $_GET['order'] );
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( $order->has_status( 'pending' ) ) { if ( $order->has_status( 'pending' ) ) {
$wp->query_vars['order-pay'] = absint( $_GET['order'] ); $wp->query_vars['order-pay'] = absint( $_GET['order'] );
@ -85,7 +85,7 @@ class WC_Shortcode_Checkout {
// Pay for existing order // Pay for existing order
$order_key = $_GET[ 'key' ]; $order_key = $_GET[ 'key' ];
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order ); $valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order );
if ( ! current_user_can( 'pay_for_order', $order_id ) ) { if ( ! current_user_can( 'pay_for_order', $order_id ) ) {
@ -119,7 +119,7 @@ class WC_Shortcode_Checkout {
// Pay for order after checkout step // Pay for order after checkout step
$order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : ''; $order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
$valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order ); $valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order );
if ( $order->id == $order_id && $order->order_key == $order_key ) { if ( $order->id == $order_id && $order->order_key == $order_key ) {
@ -186,7 +186,7 @@ class WC_Shortcode_Checkout {
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) ); $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) { if ( $order_id > 0 ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( $order->order_key != $order_key ) if ( $order->order_key != $order_key )
unset( $order ); unset( $order );
} }

View File

@ -105,7 +105,7 @@ class WC_Shortcode_My_Account {
private static function view_order( $order_id ) { private static function view_order( $order_id ) {
$user_id = get_current_user_id(); $user_id = get_current_user_id();
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( ! current_user_can( 'view_order', $order_id ) ) { if ( ! current_user_can( 'view_order', $order_id ) ) {
echo '<div class="woocommerce-error">' . __( 'Invalid order.', 'woocommerce' ) . ' <a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ).'" class="wc-forward">'. __( 'My Account', 'woocommerce' ) .'</a>' . '</div>'; echo '<div class="woocommerce-error">' . __( 'Invalid order.', 'woocommerce' ) . ' <a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ).'" class="wc-forward">'. __( 'My Account', 'woocommerce' ) .'</a>' . '</div>';
@ -118,7 +118,7 @@ class WC_Shortcode_My_Account {
wc_get_template( 'myaccount/view-order.php', array( wc_get_template( 'myaccount/view-order.php', array(
'status' => $status, // @deprecated 2.2 'status' => $status, // @deprecated 2.2
'order' => new WC_Order( $order_id ), 'order' => get_order( $order_id ),
'order_id' => $order_id 'order_id' => $order_id
) ); ) );
} }

View File

@ -59,7 +59,7 @@ class WC_Shortcode_Order_Tracking {
} else { } else {
$order = new WC_Order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) ); $order = get_order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) );
if ( $order->id && $order_email ) { if ( $order->id && $order_email ) {

View File

@ -119,7 +119,7 @@ function wc_clear_cart_after_payment() {
$order_key = ''; $order_key = '';
if ( $order_id > 0 ) { if ( $order_id > 0 ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( $order->order_key == $order_key ) { if ( $order->order_key == $order_key ) {
WC()->cart->empty_cart(); WC()->cart->empty_cart();
@ -130,7 +130,7 @@ function wc_clear_cart_after_payment() {
if ( WC()->session->order_awaiting_payment > 0 ) { if ( WC()->session->order_awaiting_payment > 0 ) {
$order = new WC_Order( WC()->session->order_awaiting_payment ); $order = get_order( WC()->session->order_awaiting_payment );
if ( $order->id > 0 ) { if ( $order->id > 0 ) {
// If the order has not failed, or is not pending, the order must have gone through // If the order has not failed, or is not pending, the order must have gone through

View File

@ -31,6 +31,17 @@ function wc_get_order_statuses() {
return apply_filters( 'wc_order_statuses', $order_statuses ); return apply_filters( 'wc_order_statuses', $order_statuses );
} }
/**
* Main function for returning orders, uses the WC_Order_Factory class.
*
* @param mixed $the_order Post object or post ID of the order.
* @param array $args (default: array()) Contains all arguments to be used to get this order.
* @return WC_Order
*/
function get_order( $the_order = false, $args = array() ) {
return WC()->order_factory->get_order( $the_order, $args );
}
/** /**
* Get the nice name for an orer status * Get the nice name for an orer status
* @param string $status * @param string $status
@ -135,7 +146,7 @@ function wc_downloadable_product_permissions( $order_id ) {
return; // Only do this once return; // Only do this once
} }
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( $order->has_status( 'processing' ) && get_option( 'woocommerce_downloads_grant_access_after_payment' ) == 'no' ) { if ( $order->has_status( 'processing' ) && get_option( 'woocommerce_downloads_grant_access_after_payment' ) == 'no' ) {
return; return;
@ -312,7 +323,7 @@ function wc_cancel_unpaid_orders() {
if ( $unpaid_orders ) { if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) { foreach ( $unpaid_orders as $unpaid_order ) {
$order = new WC_Order( $unpaid_order ); $order = get_order( $unpaid_order );
if ( apply_filters( 'woocommerce_cancel_unpaid_order', true, $order ) ) if ( apply_filters( 'woocommerce_cancel_unpaid_order', true, $order ) )
$order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) ); $order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );

View File

@ -198,7 +198,7 @@ function wc_update_new_customer_past_orders( $customer_id ) {
*/ */
function wc_paying_customer( $order_id ) { function wc_paying_customer( $order_id ) {
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( $order->user_id > 0 ) { if ( $order->user_id > 0 ) {
update_user_meta( $order->user_id, 'paying_customer', 1 ); update_user_meta( $order->user_id, 'paying_customer', 1 );
@ -278,7 +278,7 @@ function wc_customer_has_capability( $allcaps, $caps, $args ) {
switch ( $caps[0] ) { switch ( $caps[0] ) {
case 'view_order' : case 'view_order' :
$user_id = $args[1]; $user_id = $args[1];
$order = new WC_Order( $args[2] ); $order = get_order( $args[2] );
if ( $user_id == $order->user_id ) { if ( $user_id == $order->user_id ) {
$allcaps['view_order'] = true; $allcaps['view_order'] = true;
@ -295,14 +295,14 @@ function wc_customer_has_capability( $allcaps, $caps, $args ) {
break; break;
} }
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
if ( $user_id == $order->user_id || empty( $order->user_id ) ) { if ( $user_id == $order->user_id || empty( $order->user_id ) ) {
$allcaps['pay_for_order'] = true; $allcaps['pay_for_order'] = true;
} }
break; break;
case 'order_again' : case 'order_again' :
$user_id = $args[1]; $user_id = $args[1];
$order = new WC_Order( $args[2] ); $order = get_order( $args[2] );
if ( $user_id == $order->user_id ) { if ( $user_id == $order->user_id ) {
$allcaps['order_again'] = true; $allcaps['order_again'] = true;
@ -310,7 +310,7 @@ function wc_customer_has_capability( $allcaps, $caps, $args ) {
break; break;
case 'cancel_order' : case 'cancel_order' :
$user_id = $args[1]; $user_id = $args[1];
$order = new WC_Order( $args[2] ); $order = get_order( $args[2] );
if ( $user_id == $order->user_id ) { if ( $user_id == $order->user_id ) {
$allcaps['cancel_order'] = true; $allcaps['cancel_order'] = true;
@ -418,7 +418,7 @@ function wc_get_customer_available_downloads( $customer_id ) {
foreach ( $results as $result ) { foreach ( $results as $result ) {
if ( ! $order || $order->id != $result->order_id ) { if ( ! $order || $order->id != $result->order_id ) {
// new order // new order
$order = new WC_Order( $result->order_id ); $order = get_order( $result->order_id );
$_product = null; $_product = null;
} }

View File

@ -39,7 +39,7 @@ if ( $customer_orders ) : ?>
<tbody><?php <tbody><?php
foreach ( $customer_orders as $customer_order ) { foreach ( $customer_orders as $customer_order ) {
$order = new WC_Order(); $order = get_order();
$order->populate( $customer_order ); $order->populate( $customer_order );
$item_count = $order->get_item_count(); $item_count = $order->get_item_count();

View File

@ -11,7 +11,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce; global $woocommerce;
$order = new WC_Order( $order_id ); $order = get_order( $order_id );
?> ?>
<h2><?php _e( 'Order Details', 'woocommerce' ); ?></h2> <h2><?php _e( 'Order Details', 'woocommerce' ); ?></h2>
<table class="shop_table order_details"> <table class="shop_table order_details">

View File

@ -76,6 +76,11 @@ final class WooCommerce {
*/ */
public $customer = null; public $customer = null;
/**
* @var WC_Order_Factory $order_factory
*/
public $order_factory = null;
/** /**
* Main WooCommerce Instance * Main WooCommerce Instance
* *
@ -279,6 +284,7 @@ final class WooCommerce {
// Include abstract classes // Include abstract classes
include_once( 'includes/abstracts/abstract-wc-product.php' ); // Products include_once( 'includes/abstracts/abstract-wc-product.php' ); // Products
include_once( 'includes/abstracts/abstract-wc-order.php' ); // Orders
include_once( 'includes/abstracts/abstract-wc-settings-api.php' ); // Settings API (for gateways, shipping, and integrations) include_once( 'includes/abstracts/abstract-wc-settings-api.php' ); // Settings API (for gateways, shipping, and integrations)
include_once( 'includes/abstracts/abstract-wc-shipping-method.php' ); // A Shipping method include_once( 'includes/abstracts/abstract-wc-shipping-method.php' ); // A Shipping method
include_once( 'includes/abstracts/abstract-wc-payment-gateway.php' ); // A Payment gateway include_once( 'includes/abstracts/abstract-wc-payment-gateway.php' ); // A Payment gateway
@ -362,6 +368,7 @@ final class WooCommerce {
// Load class instances // Load class instances
$this->product_factory = new WC_Product_Factory(); // Product Factory to create new product instances $this->product_factory = new WC_Product_Factory(); // Product Factory to create new product instances
$this->order_factory = new WC_Order_Factory(); // Order Factory to create new order instances
$this->countries = new WC_Countries(); // Countries class $this->countries = new WC_Countries(); // Countries class
$this->integrations = new WC_Integrations(); // Integrations class $this->integrations = new WC_Integrations(); // Integrations class