Skip to content

Commit 548b00c

Browse files
killua99mikejolley
authored andcommitted
Improve the way we do queries (woocommerce#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
1 parent e8710e4 commit 548b00c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

includes/wc-coupon-functions.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
*/
1818
function wc_get_coupon_types() {
1919
return (array) apply_filters(
20-
'woocommerce_coupon_discount_types', array(
20+
'woocommerce_coupon_discount_types',
21+
array(
2122
'percent' => __( 'Percentage discount', 'woocommerce' ),
2223
'fixed_cart' => __( 'Fixed cart discount', 'woocommerce' ),
2324
'fixed_product' => __( 'Fixed product discount', 'woocommerce' ),
@@ -77,7 +78,7 @@ function wc_coupons_enabled() {
7778
*/
7879
function wc_get_coupon_code_by_id( $id ) {
7980
$data_store = WC_Data_Store::load( 'coupon' );
80-
return (string) $data_store->get_code_by_id( $id );
81+
return empty( $id ) ? '' : (string) $data_store->get_code_by_id( $id );
8182
}
8283

8384
/**
@@ -89,6 +90,11 @@ function wc_get_coupon_code_by_id( $id ) {
8990
* @return int
9091
*/
9192
function wc_get_coupon_id_by_code( $code, $exclude = 0 ) {
93+
94+
if ( empty( $code ) ) {
95+
return 0;
96+
}
97+
9298
$data_store = WC_Data_Store::load( 'coupon' );
9399
$ids = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, 'coupons' );
94100

tests/unit-tests/coupon/functions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Class Functions.
54
* @package WooCommerce\Tests\Coupon
@@ -76,6 +75,9 @@ public function test_wc_get_coupon_id_by_code() {
7675
// Delete coupon.
7776
WC_Helper_Coupon::delete_coupon( $coupon->get_id() );
7877

78+
$this->assertEquals( 0, wc_get_coupon_id_by_code( '' ) );
79+
7980
$this->assertEmpty( wc_get_coupon_id_by_code( 0 ) );
8081
}
82+
8183
}

0 commit comments

Comments
 (0)