Skip to content

Commit

Permalink
Use medium size for products
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jan 14, 2024
1 parent 93f2d05 commit 582ca89
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 62 deletions.
16 changes: 15 additions & 1 deletion includes/API/Product_Variations_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $reque
*/
public function wcpos_register_wc_rest_api_hooks(): void {
add_filter( 'woocommerce_rest_prepare_product_variation_object', array( $this, 'wcpos_variation_response' ), 10, 3 );
add_filter( 'wp_get_attachment_image_src', array( $this, 'wcpos_product_image_src' ), 10, 4 );
add_action( 'woocommerce_rest_insert_product_variation_object', array( $this, 'wcpos_insert_product_variation_object' ), 10, 3 );
add_filter( 'woocommerce_rest_product_variation_object_query', array( $this, 'wcpos_product_variation_query' ), 10, 2 );
add_filter( 'posts_search', array( $this, 'wcpos_posts_search' ), 10, 2 );
Expand All @@ -173,6 +172,21 @@ public function wcpos_variation_response( WP_REST_Response $response, WC_Data $v
// Add the barcode to the product response
$data['barcode'] = $this->wcpos_get_barcode( $variation );

// Check if the response has an image
if ( isset( $data['images'] ) && ! empty( $data['images'] ) ) {
foreach ( $data['images'] as $key => $image ) {
// Replace the full size 'src' with the URL of the medium size image.
$image_id = $image['id'];
$medium_image_data = image_downsize( $image_id, 'medium' );

if ( $medium_image_data && isset( $medium_image_data[0] ) ) {
$data['images'][ $key ]['src'] = $medium_image_data[0];
} else {
$data['images'][ $key ]['src'] = $image['src'];
}
}
}

/*
* Backwards compatibility for WooCommerce < 8.3
*
Expand Down
16 changes: 15 additions & 1 deletion includes/API/Products_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $reque
*/
public function wcpos_register_wc_rest_api_hooks(): void {
add_filter( 'woocommerce_rest_prepare_product_object', array( $this, 'wcpos_product_response' ), 10, 3 );
add_filter( 'wp_get_attachment_image_src', array( $this, 'wcpos_product_image_src' ), 10, 4 );
add_action( 'woocommerce_rest_insert_product_object', array( $this, 'wcpos_insert_product_object' ), 10, 3 );
add_filter( 'woocommerce_rest_product_object_query', array( $this, 'wcpos_product_query' ), 10, 2 );
add_filter( 'posts_search', array( $this, 'wcpos_posts_search' ), 10, 2 );
Expand All @@ -165,6 +164,21 @@ public function wcpos_product_response( WP_REST_Response $response, WC_Product $
// Add the barcode to the product response
$data['barcode'] = $this->wcpos_get_barcode( $product );

// Check if the response has an image
if ( isset( $data['images'] ) && ! empty( $data['images'] ) ) {
foreach ( $data['images'] as $key => $image ) {
// Replace the full size 'src' with the URL of the medium size image.
$image_id = $image['id'];
$medium_image_data = image_downsize( $image_id, 'medium' );

if ( $medium_image_data && isset( $medium_image_data[0] ) ) {
$data['images'][ $key ]['src'] = $medium_image_data[0];
} else {
$data['images'][ $key ]['src'] = $image['src'];
}
}
}

/*
* If product is variable, add the max and min prices and add them to the meta data
* @TODO - only need to update if there is a change
Expand Down
60 changes: 0 additions & 60 deletions includes/API/Traits/Product_Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,6 @@
use WCPOS\WooCommercePOS\Logger;

trait Product_Helpers {
/**
* Filters the attachment image source result.
* The WC REST API returns 'full' images by default, but we want to return 'shop_thumbnail' images.
*
* @TODO - should we return a wc_placeholder_img_src if no image is available?
*
* @param array|false $image {
* Array of image data, or boolean false if no image is available.
*
* @var string $0 Image source URL.
* @var int $1 Image width in pixels.
* @var int $2 Image height in pixels.
* @var bool $3 Whether the image is a resized image.
* }
*
* @param int $attachment_id Image attachment ID.
* @param int[]|string $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
* @param bool $icon Whether the image should be treated as an icon.
*/
public function wcpos_product_image_src( $image, int $attachment_id, $size, bool $icon ) {
// Get the metadata for the attachment.
$metadata = wp_get_attachment_metadata( $attachment_id );

if ( isset( $metadata['sizes']['woocommerce_gallery_thumbnail'] ) ) {
// Use the 'woocommerce_gallery_thumbnail' size if it exists.
return image_downsize( $attachment_id, 'woocommerce_gallery_thumbnail' );
}
if ( isset( $metadata['sizes']['thumbnail'] ) ) {
// If 'woocommerce_gallery_thumbnail' doesn't exist, try the 'thumbnail' size.
return image_downsize( $attachment_id, 'thumbnail' );
}

// If neither 'woocommerce_gallery_thumbnail' nor 'thumbnail' sizes exist, return the original $image.
return $image;
}

/**
* Get custom barcode postmeta.
*
Expand Down Expand Up @@ -120,27 +83,4 @@ public function wcpos_allow_decimal_quantities() {
// make sure it's true, just in case there's a corrupt setting
return true === $allow_decimal_quantities;
}

/**
* OLD CODE
* Returns thumbnail if it exists, if not, returns the WC placeholder image.
*
* @param int $id
*
* @return string
*/
private function get_thumbnail( int $id ): string {
$image = false;
$thumb_id = get_post_thumbnail_id( $id );

if ( $thumb_id ) {
$image = wp_get_attachment_image_src( $thumb_id, 'shop_thumbnail' );
}

if ( \is_array( $image ) ) {
return $image[0];
}

return wc_placeholder_img_src();
}
}

0 comments on commit 582ca89

Please sign in to comment.