Filter Pay options by product category
-
Hello, Would you be able to assist me in setting this up so that the buttons only display for specific product categories? I sell certain products that would violate the Paypal TOS so I need to filter those out by category.
The page I need help with: [log in to see the link]
-
Hi @stepanyanr
Also would this PHP function work with your Stripe (Google Pay & Apple Pay) Application?
I reviewed your site http://araxsystemsllc.com and you’re using WooCommerce Payments for your Stripe plugin.
Kind Regards,
Anyway we can get the paypal option to not display on checkout & cart pages when the function is triggered? Currently the function is working on the product single pages but when you place the product in the cart the paypal button shows up regardless.
Cart Page:
add_filter('wc_ppcp_cart_payment_gateways', function($gateways){ $my_category_ids = [277, 270, 457, 458, 375, 374, 370]; //This is a list of all your category ID's to exclude. foreach(WC()->cart->get_cart() as $key => $values){ $categories = $values['data']->get_category_ids(); if(array_intersect($my_category_ids, $values['data']->get_category_ids())){ return []; } } return $gateways; });
Checkout Page:
add_filter('woocommerce_available_payment_gateways', function($gateways){ $my_category_ids = [277, 270, 457, 458, 375, 374, 370]; //This is a list of all your category ID's to exclude. foreach(WC()->cart->get_cart() as $key => $values){ $categories = $values['data']->get_category_ids(); if(array_intersect($my_category_ids, $values['data']->get_category_ids())){ unset($gateways['ppcp']); return $gateways; } } return $gateways; });
Thank you for sharing this, the Cart PHP snippet is working but when I apply the Checkout snippet it results in a total crash of the site.
add_filter('woocommerce_available_payment_gateways', function($gateways){ $my_category_ids = [277, 270, 457, 458, 375, 374, 370, 377, 343, 388, 269, 323, 340, 365, 327, 342, 119, 441, 345, 344, 291, 292, 293, 360, 366, 288]; //This is a list of all your category ID's to exclude. foreach(WC()->cart->get_cart() as $key => $values){ $categories = $values['data']->get_category_ids(); if(array_intersect($my_category_ids, $values['data']->get_category_ids())){ unset($gateways['ppcp']); return $gateways; } } return $gateways; });
It’s erroring because the cart object isn’t defined on the Admin side of WordPress. This works:
add_filter('woocommerce_available_payment_gateways', function ($gateways) { if (is_checkout()) { $my_category_ids = [277, 270, 457, 458, 375, 374, 370, 377, 343, 388, 269, 323, 340, 365, 327, 342, 119, 441, 345, 344, 291, 292, 293, 360, 366, 288]; //This is a list of all your category ID's to exclude. foreach (WC()->cart->get_cart() as $key => $values) { $categories = $values['data']->get_category_ids(); if (array_intersect($my_category_ids, $values['data']->get_category_ids())) { unset($gateways['ppcp']); return $gateways; } } } return $gateways; });
- This reply was modified 1 year, 5 months ago by Payment Plugins.
This is great! It’s working the main Paypal button disappears but the “express checkout” paypal button remains. In the settings it seems like there is no option to disable the “express checkout” button and the only button to customize is the one that shows up for express checkout. Maybe I am missing something?
@stepanyanr You can disable PayPal from the express section via the PayPal Settings page.
Or use this code snippet:
add_filter('wc_ppcp_express_checkout_payment_gateways', function($gateways){ $my_category_ids = [277, 270, 457, 458, 375, 374, 370, 377, 343, 388, 269, 323, 340, 365, 327, 342, 119, 441, 345, 344, 291, 292, 293, 360, 366, 288]; foreach(WC()->cart->get_cart() as $key => $values){ $categories = $values['data']->get_category_ids(); if(array_intersect($my_category_ids, $values['data']->get_category_ids())){ return []; } } return $gateways; });
I can’t find the button to turn off just the “express checkout button ” that’s not getting effected by the PHP controlling the checkout category exclusions. I only see and enable button for the whole paypal gateway.
The option to enable/disable PayPal in the express section is shown in the following screenshot. It’s the last option in the screenshot labeled
PayPal Payment Sections
.that’s not getting effected by the PHP controlling the checkout category exclusions
The code provided is an example, you will need to ensure all of your category ID’s are populated in the list.
- This reply was modified 1 year, 5 months ago by Payment Plugins.
Perfect thank you so much!
You’re welcome. We always appreciate a good review of the plugin if you have a moment.
https://wordpress.org/support/plugin/pymntpl-paypal-woocommerce/reviews/
Thank you, I have left a review. Is there a way to make a donation?
Also would these functions work for the Gpay & Apple Pay plugin you have?
Hi @stepanyanr
Thank you for the nice review.
Is there a way to make a donation?
No, we don’t have a donation page setup at this time but thank you for the thought.
Also would these functions work for the Gpay & Apple Pay plugin you have?
The code would be very similar but the names of the hooks you would use would be different. If you need assistance with how to achieve this using the Stripe plugin you can create a support request here.
Kind Regards,
Well, okay then. I certainly think your support was worthy of monetary compensation. I will reach out to you guys on the Gpay app once I am ready to deploy on that! thank you again.
- The topic ‘Filter Pay options by product category’ is closed to new replies.