Support for non-product post types in WC_Product_Factory #2199

This commit is contained in:
Coen Jacobs 2013-01-10 12:46:39 +01:00
parent 00accc82e7
commit 24b2c47077
1 changed files with 17 additions and 11 deletions

View File

@ -35,23 +35,29 @@ class WC_Product_Factory {
$product_id = absint( $the_product->ID );
$post_type = $the_product->post_type;
if ( isset( $args['product_type'] ) ) {
$product_type = $args['product_type'];
} elseif ( 'product_variation' == $post_type ) {
$product_type = 'variation';
if ( in_array( $post_type, array( 'product', 'product_variation' ) ) ) {
if ( isset( $args['product_type'] ) ) {
$product_type = $args['product_type'];
} elseif ( 'product_variation' == $post_type ) {
$product_type = 'variation';
} else {
$terms = get_the_terms( $product_id, 'product_type' );
$product_type = ! empty( $terms ) && isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple';
}
$classname = 'WC_Product_' . ucfirst( $product_type );
} else {
$terms = get_the_terms( $product_id, 'product_type' );
$product_type = ! empty( $terms ) && isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple';
$classname = false;
$product_type = false;
}
// Filter classname so that the class can be overridden if extended.
$classname = apply_filters( 'woocommerce_product_class', 'WC_Product_' . ucfirst( $product_type ), $product_type, $post_type, $product_id );
$classname = apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id );
if ( class_exists( $classname ) ) {
if ( $classname && class_exists( $classname ) ) {
return new $classname( $the_product, $args );
} else {
// Use simple
return new WC_Product_Simple( $the_product, $args );
}
return false;
}
}