From 21ebc626f0af45f4a1c9e2518bd1bcd5b1f4d0dd Mon Sep 17 00:00:00 2001 From: Paul Kilmurray Date: Wed, 21 Jun 2023 23:44:57 +0100 Subject: [PATCH] fix product images and coupon form --- includes/API/Product_Variations.php | 41 ++++++++++++++++++++++++++++- includes/API/Products.php | 38 ++++++++++++++++++++++++++ includes/Templates/Payment.php | 17 +++++++++--- package.json | 2 +- readme.txt | 6 ++++- templates/payment.php | 2 +- woocommerce-pos.php | 4 +-- 7 files changed, 101 insertions(+), 9 deletions(-) diff --git a/includes/API/Product_Variations.php b/includes/API/Product_Variations.php index 4790c6f..20bf541 100644 --- a/includes/API/Product_Variations.php +++ b/includes/API/Product_Variations.php @@ -9,6 +9,9 @@ use WP_REST_Response; use WC_Product_Query; use WCPOS\WooCommercePOS\Logger; +use function image_downsize; +use function is_array; +use function wp_get_attachment_metadata; class Product_Variations { private $request; @@ -22,6 +25,7 @@ public function __construct( WP_REST_Request $request ) { $this->request = $request; add_filter( 'woocommerce_rest_prepare_product_variation_object', array( $this, 'product_response' ), 10, 3 ); + add_filter( 'wp_get_attachment_image_src', array( $this, 'product_image_src' ), 10, 4 ); } /** @@ -120,13 +124,48 @@ private function get_thumbnail( int $id ): string { $image = wp_get_attachment_image_src( $thumb_id, 'shop_thumbnail' ); } - if ( \is_array( $image ) ) { + if ( is_array( $image ) ) { return $image[0]; } return wc_placeholder_img_src(); } + /** + * Filters the attachment image source result. + * The WC REST API returns 'full' images by default, but we want to return 'shop_thumbnail' images. + * + * @param array|false $image { + * Array of image data, or boolean false if no image is available. + * + * @type string $0 Image source URL. + * @type int $1 Image width in pixels. + * @type int $2 Image height in pixels. + * @type bool $3 Whether the image is a resized image. + * } + * @param int $attachment_id Image attachment ID. + * @param string|int[] $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 product_image_src( $image, int $attachment_id, $size, bool $icon ) { + // Get the metadata for the attachment. + $metadata = wp_get_attachment_metadata( $attachment_id ); + + // Use the 'woocommerce_gallery_thumbnail' size if it exists. + if ( isset( $metadata['sizes']['woocommerce_gallery_thumbnail'] ) ) { + return image_downsize( $attachment_id, 'woocommerce_gallery_thumbnail' ); + } + // If 'woocommerce_gallery_thumbnail' doesn't exist, try the 'thumbnail' size. + else if ( isset( $metadata['sizes']['thumbnail'] ) ) { + return image_downsize( $attachment_id, 'thumbnail' ); + } + // If neither 'woocommerce_gallery_thumbnail' nor 'thumbnail' sizes exist, return the original $image. + else { + return $image; + } + } + /** * @param string $variation_id * diff --git a/includes/API/Products.php b/includes/API/Products.php index 5717d03..f64c36e 100644 --- a/includes/API/Products.php +++ b/includes/API/Products.php @@ -9,7 +9,9 @@ use WP_REST_Request; use WP_REST_Response; use WC_Product_Query; +use function image_downsize; use function is_array; +use function wp_get_attachment_metadata; /** * @property string $search_term @@ -32,6 +34,7 @@ public function __construct( WP_REST_Request $request ) { add_filter( 'posts_clauses', array( $this, 'posts_clauses' ), 10, 2 ); add_filter( 'woocommerce_rest_product_schema', array( $this, 'add_barcode_to_product_schema' ) ); add_action( 'woocommerce_rest_insert_product_object', array( $this, 'insert_product_object' ), 10, 3 ); + add_filter( 'wp_get_attachment_image_src', array( $this, 'product_image_src' ), 10, 4 ); } /** @@ -403,6 +406,41 @@ private function get_thumbnail( int $id ): string { return wc_placeholder_img_src(); } + /** + * Filters the attachment image source result. + * The WC REST API returns 'full' images by default, but we want to return 'shop_thumbnail' images. + * + * @param array|false $image { + * Array of image data, or boolean false if no image is available. + * + * @type string $0 Image source URL. + * @type int $1 Image width in pixels. + * @type int $2 Image height in pixels. + * @type bool $3 Whether the image is a resized image. + * } + * @param int $attachment_id Image attachment ID. + * @param string|int[] $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 product_image_src( $image, int $attachment_id, $size, bool $icon ) { + // Get the metadata for the attachment. + $metadata = wp_get_attachment_metadata( $attachment_id ); + + // Use the 'woocommerce_gallery_thumbnail' size if it exists. + if ( isset( $metadata['sizes']['woocommerce_gallery_thumbnail'] ) ) { + return image_downsize( $attachment_id, 'woocommerce_gallery_thumbnail' ); + } + // If 'woocommerce_gallery_thumbnail' doesn't exist, try the 'thumbnail' size. + else if ( isset( $metadata['sizes']['thumbnail'] ) ) { + return image_downsize( $attachment_id, 'thumbnail' ); + } + // If neither 'woocommerce_gallery_thumbnail' nor 'thumbnail' sizes exist, return the original $image. + else { + return $image; + } + } + /** * @param string $product_id * diff --git a/includes/Templates/Payment.php b/includes/Templates/Payment.php index d6e4b18..0f65faf 100644 --- a/includes/Templates/Payment.php +++ b/includes/Templates/Payment.php @@ -8,6 +8,8 @@ namespace WCPOS\WooCommercePOS\Templates; use Exception; +use function define; +use function defined; class Payment { /** @@ -111,8 +113,8 @@ public function remove_scripts(): void { public function get_template(): void { - if ( ! \defined( 'WOOCOMMERCE_CHECKOUT' ) ) { - \define( 'WOOCOMMERCE_CHECKOUT', true ); + if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { + define( 'WOOCOMMERCE_CHECKOUT', true ); } // if ( ! $this->gateway_id ) { @@ -139,7 +141,16 @@ public function get_template(): void { $cashier = $order->get_meta( '_pos_user', true ); $cashier = get_user_by( 'id', $cashier ); - // set customer + // create nonce for cashier to apply coupons + $coupon_nonce = wp_create_nonce( 'pos_coupon_action' ); + + /** + * The wp_set_current_user() function changes the global user object but it does not authenticate the user + * for the current session. This means that it will not affect nonce creation or validation because WordPress + * nonces are tied to the user's session. + * + * @TODO - is this the best way to do this? + */ wp_set_current_user( $order->get_customer_id() ); // create nonce for customer diff --git a/package.json b/package.json index ccc9bc8..987dae9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wcpos/woocommerce-pos", - "version": "1.2.2", + "version": "1.2.3", "description": "A simple front-end for taking WooCommerce orders at the Point of Sale.", "main": "index.js", "workspaces": { diff --git a/readme.txt b/readme.txt index 595b01a..f63be18 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: kilbot Tags: cart, e-commerce, ecommerce, inventory, point-of-sale, pos, sales, sell, shop, shopify, store, vend, woocommerce, wordpress-ecommerce Requires at least: 5.6 & WooCommerce 5.3 Tested up to: 6.2 -Stable tag: 1.2.2 +Stable tag: 1.2.3 License: GPL-3.0 License URI: http://www.gnu.org/licenses/gpl-3.0.html @@ -63,6 +63,10 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co == Changelog == += 1.2.3 - 2023/06/21 = +* Fix: coupon form on POS payment modal +* Fix: use woocommerce_gallery_thumbnail instead of full sized images for products and variations + = 1.2.2 - 2023/06/21 = * Add: basic support for coupons until a more complete solution is implemented * Fix: customer select in settings diff --git a/templates/payment.php b/templates/payment.php index 98be894..c995f86 100644 --- a/templates/payment.php +++ b/templates/payment.php @@ -276,7 +276,7 @@
- +