Skip to content

Instantly share code, notes, and snippets.

@s-a-s-k-i-a
Created September 21, 2022 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-a-s-k-i-a/9a67b7d5261552e15381e181bfe34137 to your computer and use it in GitHub Desktop.
Save s-a-s-k-i-a/9a67b7d5261552e15381e181bfe34137 to your computer and use it in GitHub Desktop.
Unset payment gateway if a product in specific category is in cart _ WooCommerce
<?php
/**
* @snippet Disable Payment Method for Specific Category
*
*/
function my_unset_gateway_by_category( $available_gateways ) {
global $woocommerce;
$parentCatID = '';
$category_IDs = array( 373, 436, 374, 375, 380, 376 );
if(!is_admin()){
$items = $woocommerce->cart->cart_contents;
if(isset($items)){
foreach ( $items as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
$verkauf_bis = get_field( 'verkauf_bis', $values['product_id'] );
$heute = date( 'Y-m-d H:i' );
$dt_days = 6;
if ( ! empty( $verkauf_bis ) ) {
$dt_verkauf_bis = new DateTime( $verkauf_bis );
$dt_heute = new DateTime( $heute );
$dt_days = $dt_verkauf_bis->diff( $dt_heute );
$dt_days = $dt_days->format( '%a' );
}
if ( $dt_days > 5 ) {
foreach ( $terms as $term ) {
$product_cat_id = $term->term_id;
$product_parent_categories_all_hierarchy = get_ancestors( $product_cat_id, 'product_cat' );
$last_parent_cat = array_slice( $product_parent_categories_all_hierarchy, -1, 1, true );
foreach ( $last_parent_cat as $last_parent_cat_value ) {
$parentCatID = $last_parent_cat_value;
}
if ( ( in_array( $term->term_id, $category_IDs ) ) /* OR ($term->term_id = $parentCatID) */ ) {
/* unset( $available_gateways['direct_deposit'] ); */
unset( $available_gateways['bacs'] );
unset( $available_gateways['paypal'] );
unset( $available_gateways['ppcp-gateway'] );
unset( $available_gateways['stripe'] );
break;
}
break;
}
} else {
/* unset( $available_gateways['direct-debit'] ); */
unset( $available_gateways['bacs'] );
}
}
}
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'my_unset_gateway_by_category' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment