Additional API updates/fixes. Added some todos
This commit is contained in:
parent
7ed100dcac
commit
11dfc44778
|
@ -684,6 +684,13 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
|
||||||
*/
|
*/
|
||||||
protected function prepare_item_for_database( $request ) {
|
protected function prepare_item_for_database( $request ) {
|
||||||
$allowed_types = array( 'simple', 'grouped', 'external', 'variable', 'variation' );
|
$allowed_types = array( 'simple', 'grouped', 'external', 'variable', 'variation' );
|
||||||
|
// @ todo - use WC_Product_Factory::get_classname_from_product_type instead
|
||||||
|
/*
|
||||||
|
$classname = WC_Product_Factory::get_classname_from_product_type( $product_type );
|
||||||
|
if ( ! class_exists( $classname ) ) {
|
||||||
|
$classname = 'WC_Product_Simple';
|
||||||
|
}
|
||||||
|
*/
|
||||||
if ( isset( $request['type'] ) && in_array( $request['type'], $allowed_types ) ) {
|
if ( isset( $request['type'] ) && in_array( $request['type'], $allowed_types ) ) {
|
||||||
$classname = "WC_Product_{$request['type']}";
|
$classname = "WC_Product_{$request['type']}";
|
||||||
} else {
|
} else {
|
||||||
|
@ -1077,6 +1084,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attributes.
|
// Attributes.
|
||||||
|
error_log( print_r ( $request['attributes'], 1 ) );
|
||||||
if ( isset( $request['attributes'] ) ) {
|
if ( isset( $request['attributes'] ) ) {
|
||||||
$attributes = array();
|
$attributes = array();
|
||||||
|
|
||||||
|
@ -1096,6 +1104,10 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
error_log( print_r ( 'test', 1 ) );
|
||||||
|
error_log( print_r ( $attribute_id, 1 ) );
|
||||||
|
error_log( print_r ( $attribute_name, 1 ) );
|
||||||
|
|
||||||
if ( $attribute_id ) {
|
if ( $attribute_id ) {
|
||||||
|
|
||||||
if ( isset( $attribute['options'] ) ) {
|
if ( isset( $attribute['options'] ) ) {
|
||||||
|
@ -1420,6 +1432,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save variations.
|
* Save variations.
|
||||||
|
* @todo after variations CRUD is done
|
||||||
*
|
*
|
||||||
* @param WC_Product $product
|
* @param WC_Product $product
|
||||||
* @param WP_REST_Request $request
|
* @param WP_REST_Request $request
|
||||||
|
@ -1866,7 +1879,8 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete product.
|
// Delete product.
|
||||||
wp_delete_post( $post->ID, true );
|
$product = wc_get_product( $post->ID );
|
||||||
|
$product->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1876,9 +1890,10 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
|
||||||
* @return WP_REST_Response|WP_Error
|
* @return WP_REST_Response|WP_Error
|
||||||
*/
|
*/
|
||||||
public function delete_item( $request ) {
|
public function delete_item( $request ) {
|
||||||
$id = (int) $request['id'];
|
$id = (int) $request['id'];
|
||||||
$force = (bool) $request['force'];
|
$force = (bool) $request['force'];
|
||||||
$post = get_post( $id );
|
$post = get_post( $id );
|
||||||
|
$product = wc_get_product( $id );
|
||||||
|
|
||||||
if ( empty( $id ) || empty( $post->ID ) || ! in_array( $post->post_type, $this->get_post_types() ) ) {
|
if ( empty( $id ) || empty( $post->ID ) || ! in_array( $post->post_type, $this->get_post_types() ) ) {
|
||||||
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid post ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid post ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||||
|
@ -1905,26 +1920,23 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
|
||||||
|
|
||||||
// If we're forcing, then delete permanently.
|
// If we're forcing, then delete permanently.
|
||||||
if ( $force ) {
|
if ( $force ) {
|
||||||
$child_product_variations = get_children( 'post_parent=' . $id . '&post_type=product_variation' );
|
|
||||||
|
|
||||||
if ( ! empty( $child_product_variations ) ) {
|
if ( $product->is_type( 'variable' ) ) {
|
||||||
foreach ( $child_product_variations as $child ) {
|
foreach ( $product->get_children() as $child_id ) {
|
||||||
wp_delete_post( $child->ID, true );
|
$child = wc_get_product( $child_id );
|
||||||
|
$child->delete();
|
||||||
}
|
}
|
||||||
}
|
} elseif ( $product->is_type( 'grouped' ) ) {
|
||||||
|
foreach ( $product->get_children() as $child_id ) {
|
||||||
$child_products = get_children( 'post_parent=' . $id . '&post_type=product' );
|
$child = wc_get_product( $child_id );
|
||||||
|
$child->set_parent_id( 0 );
|
||||||
if ( ! empty( $child_products ) ) {
|
$child->save();
|
||||||
foreach ( $child_products as $child ) {
|
|
||||||
$child_post = array();
|
|
||||||
$child_post['ID'] = $child->ID;
|
|
||||||
$child_post['post_parent'] = 0;
|
|
||||||
wp_update_post( $child_post );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = wp_delete_post( $id, true );
|
$result = wp_delete_post( $id, true );
|
||||||
|
$product->delete();
|
||||||
|
$result = $product->get_id() > 0 ? fase : true;
|
||||||
} else {
|
} else {
|
||||||
// If we don't support trashing for this type, error out.
|
// If we don't support trashing for this type, error out.
|
||||||
if ( ! $supports_trash ) {
|
if ( ! $supports_trash ) {
|
||||||
|
|
|
@ -311,6 +311,8 @@ class Products_API extends WC_REST_Unit_Test_Case {
|
||||||
$response = $this->server->dispatch( $request );
|
$response = $this->server->dispatch( $request );
|
||||||
$data = $response->get_data();
|
$data = $response->get_data();
|
||||||
|
|
||||||
|
//error_log( print_r ( $data, 1 ) );
|
||||||
|
|
||||||
$this->assertEquals( 'DUMMY SKU VARIABLE API', $data['sku'] );
|
$this->assertEquals( 'DUMMY SKU VARIABLE API', $data['sku'] );
|
||||||
$this->assertEquals( 'Test Variable Product', $data['name'] );
|
$this->assertEquals( 'Test Variable Product', $data['name'] );
|
||||||
$this->assertEquals( 'variable', $data['type'] );
|
$this->assertEquals( 'variable', $data['type'] );
|
||||||
|
|
Loading…
Reference in New Issue