Senior PHP Engineer | Former E-commerce Website Manager | Full Stack Web Developer

Check if product is on back order in PHP WooCommerce

I recently ran into an issue on a WooCommerce store where we wanted to be able to display an alert on the checkout page when a customer is ordering an product that is on back order. This was just to make sure that the customer was aware.

To check if a WooCommerce product is on backorder using PHP, you can use the get_post_meta() function to retrieve the _backorders meta value for the product. If the value is set to “yes”, the product is on backorder. Here’s an example:

$product_id = 123; // Replace with the ID of your product
$backorder_status = get_post_meta( $product_id, '_backorders', true );

if ( $backorder_status === 'yes' ) {
    // Product is on backorder
} else {
    // Product is not on backorder
}

Alternatively, you can also check if a product is on backorder using the WC_Product class in WooCommerce. Here’s an example:

$product_id = 123; // Replace with the ID of your product
$product = wc_get_product( $product_id );

if ( $product->is_on_backorder() ) {
    // Product is on backorder
} else {
    // Product is not on backorder
}

This method is a bit more efficient as it doesn’t require a database query. However, it requires the WooCommerce plugin to be activated and therefore might not be suitable in all situations, but this code did work for me.

Cycling through the cart & check for back ordered products

With my use case, I wanted to go through each item in the cart, and then check if it was on backorder so that I could then display a message to the customer.

To check if items in a cart are on backorder in WooCommerce using PHP, you can use the WC()->cart object to loop through the cart items and check each item’s backorder status using the is_on_backorder() method of the WC_Product class. Here’s an example:

$cart_items = WC()->cart->get_cart_contents();

foreach ( $cart_items as $cart_item ) {
    $product = $cart_item['data'];

    if ( $product->is_on_backorder() ) {
        // Product is on backorder
        // Do something here, like display a message to the user
    }
}

This code retrieves all items in the cart using the get_cart_contents() method of the WC()->cart object. Then, it loops through each item, retrieves the product object using the $cart_item['data'] property, and checks if the product is on back order using the is_on_backorder() method.

How to add custom code to your WooCommerce store

  • Add code to your theme’s functions.php file
    This is a common method for adding custom PHP code to WordPress. Simply open your theme’s functions.php file and add your code at the bottom, before the closing ?> tag. This code will be executed every time your site loads.
  • Create a custom plugin
    If you have a lot of custom PHP code or want to keep your code separate from your theme, you can create a custom plugin. Simply create a new PHP file in your wp-content/plugins directory and add your code. Then, activate the plugin through your WordPress dashboard.
  • Use a plugin or snippet manager
    There are many plugins available that allow you to add custom PHP code to your site without modifying your theme or creating a custom plugin. These plugins typically provide a text area where you can enter your code, and some even provide syntax highlighting and debugging tools.

Regardless of the method you choose, it’s important to test your code thoroughly before deploying it to a live site. Make sure to back up your site before making any changes, and always test your code on a staging or development site first.

Facebook
Twitter
LinkedIn
Pinterest
Email