Fetch product attrs only when needed

This commit is contained in:
vedanshujain 2020-06-05 02:05:12 +05:30
parent 13bee82ed9
commit 04f948951a
2 changed files with 221 additions and 71 deletions

View File

@ -637,74 +637,202 @@ class WC_REST_Products_V2_Controller extends WC_REST_CRUD_Controller {
if ( $request instanceof WP_REST_Request ) { if ( $request instanceof WP_REST_Request ) {
$fields = $this->get_fields_for_response( $request ); $fields = $this->get_fields_for_response( $request );
} }
$base_data = array( $base_data = array();
'id' => $product->get_id(), foreach ( $fields as $field ) {
'name' => $product->get_name( $context ), switch ( $field ) {
'slug' => $product->get_slug( $context ), case 'id':
'permalink' => $product->get_permalink(), $base_data['id'] = $product->get_id();
'date_created' => wc_rest_prepare_date_response( $product->get_date_created( $context ), false ), break;
'date_created_gmt' => wc_rest_prepare_date_response( $product->get_date_created( $context ) ), case 'name':
'date_modified' => wc_rest_prepare_date_response( $product->get_date_modified( $context ), false ), $base_data['name'] = $product->get_name( $context );
'date_modified_gmt' => wc_rest_prepare_date_response( $product->get_date_modified( $context ) ), break;
'type' => $product->get_type(), case 'slug':
'status' => $product->get_status( $context ), $base_data['slug'] = $product->get_slug( $context );
'featured' => $product->is_featured(), break;
'catalog_visibility' => $product->get_catalog_visibility( $context ),
'description' => 'view' === $context ? wpautop( do_shortcode( $product->get_description() ) ) : $product->get_description( $context ), case 'permalink':
'short_description' => 'view' === $context ? apply_filters( 'woocommerce_short_description', $product->get_short_description() ) : $product->get_short_description( $context ), $base_data['permalink'] = $product->get_permalink();
'sku' => $product->get_sku( $context ), break;
'price' => $product->get_price( $context ), case 'date_created':
'regular_price' => $product->get_regular_price( $context ), $base_data['date_created'] = wc_rest_prepare_date_response( $product->get_date_created( $context ), false );
'sale_price' => $product->get_sale_price( $context ) ? $product->get_sale_price( $context ) : '', break;
'date_on_sale_from' => wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ), false ), case 'date_created_gmt':
'date_on_sale_from_gmt' => wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ) ), $base_data['date_created_gmt'] = wc_rest_prepare_date_response( $product->get_date_created( $context ) );
'date_on_sale_to' => wc_rest_prepare_date_response( $product->get_date_on_sale_to( $context ), false ), break;
'date_on_sale_to_gmt' => wc_rest_prepare_date_response( $product->get_date_on_sale_to( $context ) ), case 'date_modified':
'on_sale' => $product->is_on_sale( $context ), $base_data['date_modified'] = wc_rest_prepare_date_response( $product->get_date_modified( $context ), false );
'purchasable' => $product->is_purchasable(), break;
'total_sales' => $product->get_total_sales( $context ), case 'date_modified_gmt':
'virtual' => $product->is_virtual(), $base_data['date_modified_gmt'] = wc_rest_prepare_date_response( $product->get_date_modified( $context ) );
'downloadable' => $product->is_downloadable(), break;
'downloads' => $this->get_downloads( $product ), case 'type':
'download_limit' => $product->get_download_limit( $context ), $base_data['type'] = $product->get_type();
'download_expiry' => $product->get_download_expiry( $context ), break;
'external_url' => $product->is_type( 'external' ) ? $product->get_product_url( $context ) : '', case 'status':
'button_text' => $product->is_type( 'external' ) ? $product->get_button_text( $context ) : '', $base_data['status'] = $product->get_status( $context );
'tax_status' => $product->get_tax_status( $context ), break;
'tax_class' => $product->get_tax_class( $context ), case 'featured':
'manage_stock' => $product->managing_stock(), $base_data['featured'] = $product->is_featured();
'stock_quantity' => $product->get_stock_quantity( $context ), break;
'in_stock' => $product->is_in_stock(), case 'catalog_visibility':
'backorders' => $product->get_backorders( $context ), $base_data['catalog_visibility'] = $product->get_catalog_visibility( $context );
'backorders_allowed' => $product->backorders_allowed(), break;
'backordered' => $product->is_on_backorder(), case 'description':
'sold_individually' => $product->is_sold_individually(), $base_data['description'] = 'view' === $context ? wpautop( do_shortcode( $product->get_description() ) ) : $product->get_description( $context );
'weight' => $product->get_weight( $context ), break;
'dimensions' => array( case 'short_description':
'length' => $product->get_length( $context ), $base_data['short_description'] = 'view' === $context ? apply_filters( 'woocommerce_short_description', $product->get_short_description() ) : $product->get_short_description( $context );
'width' => $product->get_width( $context ), break;
'height' => $product->get_height( $context ), case 'sku':
), $base_data['sku'] = $product->get_sku( $context );
'shipping_required' => $product->needs_shipping(), break;
'shipping_taxable' => $product->is_shipping_taxable(), case 'price':
'shipping_class' => $product->get_shipping_class(), $base_data['price'] = $product->get_price( $context );
'shipping_class_id' => $product->get_shipping_class_id( $context ), break;
'reviews_allowed' => $product->get_reviews_allowed( $context ), case 'regular_price':
'average_rating' => 'view' === $context ? wc_format_decimal( $product->get_average_rating(), 2 ) : $product->get_average_rating( $context ), $base_data['regular_price'] = $product->get_regular_price( $context );
'rating_count' => $product->get_rating_count(), break;
'upsell_ids' => array_map( 'absint', $product->get_upsell_ids( $context ) ), case 'sale_price':
'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids( $context ) ), $base_data['sale_price'] = $product->get_sale_price( $context ) ? $product->get_sale_price( $context ) : '';
'parent_id' => $product->get_parent_id( $context ), break;
'purchase_note' => 'view' === $context ? wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ) : $product->get_purchase_note( $context ), case 'date_on_sale_from':
'categories' => $this->get_taxonomy_terms( $product ), $base_data['date_on_sale_from'] = wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ), false );
'tags' => $this->get_taxonomy_terms( $product, 'tag' ), break;
'images' => $this->get_images( $product ), case 'date_on_sale_from_gmt':
'attributes' => $this->get_attributes( $product ), $base_data['date_on_sale_from_gmt'] = wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ) );
'default_attributes' => $this->get_default_attributes( $product ), break;
'variations' => array(), case 'date_on_sale_to':
'grouped_products' => array(), $base_data['date_on_sale_to'] = wc_rest_prepare_date_response( $product->get_date_on_sale_to( $context ), false );
'menu_order' => $product->get_menu_order( $context ), 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 ),
);
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( $data = array_merge(
$base_data, $base_data,

View File

@ -217,6 +217,25 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
return $args; 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. * Set product images.
* *
@ -1336,9 +1355,12 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
// Replace in_stock with stock_status. // Replace in_stock with stock_status.
$pos = array_search( 'in_stock', array_keys( $data ), true ); $pos = array_search( 'in_stock', array_keys( $data ), true );
$array_section_1 = array_slice( $data, 0, $pos, true ); if ( false !== $pos ) {
$array_section_2 = array_slice( $data, $pos + 1, null, true ); $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;
} }
} }