Skip to content

Commit

Permalink
add tests for pos_only products
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Nov 30, 2023
1 parent 458019f commit 1f0fd6b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 74 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"php": ">=7.4",
"ext-json": "*",
"ramsey/uuid": "^4.2",
"salesforce/handlebars-php": "3.0.1",
"vlucas/phpdotenv": "v5.5.0"
"salesforce/handlebars-php": "3.0.1"
},
"config": {
"platform": {
Expand Down
47 changes: 0 additions & 47 deletions includes/Templates/Receipt.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
namespace WCPOS\WooCommercePOS\Templates;

use Exception;
use Handlebars\Handlebars;
use Handlebars\Helpers;
use Handlebars\Loader\StringLoader;
use WCPOS\WooCommercePOS\Server;

class Receipt {
/**
Expand Down Expand Up @@ -60,16 +56,6 @@ public function get_template(): void {
wp_die( esc_html__( 'Sorry, this order is invalid.', 'woocommerce-pos' ) );
}

// if ( ! $order->is_paid() ) {
// wp_die( esc_html__( 'Sorry, this order has not been paid.', 'woocommerce-pos' ) );
// }

if ( isset( $_GET['template'] ) ) {
if ( 'legacy' === $_GET['template'] ) {
$this->legacy_receipt_template();
}
}

/**
* Put WC_Order into the global scope so that the template can access it.
*/
Expand Down Expand Up @@ -100,37 +86,4 @@ private function get_template_path( string $file_name ) {
*/
return apply_filters( 'woocommerce_pos_print_receipt_path', woocommerce_pos_locate_template( $file_name ) );
}


private function legacy_receipt_template(): void {
$server = new Server();
$order_json = $server->wp_rest_request( '/wc/v3/orders/' . $this->order_id );
$path = $this->get_template_path( 'legacy-receipt.php' );

ob_start();
include $path;
$template = ob_get_clean();

$engine = new Handlebars(array(
'loader' => new StringLoader(),
'helpers' => new Helpers(),
// 'enableDataVariables' => true,
));
$engine->addHelper('formatAddress', function ( $template, $context, $args, $source ) {
return 'formatAddress';
});
$engine->addHelper('formatDate', function ( $template, $context, $args, $source ) {
return 'formatDate';
});
$engine->addHelper('number', function ( $template, $context, $args, $source ) {
return 'number';
});
$engine->addHelper('money', function ( $template, $context, $args, $source ) {
return 'money';
});
$receipt = $engine->render( $template, $order_json );

echo $receipt;
exit;
}
}
14 changes: 8 additions & 6 deletions includes/Templates/Received.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @author Paul Kilmurray <[email protected]>
*
* @see http://wcpos.com
* @package WooCommercePOS\Templates
*/

namespace WCPOS\WooCommercePOS\Templates;
Expand All @@ -19,26 +20,27 @@ class Received {
public function __construct( int $order_id ) {
$this->order_id = $order_id;

add_filter('show_admin_bar', '__return_false');
add_filter( 'show_admin_bar', '__return_false' );
}


public function get_template(): void {
try {
// get order
$order = wc_get_order( $this->order_id );
$order = \wc_get_order( $this->order_id );

// Order or receipt url is invalid.
if ( ! $order ) {
wp_die( esc_html__( 'Sorry, this order is invalid.', 'woocommerce-pos' ) );
}

// if ( ! $order->is_paid() ) {
// wp_die( esc_html__( 'Sorry, this order has not been paid.', 'woocommerce-pos' ) );
// }
// if ( ! $order->is_paid() ) {
// wp_die( esc_html__( 'Sorry, this order has not been paid.', 'woocommerce-pos' ) );
// }

/**
* @TODO - this is a hack and needs to be fixed
* @NOTE - the received template will be removed once we move to session based checkout
*
* - hardcoding the rest endpoint is a receipe for disaster
*/
Expand All @@ -53,7 +55,7 @@ public function get_template(): void {
include woocommerce_pos_locate_template( 'received.php' );
exit;
} catch ( Exception $e ) {
wc_print_notice( $e->getMessage(), 'error' );
\wc_print_notice( $e->getMessage(), 'error' );
}
}
}
58 changes: 56 additions & 2 deletions tests/includes/Test_Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use WP_Query;
use WC_Query;
use WCPOS\WooCommercePOS\Products;
use WC_Product_Variation;

/**
* @internal
Expand All @@ -23,7 +24,7 @@ public function tearDown(): void {
}

/**
* @TODO - I have no idea why this test isn't working
*
*/
public function test_pos_only_products() {
add_filter(
Expand Down Expand Up @@ -56,12 +57,65 @@ function () {

$query = new WP_Query( $query_args );
WC()->query->product_query( $query );
$queried_ids = wp_list_pluck( $query->posts, 'ID' );
$queried_ids = wp_list_pluck( $query->get_posts(), 'ID' );

// Assert that the visible product is in the query
$this->assertContains( $visible_product->get_id(), $queried_ids );

// Assert that the hidden product is not in the query
$this->assertNotContains( $hidden_product->get_id(), $queried_ids );
}

/**
*
*/
public function test_pos_only_variations() {
add_filter(
'woocommerce_pos_general_settings',
function () {
return array(
'pos_only_products' => true,
);
}
);
new Products(); // reinstantiate the class to apply the filter

// create variations
$product = ProductHelper::create_variation_product();
$variation_3 = new WC_Product_Variation();
$variation_3->set_props(
array(
'parent_id' => $product->get_id(),
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
'regular_price' => 10,
)
);
$variation_3->set_attributes( array( 'pa_size' => 'medium' ) );
$variation_3->save();

$variation_ids = $product->get_children();
update_post_meta( $variation_ids[0], '_pos_visibility', 'pos_only' );
update_post_meta( $variation_ids[1], '_pos_visibility', 'online_only' );

// Mimic the main WooCommerce query for product variations
$query_args = array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1, // Get all variations for testing
'post_parent' => $product->get_id(), // Ensure variations of the specific product are fetched
);

$query = new WP_Query( $query_args );
WC()->query->product_query( $query );
$queried_variation_ids = wp_list_pluck( $query->get_posts(), 'ID' );

// Assert that the variation with '_pos_visibility' set to 'pos_only' is NOT in the query
$this->assertNotContains( $variation_ids[0], $queried_variation_ids );

// Assert that the variation with '_pos_visibility' set to 'online_only' IS in the query
$this->assertContains( $variation_ids[1], $queried_variation_ids );

// Assert that the variation without '_pos_visibility' set is in the query
$this->assertContains( $variation_ids[2], $queried_variation_ids );
}
}
61 changes: 44 additions & 17 deletions woocommerce-pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
* @author Paul Kilmurray <[email protected]>
*
* @see http://wcpos.com
* @package WooCommercePOS
*/

namespace WCPOS\WooCommercePOS;

use function define;
use Dotenv\Dotenv;

// Define plugin constants.
const VERSION = '1.4.0-beta.3';
const PLUGIN_NAME = 'woocommerce-pos';
Expand All @@ -31,36 +29,65 @@
\define( __NAMESPACE__ . '\PLUGIN_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
\define( __NAMESPACE__ . '\PLUGIN_URL', trailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );

// minimum requirements
// minimum requirements.
const WC_MIN_VERSION = '5.3';
const PHP_MIN_VERSION = '7.4';
const MIN_PRO_VERSION = '1.2.0';

// Autoloader
// load .env flags (for development).
function load_env( $file ) {
if ( ! file_exists( $file ) ) {
return;
}

$lines = file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
foreach ( $lines as $line ) {
if ( strpos( trim( $line ), '#' ) === 0 ) {
continue;
}

list($name, $value) = explode( '=', $line, 2 );
$name = trim( $name );
$value = trim( $value );

if ( ! array_key_exists( $name, $_SERVER ) && ! array_key_exists( $name, $_ENV ) ) {
putenv( sprintf( '%s=%s', $name, $value ) );
$_ENV[ $name ] = $value;
}
}
}

// Autoloader.
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';

// Environment variables.
Dotenv::createImmutable( __DIR__ )->safeLoad();
load_env( __DIR__ . '/.env' );

// Activate plugin
// Activate plugin.
new Activator();

// Deactivate plugin
// Deactivate plugin.
new Deactivator();
} else {
add_action( 'admin_notices', function(): void {
?>
add_action(
'admin_notices',
function (): void {
?>
<div class="notice notice-error">
<p><?php esc_html_e( 'The WooCommerce POS plugin failed to load correctly.', 'woocommerce-pos' ); ?></p>
</div>
<?php
} );
<?php
}
);
}

// Declare HPOS compatible
add_action( 'before_woocommerce_init', function(): void {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
// Declare HPOS compatible.
add_action(
'before_woocommerce_init',
function (): void {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
} );
);

0 comments on commit 1f0fd6b

Please sign in to comment.