WC_Product_Factory

Product Factory Class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-product-factory.php  
  1. class WC_Product_Factory { 
  2.  
  3. /** 
  4. * Get a product. 
  5. * @param mixed $product_id (default: false) 
  6. * @param array $deprecated Previously used to pass arguments to the factory, e.g. to force a type. 
  7. * @return WC_Product|bool Product object or null if the product cannot be loaded. 
  8. */ 
  9. public function get_product( $product_id = false, $deprecated = array() ) { 
  10. if ( ! $product_id = $this->get_product_id( $product_id ) ) { 
  11. return false; 
  12.  
  13. $product_type = $this->get_product_type( $product_id ); 
  14.  
  15. // Backwards compatibility. 
  16. if ( ! empty( $deprecated ) ) { 
  17. wc_deprecated_argument( 'args', '3.0', 'Passing args to the product factory is deprecated. If you need to force a type, construct the product class directly.' ); 
  18.  
  19. if ( isset( $deprecated['product_type'] ) ) { 
  20. $product_type = $this->get_classname_from_product_type( $deprecated['product_type'] ); 
  21.  
  22. $classname = $this->get_product_classname( $product_id, $product_type ); 
  23.  
  24. try { 
  25. return new $classname( $product_id, $deprecated ); 
  26. } catch ( Exception $e ) { 
  27. return false; 
  28.  
  29. /** 
  30. * Gets a product classname and allows filtering. Returns WC_Product_Simple if the class does not exist. 
  31. * @since 3.0.0 
  32. * @param int $product_id 
  33. * @param string $product_type 
  34. * @return string 
  35. */ 
  36. public static function get_product_classname( $product_id, $product_type ) { 
  37. $classname = apply_filters( 'woocommerce_product_class', self::get_classname_from_product_type( $product_type ), $product_type, 'variation' === $product_type ? 'product_variation' : 'product', $product_id ); 
  38.  
  39. if ( ! $classname || ! class_exists( $classname ) ) { 
  40. $classname = 'WC_Product_Simple'; 
  41.  
  42. return $classname; 
  43.  
  44. /** 
  45. * Get the product type for a product. 
  46. * @since 3.0.0 
  47. * @param int $product_id 
  48. * @return string|false 
  49. */ 
  50. public static function get_product_type( $product_id ) { 
  51. // Allow the overriding of the lookup in this function. Return the product type here. 
  52. $override = apply_filters( 'woocommerce_product_type_query', false, $product_id ); 
  53. if ( ! $override ) { 
  54. return WC_Data_Store::load( 'product' )->get_product_type( $product_id ); 
  55. } else { 
  56. return $override; 
  57.  
  58. /** 
  59. * Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class. 
  60. * @param string $product_type 
  61. * @return string|false 
  62. */ 
  63. public static function get_classname_from_product_type( $product_type ) { 
  64. return $product_type ? 'WC_Product_' . implode( '_', array_map( 'ucfirst', explode( '-', $product_type ) ) ) : false; 
  65.  
  66. /** 
  67. * Get the product ID depending on what was passed. 
  68. * @since 3.0.0 
  69. * @param mixed $product 
  70. * @return int|bool false on failure 
  71. */ 
  72. private function get_product_id( $product ) { 
  73. if ( false === $product && isset( $GLOBALS['post'], $GLOBALS['post']->ID ) && 'product' === get_post_type( $GLOBALS['post']->ID ) ) { 
  74. return $GLOBALS['post']->ID; 
  75. } elseif ( is_numeric( $product ) ) { 
  76. return $product; 
  77. } elseif ( $product instanceof WC_Product ) { 
  78. return $product->get_id(); 
  79. } elseif ( ! empty( $product->ID ) ) { 
  80. return $product->ID; 
  81. } else { 
  82. return false;