2012-11-22 11:37:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Product Factory Class
|
|
|
|
*
|
|
|
|
* The WooCommerce product factory creating the right product object
|
|
|
|
*
|
|
|
|
* @class WC_Product_Factory
|
2012-12-03 19:19:58 +00:00
|
|
|
* @version 2.0.0
|
2012-11-22 11:37:41 +00:00
|
|
|
* @package WooCommerce/Classes
|
2013-02-20 17:14:46 +00:00
|
|
|
* @category Class
|
2012-11-22 11:37:41 +00:00
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class WC_Product_Factory {
|
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
/**
|
|
|
|
* get_product function.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param bool $the_product (default: false)
|
|
|
|
* @param array $args (default: array())
|
2013-07-01 21:48:07 +00:00
|
|
|
* @return WC_Product
|
2012-11-29 16:48:40 +00:00
|
|
|
*/
|
2012-11-26 13:37:24 +00:00
|
|
|
public function get_product( $the_product = false, $args = array() ) {
|
2012-11-22 11:37:41 +00:00
|
|
|
global $post;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-22 12:56:52 +00:00
|
|
|
if ( false === $the_product ) {
|
2012-11-22 11:37:41 +00:00
|
|
|
$the_product = $post;
|
2012-11-22 12:56:52 +00:00
|
|
|
} elseif ( is_numeric( $the_product ) ) {
|
2012-11-22 11:37:41 +00:00
|
|
|
$the_product = get_post( $the_product );
|
2014-11-11 16:24:22 +00:00
|
|
|
} elseif ( $the_product instanceof WC_Product ) {
|
|
|
|
return $the_product;
|
2012-11-22 12:56:52 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-11-11 16:24:22 +00:00
|
|
|
if ( ! $the_product ) {
|
2012-11-30 15:11:39 +00:00
|
|
|
return false;
|
2014-11-11 16:24:22 +00:00
|
|
|
}
|
2012-11-30 15:11:39 +00:00
|
|
|
|
2014-11-11 16:24:22 +00:00
|
|
|
if ( is_object( $the_product ) ) {
|
2014-01-17 20:28:15 +00:00
|
|
|
$product_id = absint( $the_product->ID );
|
|
|
|
$post_type = $the_product->post_type;
|
|
|
|
}
|
2012-11-22 12:56:52 +00:00
|
|
|
|
2013-01-10 11:46:39 +00:00
|
|
|
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' );
|
2013-02-28 04:29:34 +00:00
|
|
|
$product_type = ! empty( $terms ) && isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple';
|
2013-01-10 11:46:39 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 04:35:11 +00:00
|
|
|
// Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class
|
2013-08-13 12:02:20 +00:00
|
|
|
$classname = 'WC_Product_' . implode( '_', array_map( 'ucfirst', explode( '-', $product_type ) ) );
|
2012-11-22 12:56:52 +00:00
|
|
|
} else {
|
2013-01-10 11:46:39 +00:00
|
|
|
$classname = false;
|
|
|
|
$product_type = false;
|
2012-11-22 12:56:52 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-22 11:37:41 +00:00
|
|
|
// Filter classname so that the class can be overridden if extended.
|
2013-01-10 11:46:39 +00:00
|
|
|
$classname = apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-02-05 11:28:28 +00:00
|
|
|
if ( ! class_exists( $classname ) )
|
|
|
|
$classname = 'WC_Product_Simple';
|
2013-01-10 11:46:39 +00:00
|
|
|
|
2013-02-05 11:28:28 +00:00
|
|
|
return new $classname( $the_product, $args );
|
2012-11-22 11:37:41 +00:00
|
|
|
}
|
2013-07-01 21:48:07 +00:00
|
|
|
}
|