Rodolfo Melogli

WooCommerce Function of the Week: wc_get_endpoint_url

This week’s WooCommerce function is about endpoints. What’s an endpoint? Endpoints are “an extra part in the website URL that is detected to show different content when it’s present. For example: You may have a ‘My Account’ page shown at yoursite.com/my-account. When the endpoint ‘edit-account’ is appended to this URL, making it ‘yoursite.com/my-account/edit-account‘ then the Edit Account page is shown instead of the My Account page.” (Source: WooCommerce).

There are dozens of WooCommerce endpoints, especially for the Checkout and My Account page, as well as custom endpoints added by third-party WooCommerce plugins. It’s difficult to remember all the different URLs, so here comes wc_get_endpoint_url to the rescue.

As usual, in this article, we’ll study the WooCommerce core function code for wc_get_endpoint_url in this tutorial, see where and why it’s used, and finally, we’ll look at a brief case study. Enjoy!

Read more

WooCommerce Function of the Week: woocommerce_wp_text_input

Learn how to add custom fields to settings in WooCommerce meta boxes — and give back to WooCommerce when you find some code that can be improved.

There are times when you need to “mess” with meta boxes in the WooCommerce back end. Meta boxes are the draggable boxes you find in the product, order, and coupon edit pages.

WooCommerce meta boxes contain the current custom post type settings for your products. Think of the “regular price” — that’s a custom field you can find in the “Product Data” meta box in the “product” custom post type edit screen.

Today, we’ll study the woocommerce_wp_text_input function. It provides a neat way to generate an additional text input in case you need additional settings for your products.

For example, suppose you have a “regular price” and a “sale price” but you also want to have a “list price” or “msrp” — a manufacturer’s suggested retail price (MSRP). There’s two ways you could do this:

  • NOT the best way: You could write the MSRP into the HTML description field for every product yourself, but this locks the price as static text in the content where it really doesn’t belong. You might lose track of it or accidentally write over it, and you can’t reuse it on other products without doing the same thing.
  • The BEST way: Instead, you can use woocommerce_wp_text_input to quickly generate an input field on the back end with the right classes, name and id for the product. That means you can store the additional price in the database once the current post is saved and use it on other products.

As usual, we’ll study the WooCommerce core function code for woocommerce_wp_text_input in this tutorial, see where and why it’s used, and finally, we’ll cover a quick case study. Enjoy!

Read more

WooCommerce Function of the Week: wc_current_user_has_role

What if you want to show hidden information only to “administrators” or “shop managers?” What about displaying a custom “My Account” tab just for logged-in customers?

Running functions based on user roles by using conditional logic is a common part of WooCommerce customization.

What if you want to show hidden information only to “administrators” or “shop managers?” What about displaying a custom “My Account” tab just for logged-in customers?

Well, wc_current_user_has_role allows us to check just that: what role group does the logged-in user belong to.

WordPress has six pre-defined roles: Super Admin, Administrator, Editor, Author, Contributor, and Subscriber. WooCommerce adds another two: Shop Manager and Customer. Other plugins may add more. You can theoretically create any number of user roles. Each role is allowed to perform a defined set of tasks called capabilities.

As usual, we’ll study the WooCommerce core function code for wc_current_user_has_role, see where and why it’s used, and finally, we’ll cover a quick case study. Enjoy!

Read more

WooCommerce Function of the Week: add_fee

Before we dive in this week’s function, please note that it’s usually against any payment provider’s Terms of Service (like PayPal’s) to add fees to a transaction based on the customer’s chosen payment gateway, so please make sure to use “cart fees” in a legal way.

You got it — in this issue we’ll study the add_fee WooCommerce function, which indeed gives us the power to add custom fees to orders at checkout based on any criteria you define.

As usual, we’ll study the WooCommerce core function code, see where and why it’s used, and finally we’ll cover a quick case study. Enjoy!

Read more

WooCommerce Function of the Week: wc_get_product_category_list

Here’s yet another time-saving WooCommerce function. No need to reinvent the wheel — with a single line of code and no custom queries, you can get all the categories a product belongs to.

This week’s function is wc_get_product_category_list, and there’s no need to explain what it does as its name is self-explanatory.

As usual, we’ll study the WooCommerce core function code, see where and why it’s used, and finally we’ll cover a quick case study. Enjoy!

Read more

WooCommerce Function of the Week: get_ip_address

The get_ip_address function is useful for tracking visitors who aren’t logged in as they move through key pages in your store.

Whether it’s for troubleshooting payments, creating user history logs or enabling custom geolocation, there is a WooCommerce function that lets you forget about all the $_SERVER global keys and gets you an immediate result: the current user’s IP address.

You can store an IP address in a custom field or use it to trigger another custom function. Bear in mind, IP addresses are personal identification data and therefore regulated by the GDPR: “That means that some form of justification like user consent would always be needed as soon as the IP address is processed.”

So, let’s take a look at the function code, its usage, and a case study.

Read more

WooCommerce Function of the Week: get_variation_prices

B/w photo of clothing rack

Read the code with Rodolfo Melogli! This week in WooCommerce functions we’re looking at get_variation_prices.

