Skip to content

Commit

Permalink
version 1.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jul 29, 2023
1 parent 9eff817 commit 5ec8a93
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 27 deletions.
34 changes: 30 additions & 4 deletions includes/API/Product_Variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Ramsey\Uuid\Uuid;
use WC_Data;
use WC_Product_Variation;
use WP_Error;
use WP_Query;
use WP_REST_Request;
Expand All @@ -26,7 +27,8 @@ public function __construct( WP_REST_Request $request ) {
$this->add_product_image_src_filter();

add_filter( 'woocommerce_rest_prepare_product_variation_object', array( $this, 'product_response' ), 10, 3 );
}
add_filter( 'woocommerce_rest_pre_insert_product_variation_object', array( $this, 'prevent_variation_description_update' ), 10, 3 );
}

/**
* Filter the product response.
Expand All @@ -46,9 +48,6 @@ public function product_response( WP_REST_Response $response, WC_Data $product,
// Add the barcode to the product response
$data['barcode'] = $this->get_barcode( $product );

// Truncate the product description and short_description
// $data['description'] = $this->truncate_text( $data['description'] );

/**
* Make sure we parse the meta data before returning the response
*/
Expand All @@ -61,6 +60,33 @@ public function product_response( WP_REST_Response $response, WC_Data $product,
return $response;
}

/**
* Filters the product variation object before it is inserted or updated via the REST API.
*
* This filter is used to prevent the description field
* from being updated when a product variation is updated via the REST API. It does this by
* resetting this field to its current value in the database before the
* product variation object is updated.
*
* @param WC_Product_Variation $variation The product variation object that is being inserted or updated.
* This object is mutable.
* @param WP_REST_Request $request The request object.
* @param bool $creating If is creating a new object.
*
* @return WC_Product_Variation The modified product variation object.
*/
public function prevent_variation_description_update( WC_Product_Variation $variation, WP_REST_Request $request, bool $creating ): WC_Product_Variation {
// If variation is being updated
if ( ! $creating ) {
$original_variation = wc_get_product( $variation->get_id() );

// Reset the description to the original value
$variation->set_description( $original_variation->get_description() );
}

return $variation;
}

/**
* Returns array of all product ids, name.
*
Expand Down
39 changes: 34 additions & 5 deletions includes/API/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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( 'woocommerce_rest_pre_insert_product_object', array( $this, 'prevent_description_update' ), 10, 3 );
}

/**
Expand Down Expand Up @@ -114,6 +115,38 @@ public function rest_request_before_callbacks( $response, $handler, $request ) {
return $response;
}


/**
* Filters the product object before it is inserted or updated via the REST API.
*
* This filter is used to prevent the description and short_description fields
* from being updated when a product is updated via the REST API. It does this by
* resetting these fields to their current values in the database before the
* product object is updated.
*
* Ask me why this is here 😓
*
* @param WC_Product $product The product object that is being inserted or updated.
* This object is mutable.
* @param WP_REST_Request $request The request object.
* @param bool $creating If is creating a new object.
*
* @return WC_Product The modified product object.
*/
public function prevent_description_update( WC_Product $product, WP_REST_Request $request, bool $creating ): WC_Product {
// If product is being updated
if ( ! $creating ) {
$original_product = wc_get_product( $product->get_id() );

// Reset the description and short description to the original values
$product->set_description( $original_product->get_description() );
$product->set_short_description( $original_product->get_short_description() );
}

return $product;
}


/**
* Fires after a single object is created or updated via the REST API.
*
Expand Down Expand Up @@ -148,10 +181,6 @@ public function product_response( WP_REST_Response $response, WC_Product $produc
// Add the barcode to the product response
$data['barcode'] = $this->get_barcode( $product );

// Truncate the product description and short_description
// $data['description'] = $this->truncate_text( $data['description'] );
// $data['short_description'] = $this->truncate_text( $data['short_description'] );

/**
* 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 Expand Up @@ -319,7 +348,7 @@ public function posts_search( string $search, WP_Query $wp_query ): string {
// Search in barcode field
$barcode_field = woocommerce_pos_get_settings( 'general', 'barcode_field' );
if ( $barcode_field !== '_sku' ) {
$search_array[] = $wpdb->prepare( "(wp_postmeta.meta_key = %s AND wp_postmeta.meta_value LIKE %s)", $barcode_field, $n . $like_term . $n );
$search_array[] = $wpdb->prepare( '(wp_postmeta.meta_key = %s AND wp_postmeta.meta_value LIKE %s)', $barcode_field, $n . $like_term . $n );
}
}

Expand Down
13 changes: 0 additions & 13 deletions includes/API/Traits/Product_Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,4 @@ private function get_barcode( $object ) {
return $object->get_meta( $barcode_field );
}

/**
* @param string $text
* @return string
*/
// private function truncate_text( string $text ): string {
// $max_length = 100;
// $result = wp_strip_all_tags( $text, true );
// if ( strlen( $result ) > $max_length ) {
// $result = substr( $result, 0, $max_length - 3 ) . '...';
// }
// return $result;
// }

}
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.3.4",
"version": "1.3.5",
"description": "A simple front-end for taking WooCommerce orders at the Point of Sale.",
"main": "index.js",
"workspaces": {
Expand Down
4 changes: 2 additions & 2 deletions 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.3
Stable tag: 1.3.4
Stable tag: 1.3.5
License: GPL-3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html

Expand Down Expand Up @@ -63,7 +63,7 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co

== Changelog ==

= 1.3.4 - 2023/07/29 =
= 1.3.4 and 1.3.5 - 2023/07/29 =
* Urgent Fix: product descriptions being truncated to 100 characters

= 1.3.3 - 2023/07/28 =
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.3.4
* Version: 1.3.5
* 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.3.4';
const VERSION = '1.3.5';
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 5ec8a93

Please sign in to comment.