Simplify the logic that defines the sample product IDs
This commit is contained in:
parent
aa497cd8e2
commit
2b24c07a8e
|
@ -207,7 +207,7 @@ class CustomizeStore extends Task {
|
||||||
|
|
||||||
return new WP_Post(
|
return new WP_Post(
|
||||||
(object) array(
|
(object) array(
|
||||||
'ID' => time() * 1000 + $index,
|
'ID' => $index + 1,
|
||||||
'post_author' => 1,
|
'post_author' => 1,
|
||||||
'post_date' => '',
|
'post_date' => '',
|
||||||
'post_date_gmt' => '',
|
'post_date_gmt' => '',
|
||||||
|
@ -231,6 +231,7 @@ class CustomizeStore extends Task {
|
||||||
'post_mime_type' => '',
|
'post_mime_type' => '',
|
||||||
'comment_count' => 0,
|
'comment_count' => 0,
|
||||||
'filter' => 'raw',
|
'filter' => 'raw',
|
||||||
|
'is_sample_product' => true,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -241,13 +242,12 @@ class CustomizeStore extends Task {
|
||||||
* This method creates an array representing a sample product
|
* This method creates an array representing a sample product
|
||||||
* using data from the sample products array.
|
* using data from the sample products array.
|
||||||
*
|
*
|
||||||
* @param int $product_id The ID of the product to generate. The last digit
|
* @param int $product_id The ID of the product to generate. It's used to
|
||||||
* of the product ID is used to determine which sample product to use.
|
* determine which sample product to use.
|
||||||
* @return array An array representing a sample product.
|
* @return array An array representing a sample product.
|
||||||
*/
|
*/
|
||||||
public function generate_sample_product_object( $product_id ) {
|
public function generate_sample_product_object( $product_id ) {
|
||||||
$last_digit = substr( $product_id, -1 );
|
$sample_product_data = $this->sample_products[ $product_id - 1 ];
|
||||||
$sample_product_data = $this->sample_products[ $last_digit ];
|
|
||||||
$image_src = plugins_url( '/assets/images/pattern-placeholders/' . $sample_product_data['image'], dirname( __DIR__, 4 ) );
|
$image_src = plugins_url( '/assets/images/pattern-placeholders/' . $sample_product_data['image'], dirname( __DIR__, 4 ) );
|
||||||
$currency_decimals = wc_get_price_decimals();
|
$currency_decimals = wc_get_price_decimals();
|
||||||
$prices = ( new CurrencyFormatter() )->format(
|
$prices = ( new CurrencyFormatter() )->format(
|
||||||
|
@ -299,8 +299,9 @@ class CustomizeStore extends Task {
|
||||||
/**
|
/**
|
||||||
* Adds the rendered title to the product response.
|
* Adds the rendered title to the product response.
|
||||||
*
|
*
|
||||||
* This method sets the 'rendered' title of a product to be the same as its 'raw' title
|
* This method sets the 'rendered' title of a product to be the same as the
|
||||||
* in the REST API response.
|
* one from the `$post` object. This way we short-circuit the `get_the_title`
|
||||||
|
* function used by WP core that wouldn't return the correct title.
|
||||||
*
|
*
|
||||||
* @param WP_REST_Response $response The response object.
|
* @param WP_REST_Response $response The response object.
|
||||||
* @param WP_Post $post The original post object.
|
* @param WP_Post $post The original post object.
|
||||||
|
@ -308,12 +309,10 @@ class CustomizeStore extends Task {
|
||||||
* @return WP_REST_Response The modified response object.
|
* @return WP_REST_Response The modified response object.
|
||||||
*/
|
*/
|
||||||
public function add_product_rendered_title( $response, $post, $request ) {
|
public function add_product_rendered_title( $response, $post, $request ) {
|
||||||
if ( ! empty( $response->data['title']['rendered'] ) || 'product' !== $post->post_type ) {
|
if ( $post->is_sample_product ) {
|
||||||
return $response;
|
$response->data['title']['rendered'] = $post->post_title;
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->data['title']['rendered'] = $response->data['title']['raw'];
|
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -345,9 +344,7 @@ class CustomizeStore extends Task {
|
||||||
* Handles requests for nonexistent products by generating sample product data.
|
* Handles requests for nonexistent products by generating sample product data.
|
||||||
*
|
*
|
||||||
* This method intercepts REST API requests for product details. If the requested
|
* This method intercepts REST API requests for product details. If the requested
|
||||||
* product doesn't exist and the product ID suggests it's a sample product
|
* product doesn't exist, it returns a sample product object.
|
||||||
* (ID representing a timestamp within the last 24 hours), it returns a
|
|
||||||
* sample product object.
|
|
||||||
*
|
*
|
||||||
* @param mixed $result The current result, usually null for not found.
|
* @param mixed $result The current result, usually null for not found.
|
||||||
* @param WP_REST_Server $server The REST server instance.
|
* @param WP_REST_Server $server The REST server instance.
|
||||||
|
@ -360,9 +357,7 @@ class CustomizeStore extends Task {
|
||||||
$product_id = (int) str_replace( '/wc/store/v1/products/', '', $request->get_route() );
|
$product_id = (int) str_replace( '/wc/store/v1/products/', '', $request->get_route() );
|
||||||
$product = wc_get_product( $product_id );
|
$product = wc_get_product( $product_id );
|
||||||
|
|
||||||
// Sample products use `time()` as the ID. This way we can detect that
|
if ( ! $product && $product_id <= count( $this->sample_products ) ) {
|
||||||
// it was a sample product returned 24 hours or less ago.
|
|
||||||
if ( ! $product && $product_id + 86400000 > time() * 1000 ) {
|
|
||||||
$sample_product = $this->generate_sample_product_object( $product_id );
|
$sample_product = $this->generate_sample_product_object( $product_id );
|
||||||
|
|
||||||
return rest_ensure_response( $sample_product );
|
return rest_ensure_response( $sample_product );
|
||||||
|
|
Loading…
Reference in New Issue