Hi @zulu11,
Thank you for your honest review of the plugin. The engineering team here on WordPress.org (the authors of the plugin) are more than happy to assist with this challenge.
The afterpay_is_product_supported hook (documented here) can be leveraged to achieve the outcome described above. If a callback function is attached to this Filter with two arguments, the second will be populated with a WC_Product object. This object exposes a method called get_category_ids, which can be used to exclude Afterpay from a given list of Product Categories.
For example:
/**
* @param bool $bool_result
* @param WC_Product $product
*/
function afterpay_ips_callback( $bool_result, $product ) {
# Categories #1 and #3 don't support Afterpay.
if (array_intersect( $product->get_category_ids(), array(1, 3) ) {
$bool_result = false;
}
return $bool_result;
}
add_filter( 'afterpay_is_product_supported', 'afterpay_ips_callback', 10, 2 );
Please try to implement something similar to the above inside the active theme’s functions.php file, replacing the category IDs as needed. Don’t hesitate to reply back to this thread if further support is needed.
Thank you.
Thread Starter
Gary
(@zulu11)
Hi Thanks for your prompt response! Much appreciated.
I have made changes to the category ids and tried this in the functions file but I am getting a syntax error – syntax error is -> unexpected “;” after the line:
$bool_result=false;
I have tried making some other small changes to the code to find the problem but I have had no luck, do you have any other suggestions?
Many thanks.
Hi Gary,
You are correct, there is a syntax error in the code provided above. The line before the line with $bool_result=false; is missing a closing parenthesis. It should read as follows:
if (array_intersect( $product->get_category_ids(), array(1, 3) )) {
Please excuse the mistake. Let us know how you go with the amendment above.
Thank you.
Thread Starter
Gary
(@zulu11)
Hi Thanks very much that worked perfectly! It is now not offering the Afterpay on my selected categories – however I am still able to use Afterpay at the checkout. Is there a way to prevent this?
Hi Gary,
Great to hear that the solution is operating as intended for most pages. The expectation is that Afterpay would also be unavailable at the checkout if one or more products in the cart are identified as unsupported using this method.
Please leave this with the team to investigate further on your behalf. Will reply back here within 2 business days.
Thank you also for updating your rating!
Hi @zulu11,
Here is a temporary solution to remove it from the available payment methods. Please copy the following snippet to the function.php in the active theme and replace the category IDs as needed.
/**
* @param array $gateways
*/
function afterpay_apg_callback( $gateways ) {
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
# Categories #1 and #3 don't support Afterpay.
if (array_intersect( $product->get_category_ids(), array(1, 3) )) {
unset($gateways['afterpay']);
break;
}
}
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'afterpay_apg_callback', 10, 2);
The engineering team will also include a similar enhancement in the next release.
Thank you.
-
This reply was modified 5 years, 11 months ago by
Afterpay.
Thread Starter
Gary
(@zulu11)
Thank you so much for your help with this. It works perfectly now, I appreciate the time you took to help me with a solution!
Hi Gary,
More than happy to help. Glad it’s all working now.
Thank you.