Got into an issue with a client today, and found this article to get what I want. But it was not complete for me.
I want to disable payment, disable shipping, disable subtotal, and replace with a text box, if there was more than 5 products in cart. On top of that, I want to do the same just with 2 products in cart, for a custom category. This is done with this PHP snippet, that goes into functions. If you got any issues, please write to me and I will help with the code. It's pretty complex 🙂
Steps:
// Disable payment for flat_rate:4 and adjust based on product category
function disable_payment_for_flat_rate_4($needs_payment) {
// Check if WooCommerce is active
if (class_exists('WooCommerce')) {
// Check if flat_rate:4 is selected as the shipping method
$chosen_shipping_method = WC()->session->get('chosen_shipping_methods');
if (in_array('flat_rate:4', $chosen_shipping_method)) {
// Get the cart contents
$cart_contents = WC()->cart->get_cart_contents_count();
// Set the maximum allowed products
$max_allowed_products = 5;
// Check if there are products in the "bundle" category
$bundle_category_products = false;
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
$product_categories = get_the_terms($product_id, 'product_cat');
// Check if the product belongs to the "bundle" category
if ($product_categories && is_array($product_categories)) {
foreach ($product_categories as $category) {
if ($category->slug === 'bundle') {
$bundle_category_products = true;
break;
}
}
}
}
// Adjust the maximum allowed products if there are products in the "bundle" category
if ($bundle_category_products) {
$max_allowed_products = 2;
}
// If the cart contains more than the allowed products, disable payment
if ($cart_contents > $max_allowed_products) {
$needs_payment = false;
// Enqueue the custom JavaScript
add_action('wp_footer', 'disable_place_order_button_script');
}
}
}
return $needs_payment;
}
// Hook the function to the 'woocommerce_cart_needs_payment' filter
add_filter('woocommerce_cart_needs_payment', 'disable_payment_for_flat_rate_4', 10, 1);
// Enqueue custom JavaScript to replace "Place Order" button with custom text
function disable_place_order_button_script() {
?>
<script type="text/javascript">
jQuery(document).ready(function($){
// Delay the execution to ensure the "Place Order" button is fully loaded
setTimeout(function(){
// Remove the "Place Order" button and replace it with custom text
$('.form-row.place-order').html('<p>Contact us for more information about purchasing and shipping price for this order.</p>');
}, 1000); // Adjust the delay time as needed
});
</script>
<?php
}
// Enqueue custom CSS to hide label for flat_rate:4 shipping method
function disable_label_for_flat_rate_4_style() {
// Check if flat_rate:4 is selected as the shipping method
$chosen_shipping_method = WC()->session->get('chosen_shipping_methods');
if (in_array('flat_rate:4', $chosen_shipping_method)) {
// Get the cart contents
$cart_contents = WC()->cart->get_cart_contents_count();
// Set the maximum allowed products
$max_allowed_products = 5;
// Check if there are products in the "bundle" category
$bundle_category_products = false;
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
$product_categories = get_the_terms($product_id, 'product_cat');
// Check if the product belongs to the "bundle" category
if ($product_categories && is_array($product_categories)) {
foreach ($product_categories as $category) {
if ($category->slug === 'bundle') {
$bundle_category_products = true;
break;
}
}
}
}
// Adjust the maximum allowed products if there are products in the "bundle" category
if ($bundle_category_products) {
$max_allowed_products = 2;
}
// If the cart contains more than the allowed products, enqueue the custom CSS
if ($cart_contents > $max_allowed_products) {
?>
<style type="text/css">
table.shop_table.woocommerce-checkout-review-order-table {
display: none;
}
</style>
<?php
}
}
}
// Hook the function to the 'wp_head' action
add_action('wp_head', 'disable_label_for_flat_rate_4_style');
Liked this article? Please follow me on TwitterFollow @baysorensen