I am creating a woocommerce theme and I have product variations i.e. size which is displayed on product details page but problem is that I want to get all variations in my c
A common problem often starts from changing "In stock" or "Out of stock" labels for a single product. There are few solutions out there, where you need to change functions.php file and add a new filter.
The problems get more complicated when you need to modify this for product variations.
This may be your solution: http://bucketpress.com/changing-stock-availability-text-for-product-variations
In order to display available variations of the product you need to modify variable.php file, which can be found in /wp-content/plugins/woocommerce/templates/single-product/add-to-cart/
Find this:
and before tag paste this code:
foreach( $available_variations as $i => $variation ) {
//check if variation has stock or not
if ( $variation['is_in_stock'] ) {
// Get max qty that user can purchase
$max_qty = $variation['max_qty'];
// Prepare availability html for stock available instance
$availability_html = '' . $max_qty . ' units available for your purchase.' . '
';
} else {
// Prepare availability html for out of stock instance
$availability_html = 'Oops, we have no stock left.
';
}
$available_variations[$i]['availability_html'] = $availability_html; }
Don't forget to move php end tag "?>" from this line
do_action( 'woocommerce_before_add_to_cart_form' ); ?>
after your new code.
So the complete variable.php file should like something like this (WooCommerce 3.2.4):
$variation ) {
// check if variation has stock or not
if ( $variation['is_in_stock'] ) {
// Get max qty that user can purchase
$max_qty = $variation['max_qty'];
// Prepare availability html for stock available instance
$availability_html = 'Available: ' . $max_qty . '
';
} else {
// Prepare availability html for out of stock instance
$availability_html = 'Out of stock!
';
}
$available_variations[$i]['availability_html'] = $availability_html;
} ?>
All credits to "Kevin" from: http://bucketpress.com/author/base-admin