diff --git a/includes/wc-coupon-functions.php b/includes/wc-coupon-functions.php index b2e9950b5cd..13fd7c0f9e3 100644 --- a/includes/wc-coupon-functions.php +++ b/includes/wc-coupon-functions.php @@ -17,7 +17,8 @@ defined( 'ABSPATH' ) || exit; */ function wc_get_coupon_types() { return (array) apply_filters( - 'woocommerce_coupon_discount_types', array( + 'woocommerce_coupon_discount_types', + array( 'percent' => __( 'Percentage discount', 'woocommerce' ), 'fixed_cart' => __( 'Fixed cart discount', 'woocommerce' ), 'fixed_product' => __( 'Fixed product discount', 'woocommerce' ), @@ -77,7 +78,7 @@ function wc_coupons_enabled() { */ function wc_get_coupon_code_by_id( $id ) { $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 */ function wc_get_coupon_id_by_code( $code, $exclude = 0 ) { + + if ( empty( $code ) ) { + return 0; + } + $data_store = WC_Data_Store::load( 'coupon' ); $ids = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, 'coupons' ); diff --git a/tests/unit-tests/coupon/functions.php b/tests/unit-tests/coupon/functions.php index a88c6a340db..69352f46d92 100644 --- a/tests/unit-tests/coupon/functions.php +++ b/tests/unit-tests/coupon/functions.php @@ -1,5 +1,4 @@ get_id() ); + $this->assertEquals( 0, wc_get_coupon_id_by_code( '' ) ); + $this->assertEmpty( wc_get_coupon_id_by_code( 0 ) ); } + }