1 <?php
 2 
 3 /**
 4  * Product Factory Class
 5  *
 6  * The WooCommerce product factory creating the right product object
 7  *
 8  * @class       WC_Product_Factory
 9  * @version     2.0.0
10  * @package     WooCommerce/Classes
11  * @category    Class
12  * @author      WooThemes
13  */
14 class WC_Product_Factory {
15 
16     /**
17      * get_product function.
18      *
19      * @access public
20      * @param bool $the_product (default: false)
21      * @param array $args (default: array())
22      * @return WC_Product
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             // Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class
52             $classname = 'WC_Product_' . implode( '_', array_map( 'ucfirst', explode( '-', $product_type ) ) );
53         } else {
54             $classname = false;
55             $product_type = false;
56         }
57 
58         // Filter classname so that the class can be overridden if extended.
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 
WooCommerce API documentation generated by ApiGen 2.8.0