WooCommerce: Check if Product Category is in the Cart

We already studied how to detect if a product ID is in the cart – but if you take a look at the comments many of you were asking how to detect product categories.

So, today we’ll do exactly that. You can disable shipping rates, payment gateways, you can print messages, you can apply coupon programmatically… there are lots of things you can do “conditionally”, based on whether a given product category is in the Cart or not.

Continue reading WooCommerce: Check if Product Category is in the Cart

WooCommerce: Display Cart Item Subtotal With Coupon Discount

This is a nice follow up from last week’s snippet “WooCommerce: Slashed Cart Subtotal if Coupon @ Cart“, where I showed how to display original/discounted cart total on the same totals table row.

This time, I want to let users know the original and discounted cart item (product) amount after a certain coupon is applied. Who knows – this might improve your Cart U/X πŸ™‚ Continue reading WooCommerce: Display Cart Item Subtotal With Coupon Discount

WooCommerce: Display Product Categories @ Cart & Checkout Pages

While working for a freelance client I had to “detect” the cart item categories in order to apply some PHP customization.

So I thought – why not share with you how to display product categories in the Cart and Checkout? This adds a nice touch to those two vital pages, and prints a list of product categories under each cart item.

Also, I’m glad to introduce you to the amazing world of “wc_get_product_category_list“, a very handy WooCommerce PHP function!

Continue reading WooCommerce: Display Product Categories @ Cart & Checkout Pages

WooCommerce: Change Add to Cart Quantity into a Select Drop-down

The default WooCommerce Add to Cart “Quantity Input” is a simple input field where you can enter the number of items or click on the “+” and “-” to increase/reduce the quantity.

A freelance client hired me to turn that input into a “Select” drop-down. For their audience and UX requirements, it makes sense to let their customers choose the quantity from a drop-down instead of having to manually input the number.

Online there are complex snippets, but I decided to make things easier. The WooCommerce function responsible to generate the quantity input is called “woocommerce_quantity_input“.

Luckily, it’s a pluggable function – which means we can simply add this exact same function name to our child theme’s functions.php to completely override it. Enjoy!

Continue reading WooCommerce: Change Add to Cart Quantity into a Select Drop-down

WooCommerce: Check if Product ID is in the Cart

Here’s the story: I’m working with one of my freelance clients and I need to show certain content in the Checkout (a product-specific “Terms and Conditions”) if such product is in the Cart.

Now, I’ve always looked for products in the Cart by “looping” through the Cart with a foreach (here, for example: Apply a Coupon Programmatically if a Product is in the Cart). But as I said, after some random research, I found out about another magic WooCommerce function: “find_product_in_cart()“. Which means finding a product in the Cart doesn’t require custom loops or complex PHP… it’s just a “one liner”. Enjoy!

Continue reading WooCommerce: Check if Product ID is in the Cart

WooCommerce: Force Cart to Specific Amount (Deposit)

Here’s a very simple snippet that achieves a very complex task – what if you wanted to force your Cart to charge a deposit or a fixed fee, no matter the cart total?

Well, thankfully WooCommerce is pretty flexible and a lot of workarounds can be found.

In this case, we will study two possible solutions: (1) a negative “cart fee” to make the total become e.g. $100 and (2) a filter to completely override the calculated cart total e.g. $100.

Sounds like Japanese? Great – here’s why you’re on Business Bloomer. Copy the snippet, apply it to your test WooCommerce site and see the magic happen – without knowing anything about coding!

Continue reading WooCommerce: Force Cart to Specific Amount (Deposit)

WooCommerce: Edit Add to Cart Default, Min, Max & Step Product Quantity

Yes, there are many plugins that already achieve this. But my goal at Business Bloomer is to save you from plugin conflicts, delicate updates and to make you learn some PHP.

So, here’s how you can add, with a few lines of PHP, a minimum, maximum, increment and default value to your Add to Cart quantity input field on the single product and cart pages. Who knew it was this easy?

Continue reading WooCommerce: Edit Add to Cart Default, Min, Max & Step Product Quantity

WooCommerce: See What’s Inside the Cart Array (print_r)

So many times I needed to “play” with Cart contents in order to modify the default behaviour (such as removing a columns, hide the sale price to prices, hide an item, etc.).

There is a great PHP function that always helps – it’s called print_r and you can use this to “see” what’s inside the cart array, so that you can return certain information in a message for example.

A bit advanced, but as you grow your WooCommerce coding skills, you will use this a lot πŸ™‚ Continue reading WooCommerce: See What’s Inside the Cart Array (print_r)

WooCommerce: “Buy One Get One” (BOGO)

Case scenario: if you add a product ID to cart with a specific, you want another product automatically added to cart (Buy One Get One, or “BOGO”).

This second product should have price = 0 if you wish to completely give it away, or maybe a set sale price. It should also be set to “hidden” because maybe you want to hide this free product from the shop and only gift it when the first one is added to Cart.

Also, if you remove product 1, the gifted product should go away from the Cart too. So here follows the PHP snippet of course!

Continue reading WooCommerce: “Buy One Get One” (BOGO)

WooCommerce: Weight-Based Shipping Methods

With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions.

So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that weight?

Well, you can use simple PHP to accomplish lots of “advanced” shipping rules, such as shipping by weight.

As long as all your products have their weight information filled in, you can create some simple rules to conditionally hide/show certain shipping methods based on cart weight. Enjoy!

Continue reading WooCommerce: Weight-Based Shipping Methods