This Is How To Hide Price For Specific Categories On Your WooCommerce Cart Page
Are you running a WooCommerce store and want to hide the price and total on the cart page for specific product categories? This can be a useful feature if you have certain products that you don't want customers to see the price for, such as wholesale or trade items.
Luckily, WooCommerce provides a handy filter that allows you to customize the display of prices and totals on the cart page. In this article, we'll walk you through the exact code you need to implement this functionality on your WooCommerce store.
Understanding The Problem
By default, WooCommerce displays the price and total for each item in the customer's cart. However, there may be situations where you want to hide this information for specific product categories.
For example, let's say you sell both retail and wholesale products on your WooCommerce store. You may want to hide the prices and totals for the wholesale products so that only authorized customers can see them.
The Solution
The solution to this problem is to use the woocommerce_cart_item_price
and woocommerce_cart_item_subtotal
filters provided by WooCommerce. These filters allow you to modify the price and total displayed for each item in the cart.
Here's the code you can use to implement this functionality:
add_filter('woocommerce_cart_item_price', 'hide_woocommerce_cart_item_price', 10, 3);
add_filter('woocommerce_cart_item_subtotal', 'hide_woocommerce_cart_item_price', 10, 3);
function hide_woocommerce_cart_item_price($price, $cart_item, $cart_item_key) {
$product = wc_get_product($cart_item['product_id']);
$hide_for_categories = array(
'berkley', 'cotton-lite', 'kinna', 'linen', 'luster', 'nairobi', 'panama', 'plisse', 'prints', 'sequoia', 'shantung', 'brocade', 'boucle', 'dover', 'lite-out', 'lite-out-duplex', 'moire', 'sheerweave-blackout', 'sutton', 'texas-green', 'windsor', 'sheer', 'sheerweave-3-5-10', 'sheerweave-specialty'
);
// Don't show price when it's in one of the categories
if (has_term($hide_for_categories, 'product_cat', $product->get_id())) {
return '';
}
return $price;
}
Let's break down the code:
-
add_filter('woocommerce_cart_item_price', 'hide_woocommerce_cart_item_price', 10, 3);
and add_filter('woocommerce_cart_item_subtotal', 'hide_woocommerce_cart_item_price', 10, 3);
are the filters that hook into the WooCommerce cart functionality. These filters allow us to modify the price and total displayed for each item in the cart.
-
The hide_woocommerce_cart_item_price()
function is where the actual logic is implemented. This function takes three parameters: $price
, $cart_item
, and $cart_item_key
.
-
Inside the function, we first get the product object using wc_get_product($cart_item['product_id'])
.
-
We then define an array of product categories that we want to hide the price and total for, $hide_for_categories
. In this example, we've included a list of categories, but you can customize this to fit your specific needs.
-
Finally, we check if the current product is in one of the categories we want to hide using the has_term()
function. If the product is in one of the categories, we return an empty string, which effectively hides the price and total. Otherwise, we return the original $price
value.
By implementing this code, the price and total will be hidden for any products that belong to the categories specified in the $hide_for_categories
array.
Testing and Verifying the Functionality
To ensure that the price and total are being properly hidden for the specified product categories, you can follow these steps:
-
Add products to the cart: Add some products to the cart, making sure that you have a mix of products from the "hide" categories and other categories.
-
Visit the cart page: Navigate to the cart page and inspect the cart items. You should see that the price and total are hidden for the products in the "hide" categories, while the price and total are displayed normally for the other products.
-
Inspect the code: Check the source code of the cart page to ensure that the hide_woocommerce_cart_item_price()
function is being called and that the appropriate products are having their price and total hidden.
-
Test different scenarios: Try adding and removing products from the cart, as well as changing the product categories, to ensure that the price and total hiding functionality works as expected in all cases.
By following these steps, you can be confident that the price and total hiding functionality is working correctly on your WooCommerce store.
Conclusion
In this article, we've shown you how to easily hide the price and total on the WooCommerce cart page for specific product categories. This can be a useful feature for stores that sell both retail and wholesale products, or for any other scenario where you need to selectively hide price information.
Remember, the code snippet we provided is just a starting point, and you may need to customize it to fit your specific requirements. If you have any questions or need further assistance, feel free to reach out to the Flowpoint.ai team, who can help you identify and fix any technical issues on your WooCommerce store.