I attempted to ask Customizr support what my code was running into, but they basically said they do not support 3rd party plugins such as Woocommerce
I needed to restrict the payment types based on what folk were buying on the site. For example, the Check payment type is only available for people buying lessons.
Here is the code that does this:
<?php add_filter( 'woocommerce_available_payment_gateways', 'threshold_unset_gateway_by_category' ); function threshold_unset_gateway_by_category( $available_gateways ) { global $woocommerce; $unset = false; $category_ids = array( 22, 21, 25, 20); foreach ( $woocommerce->cart->get_cart() as $key => $values ) { $terms = get_the_terms( $values['product_id'], 'product_cat' ); foreach ( $terms as $term ) { if ( in_array( $term->term_id, $category_ids ) ) { $unset = true; break; } } } if ( $unset == true ) unset( $available_gateways['cheque'] ); return $available_gateways; }
I have dug into the Customizr files but I cannot find any conflict. The WordPress files can be a bit convoluted, so I might be barking up the wrong tree.
Advertisement
Answer
So the solution to this was much more simpler than I expected. Thank you @LoicTheAztec for the tips, I utilized Loic’s code for better optimization. The fix was to check if the user is admin
if ( is_admin() ) { return $available_gateways; }
Then put the rest of the code in an else. Here is the full solution:
add_filter( 'woocommerce_available_payment_gateways', 'categories_unset_cheque_gateway', 99, 1 ); function categories_unset_cheque_gateway( $gateways ){ if (is_admin()){ return $gateways; } // BELOW define your categories in the array (can be IDs, slugs or names) else{ $categories = array( 15 ); // Loop through cart items checking for specific product categories foreach ( WC()->cart->get_cart() as $cart_item ) if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){ unset( $gateways['cheque'] ); break; // Stop the loop } return $gateways; } }