Improve the way we do queries (#22043)

* Improve the way we do queries

* PHP legacy compatible

* Update aproach to filter queries with empty values

* Fixing Unit Tests

* Moving Unit Test to its the correct test function

* Filter missing the new param, allow_empty, also simplifiying unit test.

* Helper function to create counpon does not support empty coupon codes

* Helper function does not need to allot empty search

* Wrong code standard

* Fixing Code Standard Unit Test
This commit is contained in:
Luigi 2019-01-23 14:46:32 +01:00 committed by Mike Jolley
parent e8710e4ad6
commit 548b00c660
2 changed files with 11 additions and 3 deletions

View File

@ -17,7 +17,8 @@ defined( 'ABSPATH' ) || exit;
*/ */
function wc_get_coupon_types() { function wc_get_coupon_types() {
return (array) apply_filters( return (array) apply_filters(
'woocommerce_coupon_discount_types', array( 'woocommerce_coupon_discount_types',
array(
'percent' => __( 'Percentage discount', 'woocommerce' ), 'percent' => __( 'Percentage discount', 'woocommerce' ),
'fixed_cart' => __( 'Fixed cart discount', 'woocommerce' ), 'fixed_cart' => __( 'Fixed cart discount', 'woocommerce' ),
'fixed_product' => __( 'Fixed product discount', 'woocommerce' ), 'fixed_product' => __( 'Fixed product discount', 'woocommerce' ),
@ -77,7 +78,7 @@ function wc_coupons_enabled() {
*/ */
function wc_get_coupon_code_by_id( $id ) { function wc_get_coupon_code_by_id( $id ) {
$data_store = WC_Data_Store::load( 'coupon' ); $data_store = WC_Data_Store::load( 'coupon' );
return (string) $data_store->get_code_by_id( $id ); return empty( $id ) ? '' : (string) $data_store->get_code_by_id( $id );
} }
/** /**
@ -89,6 +90,11 @@ function wc_get_coupon_code_by_id( $id ) {
* @return int * @return int
*/ */
function wc_get_coupon_id_by_code( $code, $exclude = 0 ) { function wc_get_coupon_id_by_code( $code, $exclude = 0 ) {
if ( empty( $code ) ) {
return 0;
}
$data_store = WC_Data_Store::load( 'coupon' ); $data_store = WC_Data_Store::load( 'coupon' );
$ids = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, 'coupons' ); $ids = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, 'coupons' );

View File

@ -1,5 +1,4 @@
<?php <?php
/** /**
* Class Functions. * Class Functions.
* @package WooCommerce\Tests\Coupon * @package WooCommerce\Tests\Coupon
@ -76,6 +75,9 @@ class WC_Tests_Functions extends WC_Unit_Test_Case {
// Delete coupon. // Delete coupon.
WC_Helper_Coupon::delete_coupon( $coupon->get_id() ); WC_Helper_Coupon::delete_coupon( $coupon->get_id() );
$this->assertEquals( 0, wc_get_coupon_id_by_code( '' ) );
$this->assertEmpty( wc_get_coupon_id_by_code( 0 ) ); $this->assertEmpty( wc_get_coupon_id_by_code( 0 ) );
} }
} }