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
|
|
|
|
* @version 1.7
|
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
|
|
|
|
class WC_Product_Factory {
|
|
|
|
public function __construct() {
|
|
|
|
}
|
|
|
|
|
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-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 );
|
2012-11-22 12:56:52 +00:00
|
|
|
}
|
2012-11-22 11:37:41 +00:00
|
|
|
|
|
|
|
$product_id = absint( $the_product->ID );
|
2012-11-22 11:40:42 +00:00
|
|
|
$post_type = $the_product->post_type;
|
2012-11-22 12:56:52 +00:00
|
|
|
|
|
|
|
if ( 'product_variation' == $post_type ) {
|
|
|
|
$product_type = 'variation';
|
|
|
|
} else {
|
|
|
|
$terms = get_the_terms( $product_id, 'product_type' );
|
2012-11-26 13:37:24 +00:00
|
|
|
$product_type = ! empty( $terms ) && isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple';
|
2012-11-22 12:56:52 +00:00
|
|
|
}
|
2012-11-22 11:37:41 +00:00
|
|
|
|
|
|
|
// Filter classname so that the class can be overridden if extended.
|
2012-11-27 15:31:07 +00:00
|
|
|
$classname = apply_filters( 'woocommerce_product_class', 'WC_Product_' . ucfirst($product_type), $product_type, $post_type, $product_id );
|
2012-11-22 11:37:41 +00:00
|
|
|
|
|
|
|
if ( class_exists( $classname ) ) {
|
|
|
|
return new $classname( $the_product, $args );
|
|
|
|
} else {
|
|
|
|
// Use simple
|
2012-11-22 14:13:31 +00:00
|
|
|
return new WC_Product_Simple( $the_product, $args );
|
2012-11-22 11:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|