• Resolved sadiqodunsi

    (@sadiqodunsi)


    Hello,

    I am having a problem with the code to hide a payment gateway if the wallet balance is more than subtotal. The code works fine on the checkout page but it makes the menu page disappear on the backend. Please review this code for me and kindly let me know if there is something I am doing wrong.

    /**
     * Check if a specific product category is in the cart
     */
    function payments_category_is_in_the_cart() {
    	// Add your special category slugs here
    	$categories = array( 'payments' );
    
    	// Products currently in the cart
    	$cart_ids = array();
    
    	// Categories currently in the cart
    	$cart_categories = array();
    
    	// Find each product in the cart and add it to the $cart_ids array
    	foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    		$cart_product = $values['data'];
    		$cart_ids[]   = $cart_product->id;
    	}
    
    	// Connect the products in the cart w/ their categories
    	foreach( $cart_ids as $id ) {
    		$products_categories = get_the_terms( $id, 'product_cat' );
    
    		// Loop through each product category and add it to our $cart_categories array
    		foreach ( $products_categories as $products_category ) {
    			$cart_categories[] = $products_category->slug;
    		}
    	}
    
    	// If one of the special categories are in the cart, return true.
    	if ( ! empty( array_intersect( $categories, $cart_categories ) ) ) {
    		return true;
    	} else {
    		return false;
    	}
    }
    //  Disable paysit if balance is more than subtotal
    function paysit_gateway_disable( $available_gateways ) {
            if ( ! payments_category_is_in_the_cart() ) {
        $subtotal = wc()->cart->get_subtotal('edit');
        $balance = woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), 'edit');
        if ($balance > $subtotal){
        unset(  $available_gateways['paysit'] );
    }
    }
    return $available_gateways;
    }
    add_filter( 'woocommerce_available_payment_gateways', 'paysit_gateway_disable' );
    • This topic was modified 7 years, 1 month ago by sadiqodunsi.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Subrata Mal

    (@subratamal)

    Use updated code.

    function paysit_gateway_disable($available_gateways) {
        if (!is_admin()) {
            if (!payments_category_is_in_the_cart()) {
                $subtotal = wc()->cart->get_subtotal('edit');
                $balance = woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), 'edit');
                if ($balance > $subtotal) {
                    unset($available_gateways['paysit']);
                }
            }
        }
        return $available_gateways;
    }
    
    add_filter('woocommerce_available_payment_gateways', 'paysit_gateway_disable');
    Thread Starter sadiqodunsi

    (@sadiqodunsi)

    Hello,

    You are awesome Subrata. Everything now works as it should.

    Thank you so much
    Sadiq

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘problem with the code to hide a payment gateway’ is closed to new replies.