Skip to content

Commit

Permalink
fix product images and coupon form
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jun 21, 2023
1 parent ad314ac commit 21ebc62
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 9 deletions.
41 changes: 40 additions & 1 deletion includes/API/Product_Variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
38 changes: 38 additions & 0 deletions includes/API/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
17 changes: 14 additions & 3 deletions includes/Templates/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace WCPOS\WooCommercePOS\Templates;

use Exception;
use function define;
use function defined;

class Payment {
/**
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion templates/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@

<div class="coupons">
<form method="post" action="">
<?php wp_nonce_field( 'pos_coupon_action', 'pos_coupon_nonce' ); ?>
<input type="hidden" name="pos_coupon_nonce" value="<?php echo $coupon_nonce; ?>" />
<input type="text" name="pos_coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" id="pos_coupon_code" value="" />
<button type="submit" class="button" name="pos_apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>">
<?php esc_html_e( 'Apply coupon', 'woocommerce' ); ?>
Expand Down
4 changes: 2 additions & 2 deletions woocommerce-pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WooCommerce POS
* Plugin URI: https://wordpress.org/plugins/woocommerce-pos/
* Description: A simple front-end for taking WooCommerce orders at the Point of Sale. Requires <a href="http://wordpress.org/plugins/woocommerce/">WooCommerce</a>.
* Version: 1.2.2
* Version: 1.2.3
* Author: kilbot
* Author URI: http://wcpos.com
* Text Domain: woocommerce-pos
Expand All @@ -24,7 +24,7 @@
use function define;

// Define plugin constants.
const VERSION = '1.2.2';
const VERSION = '1.2.3';
const PLUGIN_NAME = 'woocommerce-pos';
const SHORT_NAME = 'wcpos';
define( __NAMESPACE__ . '\PLUGIN_FILE', plugin_basename( __FILE__ ) ); // 'woocommerce-pos/woocommerce-pos.php'
Expand Down

0 comments on commit 21ebc62

Please sign in to comment.