WC_REST_Products_V2_Controller::get_product_data() │ protected │ WC 1.0
Get product data.
Method of the class: WC_REST_Products_V2_Controller{}
Hooks from the method
Return
Array
.
Usage
// protected - for code of main (parent) or child class $result = $this->get_product_data( $product, $context );
- $product(WC_Product) (required)
- Product instance.
- $context(string)
- Request context. Options: 'view' and 'edit'.
Default: 'view'
WC_REST_Products_V2_Controller::get_product_data() WC REST Products V2 Controller::get product data code WC 7.7.0
protected function get_product_data( $product, $context = 'view' ) { /* * @param WP_REST_Request $request Current request object. For backward compatibility, we pass this argument silently. * * TODO: Refactor to fix this behavior when DI gets included to make it obvious and clean. */ $request = func_num_args() >= 3 ? func_get_arg( 2 ) : new WP_REST_Request( '', '', array( 'context' => $context ) ); $fields = $this->get_fields_for_response( $request ); $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 'low_stock_amount': $base_data['low_stock_amount'] = '' === $product->get_low_stock_amount() ? null : $product->get_low_stock_amount(); 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( $base_data, $this->fetch_fields_using_getters( $product, $context, $fields ) ); return $data; }