Always use type arg

This commit is contained in:
Mike Jolley 2017-02-08 18:08:13 +00:00
parent eb150724c4
commit 3cf772bcb5
1 changed files with 10 additions and 5 deletions

View File

@ -678,14 +678,19 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
* @return WP_Error|stdClass $data Post object. * @return WP_Error|stdClass $data Post object.
*/ */
protected function prepare_item_for_database( $request ) { protected function prepare_item_for_database( $request ) {
if ( isset( $request['id'] ) ) { // Type is the most important part here because we need to be using the correct class and methods.
$product = wc_get_product( absint( $request['id'] ) ); if ( isset( $request['type'] ) ) {
} else { $classname = WC_Product_Factory::get_classname_from_product_type( $request['type'] );
$classname = WC_Product_Factory::get_classname_from_product_type( $request['type'] );
if ( ! class_exists( $classname ) ) { if ( ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple'; $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. // Post title.