WC_REST_Products_Controller::get_product_data()protectedWC 1.0

Get product data.

Метод класса: WC_REST_Products_Controller{}

Хуков нет.

Возвращает

Массив.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_product_data( $product, $context );
$product(WC_Product) (обязательный)
Product instance.
$context(строка)
Request context. Options: 'view' and 'edit'.
По умолчанию: 'view'

Код WC_REST_Products_Controller::get_product_data() WC 8.9.3

protected function get_product_data( $product, $context = 'view' ) {
	$data = parent::get_product_data( ...func_get_args() );

	if ( isset( $this->request ) ) {
		$fields = $this->get_fields_for_response( $this->request );

		// Add stock_status if needed.
		if ( in_array( 'stock_status', $fields, true ) ) {
			$data['stock_status'] = $product->get_stock_status( $context );
		}

		// Add has_options if needed.
		if ( in_array( 'has_options', $fields, true ) ) {
			$data['has_options'] = $product->has_options( $context );
		}

		if ( in_array( 'post_password', $fields, true ) ) {
			$data['post_password'] = $product->get_post_password( $context );
		}

		$post_type_obj = get_post_type_object( $this->post_type );
		if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
			$permalink_template_requested = in_array( 'permalink_template', $fields, true );
			$generated_slug_requested     = in_array( 'generated_slug', $fields, true );

			if ( $permalink_template_requested || $generated_slug_requested ) {
				if ( ! function_exists( 'get_sample_permalink' ) ) {
					require_once ABSPATH . 'wp-admin/includes/post.php';
				}

				$sample_permalink = get_sample_permalink( $product->get_id(), $product->get_name(), '' );

				// Add permalink_template if needed.
				if ( $permalink_template_requested ) {
					$data['permalink_template'] = $sample_permalink[0];
				}

				// Add generated_slug if needed.
				if ( $generated_slug_requested ) {
					$data['generated_slug'] = $sample_permalink[1];
				}
			}
		}
	}

	return $data;
}