Automattic\WooCommerce\StoreApi\Schemas\V1

ProductSchema::get_item_responsepublicWC 1.0

Convert a WooCommerce product into an object suitable for the response.

Method of the class: ProductSchema{}

No Hooks.

Returns

Array.

Usage

$ProductSchema = new ProductSchema();
$ProductSchema->get_item_response( $product );
$product(WC_Product) (required)
Product instance.

ProductSchema::get_item_response() code WC 10.7.0

public function get_item_response( $product ) {
	$availability      = ProductAvailabilityUtils::get_product_availability( $product );
	$password_required = post_password_required( $product->get_id() );
	$short_description = $password_required ? '' : $this->prepare_html_response( wc_format_content( wp_kses_post( $product->get_short_description() ) ) );
	$description       = $password_required ? '' : $this->prepare_html_response( wc_format_content( wp_kses_post( $product->get_description() ) ) );

	return [
		'id'                    => $product->get_id(),
		'name'                  => $this->prepare_html_response( $product->get_title() ),
		'slug'                  => $product->get_slug(),
		'parent'                => $product->get_parent_id(),
		'type'                  => $product->get_type(),
		'variation'             => $this->prepare_html_response( $product->is_type( ProductType::VARIATION ) ? wc_get_formatted_variation( $product, true, true, false ) : '' ),
		'permalink'             => $product->get_permalink(),
		'sku'                   => $this->prepare_html_response( $product->get_sku() ),
		'short_description'     => $short_description,
		'description'           => $description,
		'on_sale'               => $product->is_on_sale(),
		'prices'                => (object) $this->prepare_product_price_response( $product ),
		'price_html'            => $this->prepare_html_response( $product->get_price_html() ),
		'average_rating'        => (string) $product->get_average_rating(),
		'review_count'          => $product->get_review_count(),
		'images'                => $this->get_images( $product ),
		'categories'            => $this->get_term_list( $product, 'product_cat' ),
		'tags'                  => $this->get_term_list( $product, 'product_tag' ),
		'brands'                => $this->get_term_list( $product, 'product_brand' ),
		'attributes'            => $this->get_attributes( $product ),
		'variations'            => $this->get_variations( $product ),
		'grouped_products'      => $this->get_grouped_products( $product ),
		'has_options'           => $product->has_options(),
		'is_purchasable'        => $product->is_purchasable(),
		'is_in_stock'           => $product->is_in_stock(),
		'is_on_backorder'       => ProductStockStatus::ON_BACKORDER === $product->get_stock_status(),
		'low_stock_remaining'   => $this->get_low_stock_remaining( $product ),
		'stock_availability'    => (object) array(
			'text'  => $availability['availability'] ?? '',
			'class' => $availability['class'] ?? '',
		),
		'sold_individually'     => $product->is_sold_individually(),
		'weight'                => $product->get_weight(),
		'dimensions'            => (object) [
			'length' => $product->get_length(),
			'width'  => $product->get_width(),
			'height' => $product->get_height(),
		],
		'formatted_weight'      => wc_format_weight( (float) $product->get_weight() ),
		'formatted_dimensions'  => html_entity_decode( wc_format_dimensions( (array) $product->get_dimensions( false ) ), ENT_QUOTES, get_bloginfo( 'charset' ) ),
		'add_to_cart'           => (object) array_merge(
			[
				'text'        => $this->prepare_html_response( $product->add_to_cart_text() ),
				'description' => $this->prepare_html_response( $product->add_to_cart_description() ),
				'url'         => $this->prepare_html_response( $product->add_to_cart_url() ),
				'single_text' => $this->prepare_html_response( $product->single_add_to_cart_text() ),
			],
			( new QuantityLimits() )->get_add_to_cart_limits( $product )
		),
		'is_password_protected' => '' !== $product->get_post_password(),
		self::EXTENDING_KEY     => $this->get_extended_data( self::IDENTIFIER, $product ),

	];
}