From 960dcab60c5133ad4089b0c87a8231d0a4767d08 Mon Sep 17 00:00:00 2001 From: Paul Kilmurray Date: Fri, 23 Feb 2024 16:23:53 +0100 Subject: [PATCH] Fix _create_via warning --- includes/API.php | 7 +- .../API/Product_Variations_Controller.php | 17 ++-- includes/Admin.php | 2 +- includes/Admin/Orders/HPOS_List_Orders.php | 2 +- includes/Admin/Orders/List_Orders.php | 2 +- includes/Gateways.php | 8 +- includes/Init.php | 4 + includes/WC_API.php | 91 +++++++++++++++++++ readme.txt | 6 ++ tests/includes/Test_Products.php | 40 +++++++- woocommerce-pos.php | 2 +- 11 files changed, 161 insertions(+), 20 deletions(-) create mode 100644 includes/WC_API.php diff --git a/includes/API.php b/includes/API.php index b74f0d4..b822c49 100644 --- a/includes/API.php +++ b/includes/API.php @@ -1,11 +1,11 @@ * * @see http://wcpos.com + * @package WCPOS\WooCommercePOS */ namespace WCPOS\WooCommercePOS; @@ -17,6 +17,9 @@ use WP_REST_Response; use WP_REST_Server; +/** + * + */ class API { /** * WCPOS REST API namespaces and endpoints. diff --git a/includes/API/Product_Variations_Controller.php b/includes/API/Product_Variations_Controller.php index 7b5f3d7..45e385b 100644 --- a/includes/API/Product_Variations_Controller.php +++ b/includes/API/Product_Variations_Controller.php @@ -173,17 +173,12 @@ public function wcpos_variation_response( WP_REST_Response $response, WC_Data $v $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']; - } + if ( isset( $data['image'] ) && ! empty( $data['image'] ) && isset( $data['image']['id'] ) ) { + // Replace the full size 'src' with the URL of the medium size image. + $medium_image_data = image_downsize( $data['image']['id'], 'medium' ); + + if ( $medium_image_data && isset( $medium_image_data[0] ) ) { + $data['image']['src'] = $medium_image_data[0]; } } diff --git a/includes/Admin.php b/includes/Admin.php index 43330f6..a562c23 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -141,7 +141,7 @@ public function current_screen( $current_screen ): void { * */ public function handle_wc_hpos_orders_screen() { - if ( 'edit' === $_GET['action'] ) { + if ( isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) { new HPOS_Single_Order(); } else { new HPOS_List_Orders(); diff --git a/includes/Admin/Orders/HPOS_List_Orders.php b/includes/Admin/Orders/HPOS_List_Orders.php index 669488e..eb589f2 100644 --- a/includes/Admin/Orders/HPOS_List_Orders.php +++ b/includes/Admin/Orders/HPOS_List_Orders.php @@ -41,7 +41,7 @@ public function orders_custom_column_content( string $column_name, $order ): voi if ( $order instanceof WC_Abstract_Order ) { // Use the getter methods for order meta data $legacy = $order->get_meta( '_pos', true ); - $created_via = $order->get_meta( '_created_via', true ); + $created_via = $order->get_created_via(); // Check if the order was created via WooCommerce POS if ( 'woocommerce-pos' === $created_via || '1' === $legacy ) { diff --git a/includes/Admin/Orders/List_Orders.php b/includes/Admin/Orders/List_Orders.php index f89bb0f..72340bc 100644 --- a/includes/Admin/Orders/List_Orders.php +++ b/includes/Admin/Orders/List_Orders.php @@ -133,7 +133,7 @@ public function pos_orders_list_column_content( string $column_name, int $post_i if ( $order instanceof WC_Abstract_Order ) { // Use the getter methods for order meta data $legacy = $order->get_meta( '_pos', true ); - $created_via = $order->get_meta( '_created_via', true ); + $created_via = $order->get_created_via(); // Check if the order was created via WooCommerce POS if ( 'woocommerce-pos' === $created_via || '1' === $legacy ) { diff --git a/includes/Gateways.php b/includes/Gateways.php index a5cd966..567f981 100644 --- a/includes/Gateways.php +++ b/includes/Gateways.php @@ -1,16 +1,22 @@ * * @see https://wcpos.com + * @package WCPOS\WooCommercePOS */ namespace WCPOS\WooCommercePOS; +/** + * Gateways class. + */ class Gateways { + /** + * + */ public function __construct() { add_action( 'woocommerce_payment_gateways', array( $this, 'payment_gateways' ) ); add_filter( 'woocommerce_available_payment_gateways', array( $this, 'available_payment_gateways' ), 99 ); diff --git a/includes/Init.php b/includes/Init.php index 80144b6..b5ce915 100644 --- a/includes/Init.php +++ b/includes/Init.php @@ -39,6 +39,8 @@ public function __construct() { // Headers for API discoverability add_filter( 'rest_pre_serve_request', array( $this, 'rest_pre_serve_request' ), 5, 4 ); add_action( 'send_headers', array( $this, 'send_headers' ), 99, 1 ); + + // add_filter( 'woocommerce_rest_check_permissions', '__return_true' ); } /** @@ -110,6 +112,8 @@ private function init_integrations() { public function init_rest_api(): void { if ( woocommerce_pos_request() ) { new API(); + } else { + new WC_API(); } } diff --git a/includes/WC_API.php b/includes/WC_API.php new file mode 100644 index 0000000..5e0409d --- /dev/null +++ b/includes/WC_API.php @@ -0,0 +1,91 @@ + + * + * @see http://wcpos.com + * @package WCPOS\WooCommercePOS + */ + +namespace WCPOS\WooCommercePOS; + +/** + * + */ +class WC_API { + + /** + * + */ + public function __construct() { + $pos_only_products = woocommerce_pos_get_settings( 'general', 'pos_only_products' ); + + if ( $pos_only_products ) { + add_action( 'woocommerce_product_query', array( $this, 'hide_pos_only_products' ) ); + add_filter( 'woocommerce_variation_is_visible', array( $this, 'hide_pos_only_variations' ), 10, 4 ); + } + } + + /** + * Hide POS Only products from the shop and category pages. + * + * @TODO - this should be improved so that admin users can see the product, but get a message + * + * @param WP_Query $query Query instance. + * + * @return void + */ + public function hide_pos_only_products( $query ) { + $meta_query = $query->get( 'meta_query' ); + + // Define your default meta query. + $default_meta_query = array( + 'relation' => 'OR', + array( + 'key' => '_pos_visibility', + 'value' => 'pos_only', + 'compare' => '!=', + ), + array( + 'key' => '_pos_visibility', + 'compare' => 'NOT EXISTS', + ), + ); + + // Check if an existing meta query exists. + if ( is_array( $meta_query ) ) { + if ( ! isset( $meta_query ['relation'] ) ) { + $meta_query['relation'] = 'AND'; + } + $meta_query[] = $default_meta_query; + } else { + $meta_query = $default_meta_query; + } + + // Set the updated meta query back to the query. + $query->set( 'meta_query', $meta_query ); + } + + /** + * Remove POS Only variations from the storefront. + * + * @param bool $visible Whether the variation is visible. + * @param int $variation_id The variation ID. + * @param int $product_id The product ID. + * @param \WC_Product_Variation $variation The variation object. + */ + public function hide_pos_only_variations( $visible, $variation_id, $product_id, $variation ) { + if ( \is_shop() || \is_product_category() || \is_product() ) { + // Get the _pos_visibility meta value for the variation. + $pos_visibility = get_post_meta( $variation_id, '_pos_visibility', true ); + + // Check if _pos_visibility is 'pos_only' for this variation. + if ( $pos_visibility === 'pos_only' ) { + return false; + } + } + + return $visible; + } +} diff --git a/readme.txt b/readme.txt index 845368c..b24df8f 100644 --- a/readme.txt +++ b/readme.txt @@ -63,6 +63,12 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co == Changelog == += 1.4.11 - 2024/02/XX = +* Fix: regression in product variation images, use 'medium' sized product image instead of full size +* Fix: remove POS Only products from frontend WC REST API response +* Fix: generic get meta method should not be used for '_create_via' +* Fix: other minor PHP warnings + = 1.4.10 - 2024/01/23 = * Fix: compatibility issue with WooCommerce < 6.7.0 diff --git a/tests/includes/Test_Products.php b/tests/includes/Test_Products.php index 71f29c6..5d3acf9 100644 --- a/tests/includes/Test_Products.php +++ b/tests/includes/Test_Products.php @@ -3,7 +3,7 @@ namespace WCPOS\WooCommercePOS\Tests; use Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper; -use WC_Unit_Test_Case; +use WC_REST_Unit_Test_Case; use WP_Query; use WC_Query; use WCPOS\WooCommercePOS\Products; @@ -14,7 +14,7 @@ * * @coversNothing */ -class Test_Products extends WC_Unit_Test_Case { +class Test_Products extends WC_REST_Unit_Test_Case { public function setup(): void { parent::setup(); } @@ -118,4 +118,40 @@ function () { // Assert that the variation without '_pos_visibility' set is in the query $this->assertContains( $variation_ids[2], $queried_variation_ids ); } + + /** + * + */ + public function test_pos_only_products_via_store_api() { + add_filter( + 'woocommerce_pos_general_settings', + function () { + return array( + 'pos_only_products' => true, + ); + } + ); + new Products(); // reinstantiate the class to apply the filter + + // Create a visible product + $visible_product = ProductHelper::create_simple_product(); + + // Create a product with _pos_visibility set to 'pos_only' + $hidden_product = ProductHelper::create_simple_product(); + update_post_meta( $hidden_product->get_id(), '_pos_visibility', 'pos_only' ); + + // Verify that the meta value is set correctly + $pos_visibility = get_post_meta( $hidden_product->get_id(), '_pos_visibility', true ); + $this->assertEquals( 'pos_only', $pos_visibility, 'Meta value for _pos_visibility not set correctly' ); + + // Make WC REST request + add_filter( 'woocommerce_rest_check_permissions', '__return_true' ); + $request = new \WP_REST_Request( 'GET', '/wc/v3/products' ); + $response = $this->server->dispatch( $request ); + + $data = $response->get_data(); + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( 1, \count( $data ) ); + $this->assertEquals( $visible_product->get_id(), $data[0]['id'] ); + } } diff --git a/woocommerce-pos.php b/woocommerce-pos.php index 7f916c3..69023b4 100644 --- a/woocommerce-pos.php +++ b/woocommerce-pos.php @@ -16,7 +16,7 @@ * @author Paul Kilmurray * * @see http://wcpos.com - * @package WooCommercePOS + * @package WCPOS\WooCommercePOS */ namespace WCPOS\WooCommercePOS;