Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit c0c3819

Browse files
nerradmikejolley
authored andcommitted
add additional data to new products store endpoint (#1116)
1 parent 104fa25 commit c0c3819

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

src/RestApi/StoreApi/Schemas/ProductSchema.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,48 @@ protected function get_properties() {
119119
),
120120
),
121121
),
122+
'has_options' => array(
123+
'description' => __( 'Does the product have options?', 'woo-gutenberg-products-block' ),
124+
'type' => 'boolean',
125+
'context' => array( 'view', 'edit' ),
126+
'readonly' => true,
127+
),
128+
'is_purchasable' => array(
129+
'description' => __( 'Is the product purchasable?', 'woo-gutenberg-products-block' ),
130+
'type' => 'boolean',
131+
'context' => array( 'view', 'edit' ),
132+
'readonly' => true,
133+
),
134+
'is_in_stock' => array(
135+
'description' => __( 'Is the product in stock?', 'woo-gutenberg-products-block' ),
136+
'type' => 'boolean',
137+
'context' => array( 'view', 'edit' ),
138+
'readonly' => true,
139+
),
140+
'add_to_cart' => array(
141+
'description' => __( 'Add to cart button parameters.', 'woo-gutenberg-products-block' ),
142+
'type' => 'object',
143+
'context' => array( 'view', 'edit' ),
144+
'readonly' => true,
145+
'items' => array(
146+
'type' => 'object',
147+
'properties' => array(
148+
'text' => array(
149+
'description' => __( 'Button text.', 'woo-gutenberg-products-block' ),
150+
'type' => 'string',
151+
'context' => array( 'view', 'edit' ),
152+
'readonly' => true,
153+
),
154+
'description' => array(
155+
'description' => __( 'Button description.', 'woo-gutenberg-products-block' ),
156+
'type' => 'string',
157+
'context' => array( 'view', 'edit' ),
158+
'readonly' => true,
159+
),
160+
),
161+
),
162+
),
163+
122164
];
123165
}
124166

@@ -141,6 +183,13 @@ public function get_item_response( $product ) {
141183
'average_rating' => $product->get_average_rating(),
142184
'review_count' => $product->get_review_count(),
143185
'images' => ( new ProductImages() )->images_to_array( $product ),
186+
'has_options' => $product->has_options(),
187+
'is_purchasable' => $product->is_purchasable(),
188+
'is_in_stock' => $product->is_in_stock(),
189+
'add_to_cart' => [
190+
'text' => $product->add_to_cart_text(),
191+
'description' => $product->add_to_cart_description(),
192+
],
144193
];
145194
}
146195
}

tests/php/RestApi/StoreApi/Controllers/Products.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ public function setUp() {
2424
wp_set_current_user( 0 );
2525

2626
$this->products = [];
27-
$this->products[0] = ProductHelper::create_simple_product( false );
28-
$this->products[0]->save();
29-
$this->products[1] = ProductHelper::create_simple_product( false );
30-
$this->products[1]->save();
27+
$this->products[0] = ProductHelper::create_simple_product( true );
28+
$this->products[1] = ProductHelper::create_simple_product( true );
3129
}
3230

3331
/**
@@ -55,6 +53,11 @@ public function test_get_item() {
5553
$this->assertEquals( $this->products[0]->get_price_html(), $data['price_html'] );
5654
$this->assertEquals( $this->products[0]->get_average_rating(), $data['average_rating'] );
5755
$this->assertEquals( $this->products[0]->get_review_count(), $data['review_count'] );
56+
$this->assertEquals( $this->products[0]->has_options(), $data['has_options'] );
57+
$this->assertEquals( $this->products[0]->is_purchasable(), $data['is_purchasable'] );
58+
$this->assertEquals( $this->products[0]->is_in_stock(), $data['is_in_stock'] );
59+
$this->assertEquals( $this->products[0]->add_to_cart_text(), $data['add_to_cart']['text'] );
60+
$this->assertEquals( $this->products[0]->add_to_cart_description(), $data['add_to_cart']['description'] );
5861
}
5962

6063
/**
@@ -77,6 +80,10 @@ public function test_get_items() {
7780
$this->assertArrayHasKey( 'average_rating', $data[0] );
7881
$this->assertArrayHasKey( 'review_count', $data[0] );
7982
$this->assertArrayHasKey( 'images', $data[0] );
83+
$this->assertArrayHasKey( 'has_options', $data[0] );
84+
$this->assertArrayHasKey( 'is_purchasable', $data[0] );
85+
$this->assertArrayHasKey( 'is_in_stock', $data[0] );
86+
$this->assertArrayHasKey( 'add_to_cart', $data[0] );
8087
}
8188

8289
/**
@@ -97,6 +104,10 @@ public function test_get_item_schema() {
97104
$this->assertArrayHasKey( 'average_rating', $schema['properties'] );
98105
$this->assertArrayHasKey( 'review_count', $schema['properties'] );
99106
$this->assertArrayHasKey( 'images', $schema['properties'] );
107+
$this->assertArrayHasKey( 'has_options', $schema['properties'] );
108+
$this->assertArrayHasKey( 'is_purchasable', $schema['properties'] );
109+
$this->assertArrayHasKey( 'is_in_stock', $schema['properties'] );
110+
$this->assertArrayHasKey( 'add_to_cart', $schema['properties'] );
100111
}
101112

102113
/**
@@ -117,6 +128,10 @@ public function test_prepare_item_for_response() {
117128
$this->assertArrayHasKey( 'average_rating', $response->get_data() );
118129
$this->assertArrayHasKey( 'review_count', $response->get_data() );
119130
$this->assertArrayHasKey( 'images', $response->get_data() );
131+
$this->assertArrayHasKey( 'has_options', $response->get_data() );
132+
$this->assertArrayHasKey( 'is_purchasable', $response->get_data() );
133+
$this->assertArrayHasKey( 'is_in_stock', $response->get_data() );
134+
$this->assertArrayHasKey( 'add_to_cart', $response->get_data() );
120135
}
121136

122137
/**

0 commit comments

Comments
 (0)