From 3cf772bcb5e7f2c42e1f84750ee0b80eadf919c5 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 8 Feb 2017 18:08:13 +0000 Subject: [PATCH] Always use type arg --- .../api/class-wc-rest-products-controller.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/includes/api/class-wc-rest-products-controller.php b/includes/api/class-wc-rest-products-controller.php index 30f5d380de8..99fcdc7c5ea 100644 --- a/includes/api/class-wc-rest-products-controller.php +++ b/includes/api/class-wc-rest-products-controller.php @@ -678,14 +678,19 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller { * @return WP_Error|stdClass $data Post object. */ protected function prepare_item_for_database( $request ) { - if ( isset( $request['id'] ) ) { - $product = wc_get_product( absint( $request['id'] ) ); - } else { - $classname = WC_Product_Factory::get_classname_from_product_type( $request['type'] ); + // Type is the most important part here because we need to be using the correct class and methods. + if ( isset( $request['type'] ) ) { + $classname = WC_Product_Factory::get_classname_from_product_type( $request['type'] ); + if ( ! class_exists( $classname ) ) { $classname = 'WC_Product_Simple'; } - $product = new $classname(); + + $product = new $classname( absint( $request['id'] ) ); + } elseif ( isset( $request['id'] ) ) { + $product = wc_get_product( absint( $request['id'] ) ); + } else { + $product = new WC_Product_Simple(); } // Post title.