• Resolved Machtnix

    (@hillyfov)


    Hello,

    I was wondering if it was possible to create a function that checks on some conditions and disables the PayPal Smart Buttons in the mini cart aswell.

    What I need is a function that prevents the customer to check out using the PayPal Smart Buttons if there is a product from a certain product category in their cart but they haven’t applied a coupon code (coupon is mandatory for some products).

    As for now I have the following function which seems to work for the cart page but not the mini cart:

    add_filter( 'woocommerce_available_payment_gateways', 'my_disable_paypal' );
    function my_disable_paypal( $available_gateways ) {
        $product_categories = array( 'my-category' ); // category ID or slug of targeted category
    
        if( empty(WC()->cart->get_applied_coupons()) ) { // check if any coupons were applied
            $coupon_applied = false;
        } else {
            $coupon_applied = true;
        }
    
        foreach ( WC()->cart->get_cart() as $cart_item ){ // check the cart for products with given product category if no coupons were applied
            if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && ! $coupon_applied ) { 
                unset( $available_gateways['ppcp-gateway'] ); // found one, disable PayPal payment gateway
                break; // stop the loop 
            }
        }
        return $available_gateways;
    }

    I have found a similar question here but I did not find quite a solution for the mini cart.

    So my question is if and how it’s possible to remove PayPal Smart buttons especially from the mini cart.

    Would you be so kind and have a look? Could you maybe point me in the right direction?

    Thank you in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Syde Niklas

    (@niklasinpsyde)

    Hi @hillyfov

    At this time, PayPal Payments only provides a filter to disable the PayPal buttons on the single product page.

    The Cart button will automatically be disabled when the Checkout gateway has been unset.

    But as you noticed, this does not affect the availability of the Mini Cart button. Eventually, the team is looking into providing more control over all button locations.

    One way to potentially hide the Mini Cart buttons would be with CSS:

    #ppc-button-minicart {
    display: none !important;
    }

    Alternatively you could try setting an empty render hook for the Mini Cart buttons (in theory, this works with all button locations):

    add_filter('woocommerce_paypal_payments_mini_cart_button_renderer_hook', function() {
        return '';
    });

    I hope this provides you with some direction to extend your code.
    Please let us know if you could incorporate these for your use case.

    Thanks!

    Kind regards,
    Niklas

    Thread Starter Machtnix

    (@hillyfov)

    Thank you, @niklasinpsyde!

    Returning the empty renderer hook works per se but for some reason I couldn’t make it work when certain conditions meet only. Probably because the mini cart uses Ajax, right?

    So I ended up setting WooCommerce session variables to determine whether my conditions are met and then modifying mini cart fragments using the woocommerce_add_to_cart_fragments hook to remove the element #ppc-button-minicart from the mini cart completely.

    This way the buttons will be removed as soon as I add a product which requires a coupon code and will be present again after I remove the product from my cart.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Conditionally disable PayPal Smart Buttons in Mini Cart?’ is closed to new replies.