*/ private $term_index = []; /** * @param WP_Term[] $all_terms Pre-fetched taxonomy terms. */ public function __construct(array $all_terms) { foreach ($all_terms as $term) { $this->term_index[$term->taxonomy . ':' . $term->slug] = $term; } parent::__construct(); } /** * Prepare variation data for REST response. * * @param WC_Product_Variation $object Variation object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function prepare_object_for_response($object, $request) { $context = !empty($request['context']) ? $request['context'] : 'view'; $data = [ 'id' => $object->get_id(), 'type' => $object->get_type(), // 'date_created' => wc_rest_prepare_date_response($object->get_date_created(), false), // 'date_created_gmt' => wc_rest_prepare_date_response($object->get_date_created()), 'date_modified' => wc_rest_prepare_date_response($object->get_date_modified(), false), // 'date_modified_gmt' => wc_rest_prepare_date_response($object->get_date_modified()), // 'description' => wc_format_content($object->get_description()), // 'permalink' => $object->get_permalink(), // 'sku' => $object->get_sku(), 'price' => $object->get_price(), 'regular_price' => $object->get_regular_price(), 'sale_price' => $object->get_sale_price(), // 'date_on_sale_from' => wc_rest_prepare_date_response($object->get_date_on_sale_from(), false), // 'date_on_sale_from_gmt' => wc_rest_prepare_date_response($object->get_date_on_sale_from()), // 'date_on_sale_to' => wc_rest_prepare_date_response($object->get_date_on_sale_to(), false), // 'date_on_sale_to_gmt' => wc_rest_prepare_date_response($object->get_date_on_sale_to()), 'on_sale' => $object->is_on_sale(), 'status' => $object->get_status(), 'purchasable' => $object->is_purchasable(), // 'virtual' => $object->is_virtual(), // 'downloadable' => $object->is_downloadable(), // 'downloads' => $this->get_downloads($object, $context), // 'download_limit' => '' !== $object->get_download_limit() ? (int) $object->get_download_limit() : -1, // 'download_expiry' => '' !== $object->get_download_expiry() ? (int) $object->get_download_expiry() : -1, // 'tax_status' => $object->get_tax_status(), // 'tax_class' => $object->get_tax_class($context), 'manage_stock' => $object->managing_stock(), 'stock_quantity' => $object->get_stock_quantity(), 'stock_status' => $object->get_stock_status(), // 'backorders' => $object->get_backorders(), // 'backorders_allowed' => $object->backorders_allowed(), // 'backordered' => $object->is_on_backorder(), 'low_stock_amount' => '' === $object->get_low_stock_amount() ? null : $object->get_low_stock_amount(), // 'weight' => $object->get_weight(), // 'dimensions' => [ // 'length' => $object->get_length(), // 'width' => $object->get_width(), // 'height' => $object->get_height(), // ], // 'shipping_class' => $object->get_shipping_class(), // 'shipping_class_id' => $object->get_shipping_class_id(), 'image' => $this->get_image($object, $context), 'attributes' => $this->get_attributes($object), // 'menu_order' => $object->get_menu_order(), // 'meta_data' => $object->get_meta_data(), 'parent_id' => $object->get_parent_id(), ]; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); $response = rest_ensure_response($data); $response->add_links($this->prepare_links($object, $request)); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WooCommerce core filter. return apply_filters("woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request); } /** * Build variation attributes using the pre-indexed term map. * * @param WC_Product_Variation $product Variation object. * @return array[] */ protected function get_attributes($product) { $attributes = []; $_product = wc_get_product($product->get_parent_id()); foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { $name = str_replace('attribute_', '', $attribute_name); if (empty($attribute) && '0' !== $attribute) { continue; } if (0 === strpos($attribute_name, 'attribute_pa_')) { $option_term = $this->term_index[$name . ':' . $attribute] ?? false; $attributes[] = [ 'id' => wc_attribute_taxonomy_id_by_name($name), 'name' => $this->get_attribute_taxonomy_name($name, $_product), 'slug' => $name, 'option' => $option_term && !is_wp_error($option_term) ? $option_term->name : $attribute, ]; } else { $attributes[] = [ 'id' => 0, 'name' => $this->get_attribute_taxonomy_name($name, $_product), 'slug' => $name, 'option' => $attribute, ]; } } return $attributes; } }