Fetch product attrs only when needed
This commit is contained in:
parent
13bee82ed9
commit
04f948951a
|
@ -637,74 +637,202 @@ class WC_REST_Products_V2_Controller extends WC_REST_CRUD_Controller {
|
|||
if ( $request instanceof WP_REST_Request ) {
|
||||
$fields = $this->get_fields_for_response( $request );
|
||||
}
|
||||
$base_data = array(
|
||||
'id' => $product->get_id(),
|
||||
'name' => $product->get_name( $context ),
|
||||
'slug' => $product->get_slug( $context ),
|
||||
'permalink' => $product->get_permalink(),
|
||||
'date_created' => wc_rest_prepare_date_response( $product->get_date_created( $context ), false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $product->get_date_created( $context ) ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $product->get_date_modified( $context ), false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $product->get_date_modified( $context ) ),
|
||||
'type' => $product->get_type(),
|
||||
'status' => $product->get_status( $context ),
|
||||
'featured' => $product->is_featured(),
|
||||
'catalog_visibility' => $product->get_catalog_visibility( $context ),
|
||||
'description' => 'view' === $context ? wpautop( do_shortcode( $product->get_description() ) ) : $product->get_description( $context ),
|
||||
'short_description' => 'view' === $context ? apply_filters( 'woocommerce_short_description', $product->get_short_description() ) : $product->get_short_description( $context ),
|
||||
'sku' => $product->get_sku( $context ),
|
||||
'price' => $product->get_price( $context ),
|
||||
'regular_price' => $product->get_regular_price( $context ),
|
||||
'sale_price' => $product->get_sale_price( $context ) ? $product->get_sale_price( $context ) : '',
|
||||
'date_on_sale_from' => wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ), false ),
|
||||
'date_on_sale_from_gmt' => wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ) ),
|
||||
'date_on_sale_to' => wc_rest_prepare_date_response( $product->get_date_on_sale_to( $context ), false ),
|
||||
'date_on_sale_to_gmt' => wc_rest_prepare_date_response( $product->get_date_on_sale_to( $context ) ),
|
||||
'on_sale' => $product->is_on_sale( $context ),
|
||||
'purchasable' => $product->is_purchasable(),
|
||||
'total_sales' => $product->get_total_sales( $context ),
|
||||
'virtual' => $product->is_virtual(),
|
||||
'downloadable' => $product->is_downloadable(),
|
||||
'downloads' => $this->get_downloads( $product ),
|
||||
'download_limit' => $product->get_download_limit( $context ),
|
||||
'download_expiry' => $product->get_download_expiry( $context ),
|
||||
'external_url' => $product->is_type( 'external' ) ? $product->get_product_url( $context ) : '',
|
||||
'button_text' => $product->is_type( 'external' ) ? $product->get_button_text( $context ) : '',
|
||||
'tax_status' => $product->get_tax_status( $context ),
|
||||
'tax_class' => $product->get_tax_class( $context ),
|
||||
'manage_stock' => $product->managing_stock(),
|
||||
'stock_quantity' => $product->get_stock_quantity( $context ),
|
||||
'in_stock' => $product->is_in_stock(),
|
||||
'backorders' => $product->get_backorders( $context ),
|
||||
'backorders_allowed' => $product->backorders_allowed(),
|
||||
'backordered' => $product->is_on_backorder(),
|
||||
'sold_individually' => $product->is_sold_individually(),
|
||||
'weight' => $product->get_weight( $context ),
|
||||
'dimensions' => array(
|
||||
$base_data = array();
|
||||
foreach ( $fields as $field ) {
|
||||
switch ( $field ) {
|
||||
case 'id':
|
||||
$base_data['id'] = $product->get_id();
|
||||
break;
|
||||
case 'name':
|
||||
$base_data['name'] = $product->get_name( $context );
|
||||
break;
|
||||
case 'slug':
|
||||
$base_data['slug'] = $product->get_slug( $context );
|
||||
break;
|
||||
|
||||
case 'permalink':
|
||||
$base_data['permalink'] = $product->get_permalink();
|
||||
break;
|
||||
case 'date_created':
|
||||
$base_data['date_created'] = wc_rest_prepare_date_response( $product->get_date_created( $context ), false );
|
||||
break;
|
||||
case 'date_created_gmt':
|
||||
$base_data['date_created_gmt'] = wc_rest_prepare_date_response( $product->get_date_created( $context ) );
|
||||
break;
|
||||
case 'date_modified':
|
||||
$base_data['date_modified'] = wc_rest_prepare_date_response( $product->get_date_modified( $context ), false );
|
||||
break;
|
||||
case 'date_modified_gmt':
|
||||
$base_data['date_modified_gmt'] = wc_rest_prepare_date_response( $product->get_date_modified( $context ) );
|
||||
break;
|
||||
case 'type':
|
||||
$base_data['type'] = $product->get_type();
|
||||
break;
|
||||
case 'status':
|
||||
$base_data['status'] = $product->get_status( $context );
|
||||
break;
|
||||
case 'featured':
|
||||
$base_data['featured'] = $product->is_featured();
|
||||
break;
|
||||
case 'catalog_visibility':
|
||||
$base_data['catalog_visibility'] = $product->get_catalog_visibility( $context );
|
||||
break;
|
||||
case 'description':
|
||||
$base_data['description'] = 'view' === $context ? wpautop( do_shortcode( $product->get_description() ) ) : $product->get_description( $context );
|
||||
break;
|
||||
case 'short_description':
|
||||
$base_data['short_description'] = 'view' === $context ? apply_filters( 'woocommerce_short_description', $product->get_short_description() ) : $product->get_short_description( $context );
|
||||
break;
|
||||
case 'sku':
|
||||
$base_data['sku'] = $product->get_sku( $context );
|
||||
break;
|
||||
case 'price':
|
||||
$base_data['price'] = $product->get_price( $context );
|
||||
break;
|
||||
case 'regular_price':
|
||||
$base_data['regular_price'] = $product->get_regular_price( $context );
|
||||
break;
|
||||
case 'sale_price':
|
||||
$base_data['sale_price'] = $product->get_sale_price( $context ) ? $product->get_sale_price( $context ) : '';
|
||||
break;
|
||||
case 'date_on_sale_from':
|
||||
$base_data['date_on_sale_from'] = wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ), false );
|
||||
break;
|
||||
case 'date_on_sale_from_gmt':
|
||||
$base_data['date_on_sale_from_gmt'] = wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ) );
|
||||
break;
|
||||
case 'date_on_sale_to':
|
||||
$base_data['date_on_sale_to'] = wc_rest_prepare_date_response( $product->get_date_on_sale_to( $context ), false );
|
||||
break;
|
||||
case 'date_on_sale_to_gmt':
|
||||
$base_data['date_on_sale_to_gmt'] = wc_rest_prepare_date_response( $product->get_date_on_sale_to( $context ) );
|
||||
break;
|
||||
case 'on_sale':
|
||||
$base_data['on_sale'] = $product->is_on_sale( $context );
|
||||
break;
|
||||
case 'purchasable':
|
||||
$base_data['purchasable'] = $product->is_purchasable();
|
||||
break;
|
||||
case 'total_sales':
|
||||
$base_data['total_sales'] = $product->get_total_sales( $context );
|
||||
break;
|
||||
case 'virtual':
|
||||
$base_data['virtual'] = $product->is_virtual();
|
||||
break;
|
||||
case 'downloadable':
|
||||
$base_data['downloadable'] = $product->is_downloadable();
|
||||
break;
|
||||
case 'downloads':
|
||||
$base_data['downloads'] = $this->get_downloads( $product );
|
||||
break;
|
||||
case 'download_limit':
|
||||
$base_data['download_limit'] = $product->get_download_limit( $context );
|
||||
break;
|
||||
case 'download_expiry':
|
||||
$base_data['download_expiry'] = $product->get_download_expiry( $context );
|
||||
break;
|
||||
case 'external_url':
|
||||
$base_data['external_url'] = $product->is_type( 'external' ) ? $product->get_product_url( $context ) : '';
|
||||
break;
|
||||
case 'button_text':
|
||||
$base_data['button_text'] = $product->is_type( 'external' ) ? $product->get_button_text( $context ) : '';
|
||||
break;
|
||||
case 'tax_status':
|
||||
$base_data['tax_status'] = $product->get_tax_status( $context );
|
||||
break;
|
||||
case 'tax_class':
|
||||
$base_data['tax_class'] = $product->get_tax_class( $context );
|
||||
break;
|
||||
case 'manage_stock':
|
||||
$base_data['manage_stock'] = $product->managing_stock();
|
||||
break;
|
||||
case 'stock_quantity':
|
||||
$base_data['stock_quantity'] = $product->get_stock_quantity( $context );
|
||||
break;
|
||||
case 'in_stock':
|
||||
$base_data['in_stock'] = $product->is_in_stock();
|
||||
break;
|
||||
case 'backorders':
|
||||
$base_data['backorders'] = $product->get_backorders( $context );
|
||||
break;
|
||||
case 'backorders_allowed':
|
||||
$base_data['backorders_allowed'] = $product->backorders_allowed();
|
||||
break;
|
||||
case 'backordered':
|
||||
$base_data['backordered'] = $product->is_on_backorder();
|
||||
break;
|
||||
case 'sold_individually':
|
||||
$base_data['sold_individually'] = $product->is_sold_individually();
|
||||
break;
|
||||
case 'weight':
|
||||
$base_data['weight'] = $product->get_weight( $context );
|
||||
break;
|
||||
case 'dimensions':
|
||||
$base_data['dimensions'] = array(
|
||||
'length' => $product->get_length( $context ),
|
||||
'width' => $product->get_width( $context ),
|
||||
'height' => $product->get_height( $context ),
|
||||
),
|
||||
'shipping_required' => $product->needs_shipping(),
|
||||
'shipping_taxable' => $product->is_shipping_taxable(),
|
||||
'shipping_class' => $product->get_shipping_class(),
|
||||
'shipping_class_id' => $product->get_shipping_class_id( $context ),
|
||||
'reviews_allowed' => $product->get_reviews_allowed( $context ),
|
||||
'average_rating' => 'view' === $context ? wc_format_decimal( $product->get_average_rating(), 2 ) : $product->get_average_rating( $context ),
|
||||
'rating_count' => $product->get_rating_count(),
|
||||
'upsell_ids' => array_map( 'absint', $product->get_upsell_ids( $context ) ),
|
||||
'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids( $context ) ),
|
||||
'parent_id' => $product->get_parent_id( $context ),
|
||||
'purchase_note' => 'view' === $context ? wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ) : $product->get_purchase_note( $context ),
|
||||
'categories' => $this->get_taxonomy_terms( $product ),
|
||||
'tags' => $this->get_taxonomy_terms( $product, 'tag' ),
|
||||
'images' => $this->get_images( $product ),
|
||||
'attributes' => $this->get_attributes( $product ),
|
||||
'default_attributes' => $this->get_default_attributes( $product ),
|
||||
'variations' => array(),
|
||||
'grouped_products' => array(),
|
||||
'menu_order' => $product->get_menu_order( $context ),
|
||||
);
|
||||
break;
|
||||
case 'shipping_required':
|
||||
$base_data['shipping_required'] = $product->needs_shipping();
|
||||
break;
|
||||
case 'shipping_taxable':
|
||||
$base_data['shipping_taxable'] = $product->is_shipping_taxable();
|
||||
break;
|
||||
case 'shipping_class':
|
||||
$base_data['shipping_class'] = $product->get_shipping_class();
|
||||
break;
|
||||
case 'shipping_class_id':
|
||||
$base_data['shipping_class_id'] = $product->get_shipping_class_id( $context );
|
||||
break;
|
||||
case 'reviews_allowed':
|
||||
$base_data['reviews_allowed'] = $product->get_reviews_allowed( $context );
|
||||
break;
|
||||
case 'average_rating':
|
||||
$base_data['average_rating'] = 'view' === $context ? wc_format_decimal( $product->get_average_rating(), 2 ) : $product->get_average_rating( $context );
|
||||
break;
|
||||
case 'rating_count':
|
||||
$base_data['rating_count'] = $product->get_rating_count();
|
||||
break;
|
||||
case 'upsell_ids':
|
||||
$base_data['upsell_ids'] = array_map( 'absint', $product->get_upsell_ids( $context ) );
|
||||
break;
|
||||
case 'cross_sell_ids':
|
||||
$base_data['cross_sell_ids'] = array_map( 'absint', $product->get_cross_sell_ids( $context ) );
|
||||
break;
|
||||
case 'parent_id':
|
||||
$base_data['parent_id'] = $product->get_parent_id( $context );
|
||||
break;
|
||||
case 'purchase_note':
|
||||
$base_data['purchase_note'] = 'view' === $context ? wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ) : $product->get_purchase_note( $context );
|
||||
break;
|
||||
case 'categories':
|
||||
$base_data['categories'] = $this->get_taxonomy_terms( $product );
|
||||
break;
|
||||
case 'tags':
|
||||
$base_data['tags'] = $this->get_taxonomy_terms( $product, 'tag' );
|
||||
break;
|
||||
case 'images':
|
||||
$base_data['images'] = $this->get_images( $product );
|
||||
break;
|
||||
case 'attributes':
|
||||
$base_data['attributes'] = $this->get_attributes( $product );
|
||||
break;
|
||||
case 'default_attributes':
|
||||
$base_data['default_attributes'] = $this->get_default_attributes( $product );
|
||||
break;
|
||||
case 'variations':
|
||||
$base_data['variations'] = array();
|
||||
break;
|
||||
case 'grouped_products':
|
||||
$base_data['grouped_products'] = array();
|
||||
break;
|
||||
case 'menu_order':
|
||||
$base_data['menu_order'] = $product->get_menu_order( $context );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$data = array_merge(
|
||||
$base_data,
|
||||
|
|
|
@ -217,6 +217,25 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
|||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get products.
|
||||
*
|
||||
* @param array $query_args Query args.
|
||||
*
|
||||
* @return array Products.
|
||||
*/
|
||||
protected function get_objects( $query_args ) {
|
||||
$query_args['paginate'] = true;
|
||||
$query_args['return'] = 'objects';
|
||||
$results = wc_get_products( $query_args );
|
||||
|
||||
return array(
|
||||
'objects' => $results->products,
|
||||
'total' => $results->total,
|
||||
'pages' => $results->max_num_pages,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set product images.
|
||||
*
|
||||
|
@ -1336,9 +1355,12 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
|||
|
||||
// Replace in_stock with stock_status.
|
||||
$pos = array_search( 'in_stock', array_keys( $data ), true );
|
||||
if ( false !== $pos ) {
|
||||
$array_section_1 = array_slice( $data, 0, $pos, true );
|
||||
$array_section_2 = array_slice( $data, $pos + 1, null, true );
|
||||
$data = $array_section_1 + array( 'stock_status' => $product->get_stock_status( $context ) ) + $array_section_2;
|
||||
}
|
||||
|
||||
return $array_section_1 + array( 'stock_status' => $product->get_stock_status( $context ) ) + $array_section_2;
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue