woocommerce/classes/class-wc-product-factory.php

63 lines
1.6 KiB
PHP
Raw Normal View History

<?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
* @package WooCommerce/Classes
* @author WooThemes
*/
class WC_Product_Factory {
/**
* get_product function.
*
* @access public
* @param bool $the_product (default: false)
* @param array $args (default: array())
* @return WC_Product_Simple
*/
2012-11-26 13:37:24 +00:00
public function get_product( $the_product = false, $args = array() ) {
global $post;
2012-11-27 16:22:47 +00:00
if ( false === $the_product ) {
$the_product = $post;
} elseif ( is_numeric( $the_product ) ) {
$the_product = get_post( $the_product );
}
2012-11-27 16:22:47 +00:00
2012-11-30 15:11:39 +00:00
if ( ! $the_product )
return false;
$product_id = absint( $the_product->ID );
$post_type = $the_product->post_type;
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 {
$classname = false;
$product_type = false;
}
2012-11-27 16:22:47 +00:00
// Filter classname so that the class can be overridden if extended.
$classname = apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id );
2012-11-27 16:22:47 +00:00
if ( $classname && class_exists( $classname ) ) {
return new $classname( $the_product, $args );
}
return false;
}
}