Handle PR feedback

This commit is contained in:
Justin Shreve 2016-11-02 10:08:05 -07:00
parent 674a203487
commit db9ca040af
6 changed files with 115 additions and 113 deletions

View File

@ -1973,7 +1973,8 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* @return array * @return array
*/ */
public function get_files() { public function get_files() {
$downloadable_files = array_filter( ! empty( $this->get_downloads() ) ? (array) maybe_unserialize( $this->get_downloads() ) : array() ); $downloads = $this->get_downloads();
$downloadable_files = array_filter( ! empty( $downloads ) ? (array) maybe_unserialize( $downloads ) : array() );
if ( ! empty( $downloadable_files ) ) { if ( ! empty( $downloadable_files ) ) {
foreach ( $downloadable_files as $key => $file ) { foreach ( $downloadable_files as $key => $file ) {

View File

@ -469,20 +469,19 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
* @return array * @return array
*/ */
protected function get_product_data( $product ) { protected function get_product_data( $product ) {
$post = get_post( $product->get_id() );
$data = array( $data = array(
'id' => (int) $product->is_type( 'variation' ) ? $product->get_variation_id() : $product->get_id(), 'id' => (int) $product->is_type( 'variation' ) ? $product->get_variation_id() : $product->get_id(),
'name' => $product->get_title(), 'name' => $product->get_name(),
'slug' => $post->post_name, 'slug' => $product->get_slug(),
'permalink' => $product->get_permalink(), 'permalink' => $product->get_permalink(),
'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt ), 'date_created' => wc_rest_prepare_date_response( $product->get_date_created() ),
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt ), 'date_modified' => wc_rest_prepare_date_response( $product->get_date_modified() ),
'type' => $product->get_type(), 'type' => $product->get_type(),
'status' => $post->post_status, 'status' => $product->get_status(),
'featured' => $product->is_featured(), 'featured' => $product->is_featured(),
'catalog_visibility' => $product->get_catalog_visibility(), 'catalog_visibility' => $product->get_catalog_visibility(),
'description' => wpautop( do_shortcode( $post->post_content ) ), 'description' => wpautop( do_shortcode( $product->get_description() ) ),
'short_description' => apply_filters( 'woocommerce_short_description', $post->post_excerpt ), 'short_description' => apply_filters( 'woocommerce_short_description', $product->get_short_description() ),
'sku' => $product->get_sku(), 'sku' => $product->get_sku(),
'price' => $product->get_price(), 'price' => $product->get_price(),
'regular_price' => $product->get_regular_price(), 'regular_price' => $product->get_regular_price(),
@ -520,13 +519,13 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
'shipping_taxable' => $product->is_shipping_taxable(), 'shipping_taxable' => $product->is_shipping_taxable(),
'shipping_class' => $product->get_shipping_class(), 'shipping_class' => $product->get_shipping_class(),
'shipping_class_id' => (int) $product->get_shipping_class_id(), 'shipping_class_id' => (int) $product->get_shipping_class_id(),
'reviews_allowed' => ( 'open' === $post->comment_status ), 'reviews_allowed' => $product->get_reviews_allowed(),
'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ), 'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ),
'rating_count' => (int) $product->get_rating_count(), 'rating_count' => (int) $product->get_rating_count(),
'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ), 'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ),
'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ), 'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ),
'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ), 'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ),
'parent_id' => $product->is_type( 'variation' ) ? $product->parent->id : $post->post_parent, 'parent_id' => $product->get_parent_id(),
'purchase_note' => wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ), 'purchase_note' => wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ),
'categories' => $this->get_taxonomy_terms( $product ), 'categories' => $this->get_taxonomy_terms( $product ),
'tags' => $this->get_taxonomy_terms( $product, 'tag' ), 'tags' => $this->get_taxonomy_terms( $product, 'tag' ),
@ -866,7 +865,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
} }
if ( isset( $image['position'] ) && 0 === $image['position'] ) { if ( isset( $image['position'] ) && 0 === $image['position'] ) {
$product->set_thumbnail_id( $attachment_id ); $product->set_image_id( $attachment_id );
} else { } else {
$gallery[] = $attachment_id; $gallery[] = $attachment_id;
} }
@ -886,7 +885,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$product->set_gallery_image_ids( $gallery ); $product->set_gallery_image_ids( $gallery );
} }
} else { } else {
$product->set_thumbnail_id( '' ); $product->set_image_id( '' );
$product->set_gallery_image_ids( array() ); $product->set_gallery_image_ids( array() );
} }
@ -1192,7 +1191,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
} }
// Update parent if grouped so price sorting works and stays in sync with the cheapest child. // Update parent if grouped so price sorting works and stays in sync with the cheapest child.
if ( $product->get_parent_id() > 0 || 'grouped' === $product->get_type() ) { if ( $product->get_parent_id() > 0 || $product->is_type( 'grouped' ) ) {
$clear_parent_ids = array(); $clear_parent_ids = array();
@ -1204,7 +1203,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$parent->save(); $parent->save();
} }
if ( 'grouped' === $product->get_type() ) { if ( $product->is_type( 'grouped' ) ) {
$clear_parent_ids[] = $product->get_id(); $clear_parent_ids[] = $product->get_id();
} }
@ -1265,40 +1264,40 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$backorders = $product->get_backorders(); $backorders = $product->get_backorders();
} }
if ( 'grouped' === $product->get_type() ) { if ( $product->is_type( 'grouped' ) ) {
$product->set_manage_stock( 'no' ); $product->set_manage_stock( 'no' );
$product->set_backorders( 'no' ); $product->set_backorders( 'no' );
$product->set_stock( '' ); $product->set_stock_quantity( '' );
$product->set_stock_status( $status ); $product->set_stock_status( $status );
} elseif ( 'external' === $product->get_type() ) { } elseif ( $product->is_type( 'external' ) ) {
$product->set_manage_stock( 'no' ); $product->set_manage_stock( 'no' );
$product->set_backorders( 'no' ); $product->set_backorders( 'no' );
$product->set_stock( '' ); $product->set_stock_quantity( '' );
$product->set_stock_status( 'instock' ); $product->set_stock_status( 'instock' );
} elseif ( 'yes' === $manage_stock ) { } elseif ( 'yes' === $manage_stock ) {
$product->set_backorders( $backorders ); $product->set_backorders( $backorders );
// Stock status is always determined by children so sync later. // Stock status is always determined by children so sync later.
if ( 'variable' !== $product->get_type() ) { if ( ! $product->is_type( 'variable' ) ) {
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} }
// Stock quantity. // Stock quantity.
if ( isset( $request['stock_quantity'] ) ) { if ( isset( $request['stock_quantity'] ) ) {
$product->set_stock( wc_stock_amount( $request['stock_quantity'] ) ); $product->set_stock_quantity( wc_stock_amount( $request['stock_quantity'] ) );
} elseif ( isset( $request['inventory_delta'] ) ) { } elseif ( isset( $request['inventory_delta'] ) ) {
$stock_quantity = wc_stock_amount( $product->get_stock() ); $stock_quantity = wc_stock_amount( $product->get_stock() );
$stock_quantity += wc_stock_amount( $request['inventory_delta'] ); $stock_quantity += wc_stock_amount( $request['inventory_delta'] );
$product->set_stock( wc_stock_amount( $stock_quantity ) ); $product->set_stock_quantity( wc_stock_amount( $stock_quantity ) );
} }
} else { } else {
// Don't manage stock. // Don't manage stock.
$product->set_manage_stock( 'no' ); $product->set_manage_stock( 'no' );
$product->set_backorders( $backorders ); $product->set_backorders( $backorders );
$product->set_stock( '' ); $product->set_stock_quantity( '' );
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} }
} elseif ( 'variable' !== $product->get_type() ) { } elseif ( ! $product->is_type( 'variable' ) ) {
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} }
@ -1376,7 +1375,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
} }
// Product url and button text for external products. // Product url and button text for external products.
if ( 'external' === $product->get_type() ) { if ( $product->is_type( 'external' ) ) {
if ( isset( $request['external_url'] ) ) { if ( isset( $request['external_url'] ) ) {
$product->set_product_url( $request['external_url'] ); $product->set_product_url( $request['external_url'] );
} }

View File

@ -266,12 +266,11 @@ class WC_API_Products extends WC_API_Resource {
* @return array * @return array
*/ */
private function get_product_data( $product ) { private function get_product_data( $product ) {
$post = get_post( $product->get_id() );
return array( return array(
'title' => $product->get_title(), 'title' => $product->get_name(),
'id' => (int) $product->is_type( 'variation' ) ? $product->get_variation_id() : $product->get_id(), 'id' => (int) $product->is_type( 'variation' ) ? $product->get_variation_id() : $product->get_id(), // @todo variation
'created_at' => $this->server->format_datetime( $post->post_date_gmt ), 'created_at' => $this->server->format_datetime( $product->get_date_created() ),
'updated_at' => $this->server->format_datetime( $post->post_modified_gmt ), 'updated_at' => $this->server->format_datetime( $product->get_date_modified() ),
'type' => $product->get_type(), 'type' => $product->get_type(),
'status' => $product->get_status(), 'status' => $product->get_status(),
'downloadable' => $product->is_downloadable(), 'downloadable' => $product->is_downloadable(),
@ -286,7 +285,7 @@ class WC_API_Products extends WC_API_Resource {
'tax_status' => $product->get_tax_status(), 'tax_status' => $product->get_tax_status(),
'tax_class' => $product->get_tax_class(), 'tax_class' => $product->get_tax_class(),
'managing_stock' => $product->managing_stock(), 'managing_stock' => $product->managing_stock(),
'stock_quantity' => (int) $product->get_stock_quantity(), 'stock_quantity' => $product->get_stock_quantity(),
'in_stock' => $product->is_in_stock(), 'in_stock' => $product->is_in_stock(),
'backorders_allowed' => $product->backorders_allowed(), 'backorders_allowed' => $product->backorders_allowed(),
'backordered' => $product->is_on_backorder(), 'backordered' => $product->is_on_backorder(),
@ -311,7 +310,7 @@ class WC_API_Products extends WC_API_Resource {
'short_description' => apply_filters( 'woocommerce_short_description', $product->get_short_description() ), 'short_description' => apply_filters( 'woocommerce_short_description', $product->get_short_description() ),
'reviews_allowed' => $product->get_reviews_allowed(), 'reviews_allowed' => $product->get_reviews_allowed(),
'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ), 'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ),
'rating_count' => (int) $product->get_rating_count(), 'rating_count' => $product->get_rating_count(),
'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ), 'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ),
'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ), 'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ),
'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ), 'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ),
@ -321,11 +320,11 @@ class WC_API_Products extends WC_API_Resource {
'featured_src' => wp_get_attachment_url( get_post_thumbnail_id( $product->is_type( 'variation' ) ? $product->variation_id : $product->get_id() ) ), 'featured_src' => wp_get_attachment_url( get_post_thumbnail_id( $product->is_type( 'variation' ) ? $product->variation_id : $product->get_id() ) ),
'attributes' => $this->get_attributes( $product ), 'attributes' => $this->get_attributes( $product ),
'downloads' => $this->get_downloads( $product ), 'downloads' => $this->get_downloads( $product ),
'download_limit' => (int) $product->get_download_limit(), 'download_limit' => $product->get_download_limit(),
'download_expiry' => (int) $product->get_download_expiry(), 'download_expiry' => $product->get_download_expiry(),
'download_type' => 'standard', 'download_type' => 'standard',
'purchase_note' => apply_filters( 'the_content', $product->get_purchase_note() ), 'purchase_note' => apply_filters( 'the_content', $product->get_purchase_note() ),
'total_sales' => metadata_exists( 'post', $product->get_id(), 'total_sales' ) ? (int) get_post_meta( $product->get_id(), 'total_sales', true ) : 0, 'total_sales' => $product->get_total_sales(),
'variations' => array(), 'variations' => array(),
'parent' => array(), 'parent' => array(),
); );
@ -401,8 +400,8 @@ class WC_API_Products extends WC_API_Resource {
* @return array * @return array
*/ */
private function get_images( $product ) { private function get_images( $product ) {
$images = $attachment_ids = array();
$images = $attachment_ids = array(); $product_image = $product->get_image_id();
if ( $product->is_type( 'variation' ) ) { if ( $product->is_type( 'variation' ) ) {
// @todo variation // @todo variation
@ -411,16 +410,16 @@ class WC_API_Products extends WC_API_Resource {
// add variation image if set // add variation image if set
$attachment_ids[] = get_post_thumbnail_id( $product->get_variation_id() ); $attachment_ids[] = get_post_thumbnail_id( $product->get_variation_id() );
} elseif ( ! empty( $product->get_thumbnail_id() ) ) { } elseif ( ! empty( $product_image ) ) {
// otherwise use the parent product featured image if set // otherwise use the parent product featured image if set
$attachment_ids[] = $product->get_thumbnail_id(); $attachment_ids[] = $product_image;
} }
} else { } else {
// Add featured image // Add featured image
if ( ! empty( $product->get_thumbnail_id() ) ) { if ( ! empty( $product_image ) ) {
$attachment_ids[] = $product->get_thumbnail_id(); $attachment_ids[] = $product_image;
} }
// add gallery images // add gallery images

View File

@ -268,7 +268,7 @@ class WC_API_Products extends WC_API_Resource {
$id = $product->get_id(); $id = $product->get_id();
// Checks for an error in the product creation // Checks for an error in the product creation
if ( ! $id > 0 ) { if ( 0 >= $id ) {
throw new WC_API_Exception( 'woocommerce_api_cannot_create_product', $id->get_error_message(), 400 ); throw new WC_API_Exception( 'woocommerce_api_cannot_create_product', $id->get_error_message(), 400 );
} }
@ -679,12 +679,11 @@ class WC_API_Products extends WC_API_Resource {
*/ */
private function get_product_data( $product ) { private function get_product_data( $product ) {
$prices_precision = wc_get_price_decimals(); $prices_precision = wc_get_price_decimals();
$post = get_post( $product->get_id() );
return array( return array(
'title' => $product->get_title(), 'title' => $product->get_name(),
'id' => (int) $product->is_type( 'variation' ) ? $product->get_variation_id() : $product->get_id(), 'id' => (int) $product->is_type( 'variation' ) ? $product->get_variation_id() : $product->get_id(), // @todo variation
'created_at' => $this->server->format_datetime( $post->post_date_gmt ), 'created_at' => $this->server->format_datetime( $product->get_date_created() ),
'updated_at' => $this->server->format_datetime( $post->post_modified_gmt ), 'updated_at' => $this->server->format_datetime( $product->get_date_modified() ),
'type' => $product->get_type(), 'type' => $product->get_type(),
'status' => $product->get_status(), 'status' => $product->get_status(),
'downloadable' => $product->is_downloadable(), 'downloadable' => $product->is_downloadable(),
@ -699,7 +698,7 @@ class WC_API_Products extends WC_API_Resource {
'tax_status' => $product->get_tax_status(), 'tax_status' => $product->get_tax_status(),
'tax_class' => $product->get_tax_class(), 'tax_class' => $product->get_tax_class(),
'managing_stock' => $product->managing_stock(), 'managing_stock' => $product->managing_stock(),
'stock_quantity' => (int) $product->get_stock_quantity(), 'stock_quantity' => $product->get_stock_quantity(),
'in_stock' => $product->is_in_stock(), 'in_stock' => $product->is_in_stock(),
'backorders_allowed' => $product->backorders_allowed(), 'backorders_allowed' => $product->backorders_allowed(),
'backordered' => $product->is_on_backorder(), 'backordered' => $product->is_on_backorder(),
@ -726,7 +725,7 @@ class WC_API_Products extends WC_API_Resource {
'short_description' => apply_filters( 'woocommerce_short_description', $product->get_short_description() ), 'short_description' => apply_filters( 'woocommerce_short_description', $product->get_short_description() ),
'reviews_allowed' => $product->get_reviews_allowed(), 'reviews_allowed' => $product->get_reviews_allowed(),
'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ), 'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ),
'rating_count' => (int) $product->get_rating_count(), 'rating_count' => $product->get_rating_count(),
'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ), 'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ),
'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ), 'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ),
'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ), 'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ),
@ -734,11 +733,11 @@ class WC_API_Products extends WC_API_Resource {
'categories' => wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'names' ) ), 'categories' => wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'names' ) ),
'tags' => wp_get_post_terms( $product->get_id(), 'product_tag', array( 'fields' => 'names' ) ), 'tags' => wp_get_post_terms( $product->get_id(), 'product_tag', array( 'fields' => 'names' ) ),
'images' => $this->get_images( $product ), 'images' => $this->get_images( $product ),
'featured_src' => (string) wp_get_attachment_url( get_post_thumbnail_id( $product->is_type( 'variation' ) ? $product->variation_id : $product->get_id() ) ), 'featured_src' => wp_get_attachment_url( get_post_thumbnail_id( $product->is_type( 'variation' ) ? $product->variation_id : $product->get_id() ) ),
'attributes' => $this->get_attributes( $product ), 'attributes' => $this->get_attributes( $product ),
'downloads' => $this->get_downloads( $product ), 'downloads' => $this->get_downloads( $product ),
'download_limit' => (int) $product->get_download_limit(), 'download_limit' => $product->get_download_limit(),
'download_expiry' => (int) $product->get_download_expiry(), 'download_expiry' => $product->get_download_expiry(),
'download_type' => 'standard', 'download_type' => 'standard',
'purchase_note' => wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ), 'purchase_note' => wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ),
'total_sales' => $product->get_total_sales(), 'total_sales' => $product->get_total_sales(),
@ -928,7 +927,7 @@ class WC_API_Products extends WC_API_Resource {
$attribute_object->set_id( $attribute_id ); $attribute_object->set_id( $attribute_id );
$attribute_object->set_name( $taxonomy ); $attribute_object->set_name( $taxonomy );
$attribute_object->set_options( $values ); $attribute_object->set_options( $values );
$attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); $attribute_object->set_position( isset( $attribute['position'] ) ? absint( $attribute['position'] ) : 0 );
$attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
$attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
$attributes[] = $attribute_object; $attributes[] = $attribute_object;
@ -947,7 +946,7 @@ class WC_API_Products extends WC_API_Resource {
$attribute_object = new WC_Product_Attribute(); $attribute_object = new WC_Product_Attribute();
$attribute_object->set_name( $attribute['name'] ); $attribute_object->set_name( $attribute['name'] );
$attribute_object->set_options( $values ); $attribute_object->set_options( $values );
$attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); $attribute_object->set_position( isset( $attribute['position'] ) ? absint( $attribute['position'] ) : 0 );
$attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
$attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
$attributes[] = $attribute_object; $attributes[] = $attribute_object;
@ -1020,7 +1019,7 @@ class WC_API_Products extends WC_API_Resource {
} }
// Update parent if grouped so price sorting works and stays in sync with the cheapest child // Update parent if grouped so price sorting works and stays in sync with the cheapest child
if ( $product->get_parent_id() > 0 || 'grouped' === $product->get_type() ) { if ( $product->get_parent_id() > 0 || $product->is_type( 'grouped' ) ) {
if ( $product->get_parent_id() > 0 ) { if ( $product->get_parent_id() > 0 ) {
$parent = wc_get_product( $product->get_parent_id() ); $parent = wc_get_product( $product->get_parent_id() );
$children = $parent->get_children(); $children = $parent->get_children();
@ -1068,36 +1067,36 @@ class WC_API_Products extends WC_API_Resource {
$backorders = $product->get_backorders(); $backorders = $product->get_backorders();
} }
if ( 'grouped' == $product->get_type() ) { if ( $product->is_type( 'grouped' ) ) {
$product->set_manage_stock( 'no' ); $product->set_manage_stock( 'no' );
$product->set_backorders( 'no' ); $product->set_backorders( 'no' );
$product->set_stock( '' ); $product->set_stock_quantity( '' );
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} elseif ( 'external' == $product->get_type() ) { } elseif ( $product->is_type( 'external' ) ) {
$product->set_manage_stock( 'no' ); $product->set_manage_stock( 'no' );
$product->set_backorders( 'no' ); $product->set_backorders( 'no' );
$product->set_stock( '' ); $product->set_stock_quantity( '' );
$product->set_stock_status( 'instock' ); $product->set_stock_status( 'instock' );
} elseif ( 'yes' == $managing_stock ) { } elseif ( 'yes' == $managing_stock ) {
$product->set_backorders( $backorders ); $product->set_backorders( $backorders );
// Stock status is always determined by children so sync later. // Stock status is always determined by children so sync later.
if ( 'variable' !== $product->get_type() ) { if ( ! $product->is_type( 'variable' ) ) {
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} }
// Stock quantity // Stock quantity
if ( isset( $data['stock_quantity'] ) ) { if ( isset( $data['stock_quantity'] ) ) {
$product->set_stock( wc_stock_amount( $data['stock_quantity'] ) ); $product->set_stock_quantity( wc_stock_amount( $data['stock_quantity'] ) );
} }
} else { } else {
// Don't manage stock. // Don't manage stock.
$product->set_manage_stock( 'no' ); $product->set_manage_stock( 'no' );
$product->set_backorders( $backorders ); $product->set_backorders( $backorders );
$product->set_stock( '' ); $product->set_stock_quantity( '' );
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} }
} elseif ( 'variable' !== $product->get_type() ) { } elseif ( ! $product->is_type( 'variable' ) ) {
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} }
@ -1177,7 +1176,7 @@ class WC_API_Products extends WC_API_Resource {
} }
// Product url // Product url
if ( 'external' === $product->get_type() ) { if ( $product->is_type( 'external' ) ) {
if ( isset( $data['product_url'] ) ) { if ( isset( $data['product_url'] ) ) {
$product->set_product_url( $data['product_url'] ); $product->set_product_url( $data['product_url'] );
} }
@ -1639,25 +1638,25 @@ class WC_API_Products extends WC_API_Resource {
* @return array * @return array
*/ */
private function get_images( $product ) { private function get_images( $product ) {
$images = $attachment_ids = array();
$images = $attachment_ids = array(); $product_image = $product->get_image_id();
if ( $product->is_type( 'variation' ) ) { if ( $product->is_type( 'variation' ) ) {
// @todo variation
if ( has_post_thumbnail( $product->get_variation_id() ) ) { if ( has_post_thumbnail( $product->get_variation_id() ) ) {
// Add variation image if set // Add variation image if set
$attachment_ids[] = get_post_thumbnail_id( $product->get_variation_id() ); $attachment_ids[] = get_post_thumbnail_id( $product->get_variation_id() );
} elseif ( ! empty( $product->get_thumbnail_id() ) ) { } elseif ( ! empty( $product_image ) ) {
// Otherwise use the parent product featured image if set // Otherwise use the parent product featured image if set
$attachment_ids[] = $product->get_thumbnail_id(); $attachment_ids[] = $product_image;
} }
} else { } else {
// Add featured image // Add featured image
if ( ! empty( $product->get_thumbnail_id() ) ) { if ( ! empty( $product_image ) ) {
$attachment_ids[] = $product->get_thumbnail_id(); $attachment_ids[] = $product_image;
} }
// Add gallery images // Add gallery images
@ -1733,7 +1732,7 @@ class WC_API_Products extends WC_API_Resource {
$attachment_id = $this->set_product_image_as_attachment( $upload, $product->get_id() ); $attachment_id = $this->set_product_image_as_attachment( $upload, $product->get_id() );
} }
$product->set_thumbnail_id( $attachment_id ); $product->set_image_id( $attachment_id );
} else { } else {
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0; $attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;
@ -1752,11 +1751,11 @@ class WC_API_Products extends WC_API_Resource {
} }
if ( ! empty( $gallery ) ) { if ( ! empty( $gallery ) ) {
$product->set_gallery_attachment_ids( $gallery ); $product->set_gallery_image_ids( $gallery );
} }
} else { } else {
$product->set_thumbnail_id( '' ); $product->set_image_id( '' );
$product->set_gallery_attachment_ids( array() ); $product->set_gallery_image_ids( array() );
} }
return $product; return $product;

View File

@ -207,9 +207,12 @@ class WC_API_Products extends WC_API_Resource {
$product_data['grouped_products'] = $this->get_grouped_products_data( $product ); $product_data['grouped_products'] = $this->get_grouped_products_data( $product );
} }
if ( $product->is_type( 'simple' ) && ! empty( $product->get_parent_id() ) ) { if ( $product->is_type( 'simple' ) ) {
$_product = wc_get_product( $product->get_parent_id() ); $parent_id = $product->get_parent_id();
$product_data['parent'] = $this->get_product_data( $_product ); if ( ! empty( $parent_id ) ) {
$_product = wc_get_product( $parent_id );
$product_data['parent'] = $this->get_product_data( $_product );
}
} }
return array( 'product' => apply_filters( 'woocommerce_api_product_response', $product_data, $product, $fields, $this->server ) ); return array( 'product' => apply_filters( 'woocommerce_api_product_response', $product_data, $product, $fields, $this->server ) );
@ -319,7 +322,7 @@ class WC_API_Products extends WC_API_Resource {
$id = $product->get_id(); $id = $product->get_id();
// Checks for an error in the product creation. // Checks for an error in the product creation.
if ( ! $id > 0 ) { if ( 0 >= $id ) {
throw new WC_API_Exception( 'woocommerce_api_cannot_create_product', $id->get_error_message(), 400 ); throw new WC_API_Exception( 'woocommerce_api_cannot_create_product', $id->get_error_message(), 400 );
} }
@ -1121,12 +1124,11 @@ class WC_API_Products extends WC_API_Resource {
* @return WC_Product * @return WC_Product
*/ */
private function get_product_data( $product ) { private function get_product_data( $product ) {
$post = get_post( $product->get_id() );
return array( return array(
'title' => $product->get_title(), 'title' => $product->get_name(),
'id' => (int) $product->is_type( 'variation' ) ? $product->get_variation_id() : $product->get_id(), 'id' => (int) $product->is_type( 'variation' ) ? $product->get_variation_id() : $product->get_id(), // @todo variation
'created_at' => $this->server->format_datetime( $post->post_date_gmt ), 'created_at' => $this->server->format_datetime( $product->get_date_created() ),
'updated_at' => $this->server->format_datetime( $post->post_modified_gmt ), 'updated_at' => $this->server->format_datetime( $product->get_date_modified() ),
'type' => $product->get_type(), 'type' => $product->get_type(),
'status' => $product->get_status(), 'status' => $product->get_status(),
'downloadable' => $product->is_downloadable(), 'downloadable' => $product->is_downloadable(),
@ -1166,9 +1168,9 @@ class WC_API_Products extends WC_API_Resource {
'shipping_class_id' => ( 0 !== $product->get_shipping_class_id() ) ? $product->get_shipping_class_id() : null, 'shipping_class_id' => ( 0 !== $product->get_shipping_class_id() ) ? $product->get_shipping_class_id() : null,
'description' => wpautop( do_shortcode( $product->get_description() ) ), 'description' => wpautop( do_shortcode( $product->get_description() ) ),
'short_description' => apply_filters( 'woocommerce_short_description', $product->get_short_description() ), 'short_description' => apply_filters( 'woocommerce_short_description', $product->get_short_description() ),
'reviews_allowed' => ( 'open' === $product->get_post_data()->comment_status ), 'reviews_allowed' => $product->get_reviews_allowed(),
'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ), 'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ),
'rating_count' => (int) $product->get_rating_count(), 'rating_count' => $product->get_rating_count(),
'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ), 'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ),
'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ), 'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ),
'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ), 'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ),
@ -1176,11 +1178,11 @@ class WC_API_Products extends WC_API_Resource {
'categories' => wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'names' ) ), 'categories' => wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'names' ) ),
'tags' => wp_get_post_terms( $product->get_id(), 'product_tag', array( 'fields' => 'names' ) ), 'tags' => wp_get_post_terms( $product->get_id(), 'product_tag', array( 'fields' => 'names' ) ),
'images' => $this->get_images( $product ), 'images' => $this->get_images( $product ),
'featured_src' => (string) wp_get_attachment_url( get_post_thumbnail_id( $product->is_type( 'variation' ) ? $product->variation_id : $product->get_id() ) ), 'featured_src' => wp_get_attachment_url( get_post_thumbnail_id( $product->is_type( 'variation' ) ? $product->variation_id : $product->get_id() ) ),
'attributes' => $this->get_attributes( $product ), 'attributes' => $this->get_attributes( $product ),
'downloads' => $this->get_downloads( $product ), 'downloads' => $this->get_downloads( $product ),
'download_limit' => (int) $product->get_download_limit(), 'download_limit' => $product->get_download_limit(),
'download_expiry' => (int) $product->get_download_expiry(), 'download_expiry' => $product->get_download_expiry(),
'download_type' => 'standard', 'download_type' => 'standard',
'purchase_note' => wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ), 'purchase_note' => wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ),
'total_sales' => $product->get_total_sales(), 'total_sales' => $product->get_total_sales(),
@ -1415,7 +1417,7 @@ class WC_API_Products extends WC_API_Resource {
$attribute_object->set_id( $attribute_id ); $attribute_object->set_id( $attribute_id );
$attribute_object->set_name( $taxonomy ); $attribute_object->set_name( $taxonomy );
$attribute_object->set_options( $values ); $attribute_object->set_options( $values );
$attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); $attribute_object->set_position( isset( $attribute['position'] ) ? absint( $attribute['position'] ) : 0 );
$attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
$attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
$attributes[] = $attribute_object; $attributes[] = $attribute_object;
@ -1434,7 +1436,7 @@ class WC_API_Products extends WC_API_Resource {
$attribute_object = new WC_Product_Attribute(); $attribute_object = new WC_Product_Attribute();
$attribute_object->set_name( $attribute['name'] ); $attribute_object->set_name( $attribute['name'] );
$attribute_object->set_options( $values ); $attribute_object->set_options( $values );
$attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); $attribute_object->set_position( isset( $attribute['position'] ) ? absint( $attribute['position'] ) : 0 );
$attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
$attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
$attributes[] = $attribute_object; $attributes[] = $attribute_object;
@ -1506,7 +1508,7 @@ class WC_API_Products extends WC_API_Resource {
} }
// Update parent if grouped so price sorting works and stays in sync with the cheapest child. // Update parent if grouped so price sorting works and stays in sync with the cheapest child.
if ( $product->get_parent_id() > 0 || 'grouped' === $product->get_type() ) { if ( $product->get_parent_id() > 0 || $product->is_type( 'grouped' ) ) {
if ( $product->get_parent_id() > 0 ) { if ( $product->get_parent_id() > 0 ) {
$parent = wc_get_product( $product->get_parent_id() ); $parent = wc_get_product( $product->get_parent_id() );
$children = $parent->get_children(); $children = $parent->get_children();
@ -1554,40 +1556,40 @@ class WC_API_Products extends WC_API_Resource {
$backorders = $product->get_backorders(); $backorders = $product->get_backorders();
} }
if ( 'grouped' == $product->get_type() ) { if ( $product->is_type( 'grouped' ) ) {
$product->set_manage_stock( 'no' ); $product->set_manage_stock( 'no' );
$product->set_backorders( 'no' ); $product->set_backorders( 'no' );
$product->set_stock( '' ); $product->set_stock_quantity( '' );
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} elseif ( 'external' == $product->get_type() ) { } elseif ( $product->is_type( 'external' ) ) {
$product->set_manage_stock( 'no' ); $product->set_manage_stock( 'no' );
$product->set_backorders( 'no' ); $product->set_backorders( 'no' );
$product->set_stock( '' ); $product->set_stock_quantity( '' );
$product->set_stock_status( 'instock' ); $product->set_stock_status( 'instock' );
} elseif ( 'yes' == $managing_stock ) { } elseif ( 'yes' == $managing_stock ) {
$product->set_backorders( $backorders ); $product->set_backorders( $backorders );
// Stock status is always determined by children so sync later. // Stock status is always determined by children so sync later.
if ( 'variable' !== $product->get_type() ) { if ( ! $product->is_type( 'variable' ) ) {
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} }
// Stock quantity. // Stock quantity.
if ( isset( $data['stock_quantity'] ) ) { if ( isset( $data['stock_quantity'] ) ) {
$product->set_stock( wc_stock_amount( $data['stock_quantity'] ) ); $product->set_stock_quantity( wc_stock_amount( $data['stock_quantity'] ) );
} else if ( isset( $data['inventory_delta'] ) ) { } else if ( isset( $data['inventory_delta'] ) ) {
$stock_quantity = wc_stock_amount( $product->get_stock() ); $stock_quantity = wc_stock_amount( $product->get_stock() );
$stock_quantity += wc_stock_amount( $data['inventory_delta'] ); $stock_quantity += wc_stock_amount( $data['inventory_delta'] );
$product->set_stock( wc_stock_amount( $stock_quantity ) ); $product->set_stock_quantity( wc_stock_amount( $stock_quantity ) );
} }
} else { } else {
// Don't manage stock. // Don't manage stock.
$product->set_manage_stock( 'no' ); $product->set_manage_stock( 'no' );
$product->set_backorders( $backorders ); $product->set_backorders( $backorders );
$product->set_stock( '' ); $product->set_stock_quantity( '' );
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} }
} elseif ( 'variable' !== $product->get_type() ) { } elseif ( ! $product->is_type( 'variable' ) ) {
$product->set_stock_status( $stock_status ); $product->set_stock_status( $stock_status );
} }
@ -1667,7 +1669,7 @@ class WC_API_Products extends WC_API_Resource {
} }
// Product url. // Product url.
if ( 'external' === $product->get_type() ) { if ( $product->is_type( 'external' ) ) {
if ( isset( $data['product_url'] ) ) { if ( isset( $data['product_url'] ) ) {
$product->set_product_url( $data['product_url'] ); $product->set_product_url( $data['product_url'] );
} }
@ -2161,8 +2163,8 @@ class WC_API_Products extends WC_API_Resource {
* @return array * @return array
*/ */
private function get_images( $product ) { private function get_images( $product ) {
$images = $attachment_ids = array();
$images = $attachment_ids = array(); $product_image = $product->get_image_id();
if ( $product->is_type( 'variation' ) ) { if ( $product->is_type( 'variation' ) ) {
// @todo variation // @todo variation
@ -2171,15 +2173,15 @@ class WC_API_Products extends WC_API_Resource {
// Add variation image if set // Add variation image if set
$attachment_ids[] = get_post_thumbnail_id( $product->get_variation_id() ); $attachment_ids[] = get_post_thumbnail_id( $product->get_variation_id() );
} elseif ( ! empty( $product->get_thumbnail_id() ) ) { } elseif ( ! empty( $product_image ) ) {
// Otherwise use the parent product featured image if set // Otherwise use the parent product featured image if set
$attachment_ids[] = $product->get_thumbnail_id(); $attachment_ids[] = $product_image;
} }
} else { } else {
// Add featured image // Add featured image
if ( ! empty( $product->get_thumbnail_id() ) ) { if ( ! empty( $product_image ) ) {
$attachment_ids[] = $product->get_thumbnail_id(); $attachment_ids[] = $product_image;
} }
// Add gallery images // Add gallery images
@ -2256,7 +2258,7 @@ class WC_API_Products extends WC_API_Resource {
$attachment_id = $this->set_product_image_as_attachment( $upload, $product->get_id() ); $attachment_id = $this->set_product_image_as_attachment( $upload, $product->get_id() );
} }
$product->set_thumbnail_id( $attachment_id ); $product->set_image_id( $attachment_id );
} else { } else {
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0; $attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;
@ -2285,11 +2287,11 @@ class WC_API_Products extends WC_API_Resource {
} }
if ( ! empty( $gallery ) ) { if ( ! empty( $gallery ) ) {
$product->set_gallery_attachment_ids( $gallery ); $product->set_gallery_image_ids( $gallery );
} }
} else { } else {
$product->set_thumbnail_id( '' ); $product->set_image_id( '' );
$product->set_gallery_attachment_ids( array() ); $product->set_gallery_image_ids( array() );
} }
return $product; return $product;

View File

@ -178,7 +178,9 @@ class WC_Product_Grouped extends WC_Product {
$child_prices = array(); $child_prices = array();
foreach ( $this->get_children() as $child_id ) { foreach ( $this->get_children() as $child_id ) {
$child = wc_get_product( $child_id ); $child = wc_get_product( $child_id );
$child_prices[] = $child->get_price(); if ( $child ) {
$child_prices[] = $child->get_price();
}
} }
$child_prices = array_filter( $child_prices ); $child_prices = array_filter( $child_prices );
delete_post_meta( $this->get_id(), '_price' ); delete_post_meta( $this->get_id(), '_price' );