Copy and Modified WooCommerce function is_coupon_valid [closed]

Without seeing your whole it could be tricky to answer. So assuming that your code is written the way I think it is, then the error is most likely caused by the $this keyword in your custom_is_coupon_valid function. It seems like you’re attempting to call methods that exist in WooCommerce’s WC_Coupon class. The $this keyword is used inside object context only and it refers to the calling object (the instance of the class where the method is being called). When you copy methods to a global function in functions.php, $this becomes meaningless because there’s no object context.

Here’s a revised custom_is_coupon_valid function. Instead of using $this, we’re directly referring to the $coupon object which should be an instance of the WC_Coupon class:

function custom_is_coupon_valid( $coupon ) {
    try {
        $coupon->validate_coupon_exists();
        $coupon->validate_coupon_usage_limit();
        $coupon->validate_coupon_user_usage_limit();
        $coupon->validate_coupon_expiry_date();
        $coupon->validate_coupon_minimum_amount();
        $coupon->validate_coupon_maximum_amount();
        $coupon->validate_coupon_product_ids();
        $coupon->validate_coupon_product_categories();
        $coupon->validate_coupon_excluded_items();
        $coupon->validate_coupon_eligible_items();

        if ( ! apply_filters( 'woocommerce_coupon_is_valid', true, $coupon, null ) ) {
            throw new Exception( __( 'Coupon is not valid.', 'woocommerce' ), 100 );
        }
    } catch ( Exception $e ) {
        $message = "Invalid coupon";
        return new WP_Error(
            'invalid_coupon',
            $message,
            array(
                'status' => 400,
            )
        );
    }
    return true;
}

In your custom_show_coupons_shortcode function, you’re calling $discounts->custom_is_coupon_valid( $coupon ); but you’ve defined custom_is_coupon_valid as a global function, not a method within a class. So, you should call it like this: custom_is_coupon_valid( $coupon );.

Also, in this function, you’re initializing $coupon as new \WC_Coupon( $coupon_code ); but $coupon_code is not defined anywhere in this context. Based on your code, I assume you’re trying to use the coupon ID, so it should be new \WC_Coupon( $coupon_id );.

After making these changes, your code should function as expected. And make sure you’re correctly initializing and passing the $coupon object.

deneme bonusu veren sitelerbahis casinomakrobetceltabettipobet365pinbahispolobetgizabetaresbetlordbahisladesbetbetofbettrbettumbet