• Resolved pushpendu221

    (@pushpendu221)


    Hi,

    I came across a bug while developing, on calling class WC_Discounts

    	$discounts = new WC_Discounts( WC()->cart );
    	$valid     = $discounts->is_coupon_valid( '4syse293' );

    It always generates the following fatal error messages for valid and invalid coupons.

    Fatal error: Uncaught Error: Call to a member function get_id() on string in /var/www/html/wpnew2018/app-woocommerce/wp-content/plugins/woocommerce/includes/class-wc-discounts.php:583 Stack trace: #0 /var/www/html/wpnew2018/app-woocommerce/wp-content/plugins/woocommerce/includes/class-wc-discounts.php(949): WC_Discounts->validate_coupon_exists(‘4syse293’) #1 /var/www/html/wpnew2018/app-woocommerce/wp-content/themes/storefront/inc/woo-API-functions.php(35): WC_Discounts->is_coupon_valid(‘4syse293’) #2 /var/www/html/wpnew2018/app-woocommerce/wp-content/themes/storefront/page.php(14): checkCouponValid(‘4syse293’, 40) #3 /var/www/html/wpnew2018/app-woocommerce/wp-includes/template-loader.php(106): include(‘/var/www/html/w…’) #4 /var/www/html/wpnew2018/app-woocommerce/wp-blog-header.php(19): require_once(‘/var/www/html/w…’) #5 /var/www/html/wpnew2018/app-woocommerce/index.php(17): require(‘/var/www/html/w…’) #6 {main} thrown in /var/www/html/wpnew2018/app-woocommerce/wp-content/plugins/woocommerce/includes/class-wc-discounts.php on line 583

    Please check.. it seems like an update issue.
    Current WP Version: 5.4.1
    Woocommerce Version: 4.1.1

    Thanks
    Pushpendu

    • This topic was modified 4 years, 9 months ago by pushpendu221.
    • This topic was modified 4 years, 9 months ago by pushpendu221.
    • This topic was modified 4 years, 9 months ago by pushpendu221.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Maybe you need to pass a coupon object to is_coupon_valid(), not a string, so:

    $coupon = new WC_Coupon( '4syse293' );
    $valid = $discounts->is_coupon_valid( $coupon );

    Sorry, not tested.

    Thanks @lorro!

    @pushpendu221 – We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

    – Joey

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Fatal Error on calling WC_Discounts class’ is closed to new replies.