WooCommerce: Display Stock Status For External Products

By default, WooCommerce external products do not have and do not display any stock, as they are simple redirects to an external URL. This may be unfortunate, because before clicking on an external URL and send people away from your website, you may want to make sure the current item is in stock (so that you have more chances to convert the sale and earn a referral commission, if that’s your business model).

So, how do we “manage stock” for an external product, and display the stock status on the single product page, just before the “Buy Product” button?

I’m now able to show the stock status for WooCommerce external/affiliate products as well! Just store the status in a custom field, and the snippet below will do the rest.

PHP Snippet: Show Stock Availability @ WooCommerce Single External Product Page

Before you use the snippet below, you first need to “set” the external product stock status via a custom field that we’ll call “extstock“. I set its value to either 1 (in stock) or 0 (out of stock). You can of course rename it to whatever you want it, redefine values, use it as stock quantity as opposed to stock status, and even “get” it from another custom field defined by a plugin:

I’m on the single product page for an external product, and I’ve just manually added a new custom field called “extstock” with a value of 1. I will need this in the snippet below to find out the stock status, so that I can display it on the frontend.
/**
 * @snippet       External Product Stock Status @ WooCommerce Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_external_add_to_cart', 'bbloomer_external_product_stock', 29 );

function bbloomer_external_product_stock() {
	global $product;
	$stock_status = get_post_meta( $product->get_id(), 'extstock', true );
	if ( ! $stock_status ) return;
	if ( $stock_status == 1 ) {
		$availability = __( 'In stock', 'woocommerce' );
		$class = 'in-stock';
	} else {
		$availability = __( 'Out of stock', 'woocommerce' );
		$class = 'out-of-stock';
	}
	wc_get_template(
		'single-product/stock.php',
		array(
			'product'      => $product,
			'class'        => $class,
			'availability' => $availability,
		)
	);
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Show “Sold Out” @ Shop Page
    Here’s another simple snippet that can easily help user experience and make sure a “sold out” badge shows on each out of stock product in the category & shop pages. Not all themes allow this so you can use the snippet below to make it happen!
  • WooCommerce: Display Variations’ Stock @ Shop Page
    Thanks to the various requests I get from Business Bloomer fans, this week I’m going to show you a simple PHP snippet to echo the variations’ name and stock quantity on the shop, categories and loop pages. Of course, if “Manage stock” is not enabled at variation level, the quantity will be null, and therefore […]
  • WooCommerce: Display Stock Availability @ Shop Page
    In this tutorial, my goal is to show the “stock availability” under each product in the shop, category and archive pages. This follows exactly the same settings as the stock display of the single product page. Go to /wp-admin/admin.php?page=wc-settings&tab=products&section=inventory to manage “Stock display format”. Enjoy!
  • WooCommerce: Display Out of Stock Products (Shortcode)
    A client of mine wanted to show out of stock products on a separate page – so I coded a simple shortcode for you all! You can use this shortcode for different goals. For example, you might want to display what products you’ve sold to enhance customer trust / social proof. So let’s see (1) […]
  • WooCommerce: Display “In Stock” Products First @ Shop
    We’ve already seen how to add a custom “Product Sorting” option to the “Default Sorting” dropdown in the WooCommerce Shop page. The task I was presented with, however, was to display items based on a custom “meta key”. Now, if you have no idea what a “meta key” is, don’t worry too much. For example, […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *