Merge pull request #13120 from woocommerce/fix/factory-type

Move post type logic from product and order factories.
This commit is contained in:
Mike Jolley 2017-02-09 11:02:57 +00:00 committed by GitHub
commit 639b805a24
8 changed files with 69 additions and 17 deletions

View File

@ -29,16 +29,15 @@ class WC_Order_Factory {
return false;
}
$post_type = get_post_type( $order_id );
if ( $order_type = wc_get_order_type( $post_type ) ) {
$classname = $order_type['class_name'];
$order_type = WC_Data_Store::load( 'order' )->get_order_type( $order_id );
if ( $order_type_data = wc_get_order_type( $order_type ) ) {
$classname = $order_type_data['class_name'];
} else {
$classname = false;
}
// Filter classname so that the class can be overridden if extended.
$classname = apply_filters( 'woocommerce_order_class', $classname, $post_type, $order_id );
$classname = apply_filters( 'woocommerce_order_class', $classname, $order_type, $order_id );
if ( ! class_exists( $classname ) ) {
return false;
@ -60,8 +59,7 @@ class WC_Order_Factory {
global $wpdb;
if ( is_numeric( $item_id ) ) {
$item_data = $wpdb->get_row( $wpdb->prepare( "SELECT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item_id ) );
$item_type = $item_data->order_item_type;
$item_type = WC_Data_Store::load( 'order-item' )->get_order_item_type( $item_id );
$id = $item_id;
} elseif ( $item_id instanceof WC_Order_Item ) {
$item_type = $item_id->get_type();

View File

@ -86,16 +86,7 @@ class WC_Product_Factory {
// Allow the overriding of the lookup in this function. Return the product type here.
$override = apply_filters( 'woocommerce_product_type_query', false, $product_id );
if ( ! $override ) {
$post_type = get_post_type( $product_id );
if ( 'product_variation' === $post_type ) {
return 'variation';
} elseif ( 'product' === $post_type ) {
$terms = get_the_terms( $product_id, 'product_type' );
return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
} else {
return false;
}
return WC_Data_Store::load( 'product' )->get_product_type( $product_id );
} else {
return $override;
}

View File

@ -585,4 +585,15 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
$order_id = WC_Order_Factory::get_order_id( $order );
update_post_meta( $order_id, '_order_stock_reduced', wc_bool_to_string( $set ) );
}
/**
* Get the order type based on Order ID.
*
* @since 2.7.0
* @param int $order_id
* @return string
*/
public function get_order_type( $order_id ) {
return get_post_type( $order_id );
}
}

View File

@ -134,4 +134,16 @@ class WC_Order_Item_Data_Store implements WC_Order_Item_Data_Store_Interface {
) );
}
/**
* Get the order item type based on Item ID.
*
* @since 2.7.0
* @param int $item_id
* @return string
*/
public function get_order_item_type( $item_id ) {
global $wpdb;
$item_data = $wpdb->get_row( $wpdb->prepare( "SELECT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item_id ) );
return $item_data->order_item_type;
}
}

View File

@ -1197,4 +1197,23 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
return wp_parse_id_list( $product_ids );
}
/**
* Get the product type based on product ID.
*
* @since 2.7.0
* @param int $product_id
* @return bool|string
*/
public function get_product_type( $product_id ) {
$post_type = get_post_type( $product_id );
if ( 'product_variation' === $post_type ) {
return 'variation';
} elseif ( 'product' === $post_type ) {
$terms = get_the_terms( $product_id, 'product_type' );
return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
} else {
return false;
}
}
}

View File

@ -116,4 +116,11 @@ interface WC_Order_Data_Store_Interface {
* @param bool $set
*/
public function set_recorded_coupon_usage_counts( $order, $set );
/**
* Get the order type based on Order ID.
* @param int $order_id
* @return string
*/
public function get_order_type( $order_id );
}

View File

@ -81,4 +81,11 @@ interface WC_Order_Item_Data_Store_Interface {
* @return int
*/
function get_order_id_by_order_item_id( $item_id );
/**
* Get the order item type based on Item ID.
* @param int $item_id
* @return string
*/
public function get_order_item_type( $item_id );
}

View File

@ -115,4 +115,11 @@ interface WC_Product_Data_Store_Interface {
* @return array
*/
public function get_products( $args = array() );
/**
* Get the product type based on product ID.
* @param int $product_id
* @return bool|string
*/
public function get_product_type( $product_id );
}