0

i have an incompatibility with two plugins. there I need to disable the PayPal quick check out if a product from a special category is being bought. this works perfectly fine from a frontend perspective:

add_filter( 'woocommerce_available_payment_gateways', 'abo_no_paypalCheckout' );

function abo_no_paypalCheckout( $available_gateways ) {
global $woocommerce;

$cat_in_cart = false;
if (WC()->cart && !WC()->cart->is_empty()){
// Loop through all products in the Cart        
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    #echo ($cart_item['product_id']);
    // If Cart has category "download", set $cat_in_cart to true
    if ( (has_term( 'abc', 'product_cat', $cart_item['product_id'] ))  ) {
        $cat_in_cart = true;
        break;
    }
}
}    
// Do something if category "download" is in the Cart      
if ( $cat_in_cart ) {
unset( $available_gateways['ppec_paypal'] );

}
 return $available_gateways;
}   

somehow I get a fatal error trying to access woo > settings > payments and this in the log:

2020-10-19T12:05:58+00:00 CRITICAL Uncaught Error: Call to a member function get_cart() on null in /www/shop/public/wp-content/themes/woondershop-pt-child/functions.php:536 Stack trace: #0 /www/shop/public/wp-includes/class-wp-hook.php(288): abo_no_paypalCheckout(Array) #1 /www/shop/public/wp-includes/plugin.php(206): WP_Hook->apply_filters(Array, Array) #2 /www/shop/public/wp-content/plugins/woocommerce/includes/class-wc-payment-gateways.php(160): apply_filters('woocommerce_ava...', Array) #3 /www/shop/public/wp-content/plugins/woocommerce-subscriptions/includes/gateways/class-wc-subscriptions-payment-gateways.php(115): WC_Payment_Gateways->get_available_payment_gateways() #4 /www/shop/public/wp-content/plugins/woocommerce-subscriptions/includes/admin/class-wc-subscriptions-admin.php(1853): WC_Subscriptions_Payment_Gateways::one_gateway_supports('subscriptions') #5 /www/shop/public/wp-includes/class-wp-hook.php(288): WC_Subscriptions_Admin::add_recurring_payment_gateway_informatio in /www/shop/public/wp-content/themes/woondershop-pt-child/functions.php in Zeile 536

line 536 is the beginning of the foreach.

any ideas?

thanks!

--- EDIT ---

code fixed!

Ele
  • 523
  • 2
  • 6
  • 19
  • View this answer: [Avoid error “call to a member function get_cart()…” in WooCommerce function](https://stackoverflow.com/a/64425183/11987538), while it is a different filter hook, the solution to your question to avoid the error is the same – 7uc1f3r Oct 20 '20 at 12:48
  • 1
    I slightly change the code and put it before the loop: if (WC()->cart && !WC()->cart->is_empty()){ and it worked! – Ele Oct 20 '20 at 13:03
  • 1
    Ok, you could also remove `global $woocommerce;` since you don't use it in your code. Regards – 7uc1f3r Oct 20 '20 at 13:07

0 Answers0