Created
October 4, 2016 12:10
-
-
Save davidchc/f77110c125e8f2c0ee3cdea94ded558e to your computer and use it in GitHub Desktop.
Habilitar método de pagamento por produto. No exemplo define os produtos q irao exibir um determinando método de pagamento
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function custom_woocommerce_payment_by_product($gateways){ | |
$products = array(3, 5); //id dos produtos | |
//Se existir o método de pagamento, e não tiver os produtos no carrinho, e remove o metodo de pagamento | |
if( isset( $gateways['pagseguro']) && !is_custom_has_product_cart($products)){ | |
unset( $gateways['pagseguro']); | |
} | |
return $gateways; | |
} | |
add_filter("woocommerce_available_payment_gateways", "custom_woocommerce_payment_by_product"); | |
/*Função pra verificar se o produto está no carrinho*/ | |
function is_custom_has_product_cart($products){ | |
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$_product = $values['data']; | |
if(in_array($_product->id, $product) ) { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment