From 582ca8965241e14813500f02f3a484c7cddef1c7 Mon Sep 17 00:00:00 2001 From: Paul Kilmurray Date: Sun, 14 Jan 2024 17:32:46 +0100 Subject: [PATCH] Use medium size for products --- .../API/Product_Variations_Controller.php | 16 ++++- includes/API/Products_Controller.php | 16 ++++- includes/API/Traits/Product_Helpers.php | 60 ------------------- 3 files changed, 30 insertions(+), 62 deletions(-) diff --git a/includes/API/Product_Variations_Controller.php b/includes/API/Product_Variations_Controller.php index b1f714a6..7b5f3d72 100644 --- a/includes/API/Product_Variations_Controller.php +++ b/includes/API/Product_Variations_Controller.php @@ -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 ); @@ -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 * diff --git a/includes/API/Products_Controller.php b/includes/API/Products_Controller.php index ca1a2963..a84e64c8 100644 --- a/includes/API/Products_Controller.php +++ b/includes/API/Products_Controller.php @@ -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 ); @@ -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 diff --git a/includes/API/Traits/Product_Helpers.php b/includes/API/Traits/Product_Helpers.php index 146a242b..ebc7d954 100644 --- a/includes/API/Traits/Product_Helpers.php +++ b/includes/API/Traits/Product_Helpers.php @@ -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. * @@ -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(); - } }