4
25/11/2024 12:31 pm
Início do tópico
Tenho uma loja eletrónica WooCommerce e, para uma categoria específica (livros electrónicos), pretendo remover o método de pagamento contra-reembolso (cod).
Como é que o posso fazer com um código em functions.php?
Obrigado, Sam!
2 Respostas
3
25/11/2024 12:36 pm
Aqui está o código, meu amigo (coloque-o em functions.php no seu tema filho):
function custom_payment_gateway_for_product_category($available_gateways) {
if (is_admin()) return $available_gateways;
// Define the product category or tag ID
$target_category = 'example-category'; // Replace with your category slug
// Get the items in the cart
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
// Check if the product belongs to the target category
if (has_term($target_category, 'product_cat', $product_id)) {
// Allow only specific payment gateway(s) and disable others
unset($available_gateways['cod']); // Disable Cash on Delivery (COD)
// You can unset other gateways similarly, if needed
}
}
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'custom_payment_gateway_for_product_category');

