1 <?php
2
3 4 5 6 7 8 9 10 11 12 13
14 class WC_Product_Factory {
15
16 17 18 19 20 21 22 23
24 public function get_product( $the_product = false, $args = array() ) {
25 global $post;
26
27 if ( false === $the_product ) {
28 $the_product = $post;
29 } elseif ( is_numeric( $the_product ) ) {
30 $the_product = get_post( $the_product );
31 }
32
33 if ( ! $the_product )
34 return false;
35
36 if ( is_object ( $the_product ) ) {
37 $product_id = absint( $the_product->ID );
38 $post_type = $the_product->post_type;
39 }
40
41 if ( in_array( $post_type, array( 'product', 'product_variation' ) ) ) {
42 if ( isset( $args['product_type'] ) ) {
43 $product_type = $args['product_type'];
44 } elseif ( 'product_variation' == $post_type ) {
45 $product_type = 'variation';
46 } else {
47 $terms = get_the_terms( $product_id, 'product_type' );
48 $product_type = ! empty( $terms ) && isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple';
49 }
50
51
52 $classname = 'WC_Product_' . implode( '_', array_map( 'ucfirst', explode( '-', $product_type ) ) );
53 } else {
54 $classname = false;
55 $product_type = false;
56 }
57
58
59 $classname = apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id );
60
61 if ( ! class_exists( $classname ) )
62 $classname = 'WC_Product_Simple';
63
64 return new $classname( $the_product, $args );
65 }
66 }
67