Fjott
Fjott

Reputation: 1137

Woocommerce: get availability remaining from fixed quantity

I am using the following function in order to customize the display of available products/seats left, using Woocommerce.

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);

function custom_get_availability( $availability, $_product ) {
  global $product;
  $stock = $product->get_total_stock();

  if ( $_product->is_in_stock() ) $availability['availability'] = __($stock . ' seats left', 'woocommerce');
  if ( !$_product->is_in_stock() ) $availability['availability'] = __('No more seats', 'woocommerce');

  return $availability;
}

In addition I would like to be able to display the original quantity of products/seats. Like this: "xx seats left of yy total seats"

Is there something like: $product->get_original_stock(); that I can use?

SOLUTION:

$original_stock = get_post_meta( get_the_ID(), 'original_stock', true );

Upvotes: 2

Views: 1085

Answers (1)

Serg Chernata
Serg Chernata

Reputation: 12400

Woocommerce doesn't keep any record of original stock quantity because that number is always decremented upon purchase. However, you could use custom fields. Add the field in dashboard and then retrieve it like so:

$custom_attributes = get_post_custom();
print $custom_attributes['original_stock'];

Upvotes: 1

Related Questions