If you’re a WooCommerce developer, variable products are always difficult to deal with. Manipulating them with code snippets or custom plugins is even tougher.

Today’s core function (get_variation_prices) will definitely help you save time while working with prices, custom notices, conditional logic and such.

As usual, we’ll study the function code, take a look at its usage, and finally analyze a case study that will come in useful sooner or later. Enjoy!

Read more

WooCommerce Function of the Week: wc_price

Calculator phone app

The wc_price function solves your WooCommerce price formatting problems. Using it means you can ignore the WooCommerce price settings.

When you work on WooCommerce customization in your plugin/code snippet, you often need to format a price in order to display it correctly on the front-end.

Unfortunately, this can get tricky: you need to get (from the store settings) the WooCommerce currency symbol, the number of decimals, the decimal separator, the suffix, and so on. “Rebuilding” the price format each time can be really time-consuming.

Developers; please meet wc_price.

This neat WooCommerce function can finally solve your price formatting nightmares and make you completely forget about the WooCommerce price settings: it’ll do the job for you.

As usual, we’ll take a look at practical examples; but first, let’s analyze the function code.

Read more

WooCommerce Function of the Week: get_european_union_countries

Close-Up Photo of Wooden Jigsaw Map

You can use get_european_union_countries() for currency switching, to display conditional notices, content, or fields. Basically, whenever there is a country dropdown, get_european_union_countries() may be what you need.

Before we dive into WooCommerce again, here’s a little historical context for the function we’re going to look at this week: get_european_union_countries.

In 1957, Belgium, France, Italy, Luxembourg, the Netherlands, and West Germany signed the Treaty of Rome, which created the European Economic Community. The European Union was formally established with the Maastricht Treaty on 1 November 1993. There are now 27 EU members, yet the list is always subject to change. With Brexit, the United Kingdom left the EU. There is talk currently about Ukraine joining.

Thankfully, WooCommerce developers coded a function called get_european_union_countries(), which returns an array of … countries in the EU! (Did you see that coming?) Once again, we can rely on a lazy WooCommerce function and stop worrying about who is and who is not in the EU.

But why would you ever need to use get_european_union_countries()? Let’s look at its syntax, its arguments, and some possible uses.

Read more

WooCommerce Function of the Week: wc_get_customer_last_order

Two women window shopping

This week we’ll take a look at WooCommerce orders and a neat WooCommerce core function called wc_get_customer_last_order.

Nothing mysterious here — pass a customer ID, and with a PHP one-liner, you get immediately the customer’s latest order!

This can be used in several scenarios. Think for example of an email campaign that triggers six months after the customer’s last order to entice them to reorder. (In marketing, you’d call it a “Win-Back” campaign.)

But before looking at some case studies, let’s analyze the function syntax and source code.

Read more

WooCommerce Function of the Week: wc_get_held_stock_quantity

A getting stock from a shelf in a warehouse.

Welcome back to WooCommerce Function of the Week! This week we cover a little hidden gem within the WooCommerce plugin code: wc_get_held_stock_quantity(). As the name suggests, this function helps you get the amount of stock present in pending orders for a given product.

If your store relies on careful stock management — maybe because you handle products with a small inventory — wc_get_held_stock_quantity is very useful. As orders come in for a limited number of products, you can take special action. Maybe you need to cancel those orders, or maybe you want to entice the customers placing them to complete their purchases before the item goes out of stock.

So, as usual, let’s first take a look at the function syntax and then consider a practical example of how you might use or display the returned value. Enjoy!

Read more

WooCommerce Function of the Week: wc_get_logger

Laptop on a log

If you are or aim to be an advanced WooCommerce developer, this week’s WooCommerce function will help you immensely.

And — spoiler alert — that’s not because of this function’s syntax (which is beyond my own understanding), but due to the world of possibility it opens when it’s called from inside a plugin or code snippet.

Logging in software development means keeping a file log of events for troubleshooting and health tracking purposes. Go to WordPress Dashboard > WooCommerce > Status > Logs and take a look at the dropdown: you’ll see dozens if not hundreds of log entries generated by either WooCommerce itself or some of your active WooCommerce extensions.

You can do that too, with wc_get_logger. The wc_get_logger function lets you create your own event log. You might want one for troubleshooting something under development or to give your clients a handy report of what a plugin or code snippet did on any given day.

Not sure why and how you should use wc_get_logger? Here’s more context, some case studies, and a code example. Enjoy!

Read more

WooCommerce Function of the Week: wc_get_customer_total_spent

WooCommerce is full of unknown gems for developers. For example, there are some core functions that calculate pretty complex data, and these functions are mainly “public” so that WooCommerce developers can reuse them for their own purposes.

Today, let’s take a look at the wc_get_customer_total_spent( $user_id ) WooCommerce PHP function.

As you can guess by its name, this function returns the total amount spent by a given customer identified by their user ID. There is no need to loop through user orders to add up totals and return a final sum — WooCommerce already has you covered.

So, let’s take a deeper look at the syntax, meaning, and usage of wc_get_customer_total_spent.

Read more

A2 Hosting
Omnisend
WordPress.com