Skip to content

Instantly share code, notes, and snippets.

@harishankerr
Created September 25, 2016 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harishankerr/78d32ed7f2f266c0f625321615f78c9b to your computer and use it in GitHub Desktop.
Save harishankerr/78d32ed7f2f266c0f625321615f78c9b to your computer and use it in GitHub Desktop.
<?php
// Shortcode to get variation stock. usage example: Current Stock: [variation_stock id="2525"]
// The id being entered should be that of the variation, not the product.
add_shortcode('variation_stock', 'variation_stock');
function variation_stock( $atts ) {
if ( ! $atts['id'] ) {
return '';
}
$varid = $atts['id'];
$varobj = new WC_Product_variation($varid);
$stock = $varobj->get_stock_quantity();
return (int)$stock; // prints 19, ie the quantity of variation 2525
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment