Simplified version of Product Factory using an args array
This commit is contained in:
parent
93cddbaa16
commit
b2a868301c
|
@ -19,7 +19,7 @@ class WC_Product_External extends WC_Product {
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $product
|
* @param mixed $product
|
||||||
*/
|
*/
|
||||||
function __construct( $product ) {
|
function __construct( $product, $args ) {
|
||||||
|
|
||||||
parent::__construct( $product );
|
parent::__construct( $product );
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?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() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_product( $the_product = false, $args ) {
|
||||||
|
global $post;
|
||||||
|
|
||||||
|
if ( false === $the_product )
|
||||||
|
$the_product = $post;
|
||||||
|
elseif ( is_numeric( $the_product ) )
|
||||||
|
$the_product = get_post( $the_product );
|
||||||
|
|
||||||
|
$product_id = absint( $the_product->ID );
|
||||||
|
$terms = get_the_terms( $product_id, 'product_type' );
|
||||||
|
$product_type = isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple';
|
||||||
|
|
||||||
|
// Filter classname so that the class can be overridden if extended.
|
||||||
|
$classname = apply_filters( 'woocommerce_product_class', 'WC_Product_' . $product_type, $product_type, $post_type, $product_id );
|
||||||
|
|
||||||
|
if ( class_exists( $classname ) ) {
|
||||||
|
return new $classname( $the_product, $args );
|
||||||
|
} else {
|
||||||
|
// Use simple
|
||||||
|
return new WC_Product_Simple( $the_product );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,7 +25,7 @@ class WC_Product_Grouped extends WC_Product {
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $product
|
* @param mixed $product
|
||||||
*/
|
*/
|
||||||
function __construct( $product ) {
|
function __construct( $product, $args ) {
|
||||||
|
|
||||||
parent::__construct( $product );
|
parent::__construct( $product );
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ class WC_Product_Simple extends WC_Product {
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $product
|
* @param mixed $product
|
||||||
*/
|
*/
|
||||||
function __construct( $product ) {
|
function __construct( $product, $args ) {
|
||||||
|
|
||||||
parent::__construct( $product );
|
parent::__construct( $product );
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ class WC_Product_Variable extends WC_Product {
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $product
|
* @param mixed $product
|
||||||
*/
|
*/
|
||||||
function __construct( $product ) {
|
function __construct( $product, $args ) {
|
||||||
|
|
||||||
parent::__construct( $product );
|
parent::__construct( $product );
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,9 @@ class WC_Product_Variation extends WC_Product {
|
||||||
* @param array $parent_custom_fields (default: '') Array of the parent products meta data
|
* @param array $parent_custom_fields (default: '') Array of the parent products meta data
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function __construct( $variation, $parent_id = '', $parent_custom_fields = '' ) {
|
function __construct( $variation, $args ) {
|
||||||
|
$parent_id = $args['parent_id'];
|
||||||
|
$parent_custom_fields = $args['meta'];
|
||||||
|
|
||||||
if ( is_object( $variation ) ) {
|
if ( is_object( $variation ) ) {
|
||||||
$this->variation_id = absint( $variation->ID );
|
$this->variation_id = absint( $variation->ID );
|
||||||
|
|
|
@ -20,7 +20,7 @@ add_filter( 'woocommerce_coupon_code', 'strtolower' ); // Coupons case-insensiti
|
||||||
add_filter( 'woocommerce_stock_amount', 'absint' ); // Stock amounts are integers by default
|
add_filter( 'woocommerce_stock_amount', 'absint' ); // Stock amounts are integers by default
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function for returning products.
|
* Main function for returning products, uses the WC_Product_Factory class.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $the_product Post object or post ID of the product.
|
* @param mixed $the_product Post object or post ID of the product.
|
||||||
|
@ -29,40 +29,13 @@ add_filter( 'woocommerce_stock_amount', 'absint' ); // Stock amounts are integer
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function get_product( $the_product = false, $parent_id = '', $meta = '' ) {
|
function get_product( $the_product = false, $parent_id = '', $meta = '' ) {
|
||||||
global $post;
|
$args = array(
|
||||||
|
'parent_id' => $parent_id,
|
||||||
|
'meta' => $meta,
|
||||||
|
);
|
||||||
|
|
||||||
if ( false === $the_product )
|
$factory = new WC_Product_Factory();
|
||||||
$the_product = $post;
|
return $factory->get_product( $the_product, $parent_id, $meta );
|
||||||
elseif ( is_numeric( $the_product ) )
|
|
||||||
$the_product = get_post( $the_product );
|
|
||||||
|
|
||||||
$product_id = absint( $the_product->ID );
|
|
||||||
$post_type = $the_product->post_type;
|
|
||||||
|
|
||||||
if ( $post_type == 'product_variation' ) {
|
|
||||||
// Filter classname so that the class can be overridden if extended.
|
|
||||||
$classname = apply_filters( 'woocommerce_product_variation_class', 'WC_Product_Variation', $product_id );
|
|
||||||
|
|
||||||
if ( class_exists( $classname ) ) {
|
|
||||||
return new $classname( $the_product, $parent_id, $meta );
|
|
||||||
} else {
|
|
||||||
// Use simple
|
|
||||||
return new WC_Product_Variation( $the_product, $parent_id, $meta );
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$terms = get_the_terms( $product_id, 'product_type' );
|
|
||||||
$product_type = isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple';
|
|
||||||
|
|
||||||
// Filter classname so that the class can be overridden if extended.
|
|
||||||
$classname = apply_filters( 'woocommerce_product_class', 'WC_Product_' . $product_type, $product_type, $post_type, $product_id );
|
|
||||||
|
|
||||||
if ( class_exists( $classname ) ) {
|
|
||||||
return new $classname( $the_product );
|
|
||||||
} else {
|
|
||||||
// Use simple
|
|
||||||
return new WC_Product_Simple( $the_product );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -172,6 +172,7 @@ class Woocommerce {
|
||||||
include( 'classes/class-wc-countries.php' ); // Defines countries and states
|
include( 'classes/class-wc-countries.php' ); // Defines countries and states
|
||||||
include( 'classes/class-wc-order.php' ); // Single order class
|
include( 'classes/class-wc-order.php' ); // Single order class
|
||||||
|
|
||||||
|
include( 'classes/class-wc-product-factory.php' ); // Product factory
|
||||||
include( 'classes/class-wc-product.php' ); // Product class abstract
|
include( 'classes/class-wc-product.php' ); // Product class abstract
|
||||||
include( 'classes/class-wc-product-simple.php' ); // Simple product type class
|
include( 'classes/class-wc-product-simple.php' ); // Simple product type class
|
||||||
include( 'classes/class-wc-product-external.php' ); // External product type class
|
include( 'classes/class-wc-product-external.php' ); // External product type class
|
||||||
|
|
Loading…
Reference in New Issue