I want to retrieve all variations in a template file including hidden variations,
like $product->get_available_variations
I have placed the following in my theme function.php to extend the WC_Product_Variable class.
But when I call my new function
$product->get_all_variations();
in my template (single-product\add-to-cart\variable-subscription.php) it gives an undefined method error.
Calling $product->get_available_variations(); works fine.
What have I missed?
add_action( 'init', 'register_custom_variation' );
function register_custom_variation () {
class WC_Product_Variable_Extension extends WC_Product_Variable {
public function __construct( $product ) {
parent::__construct( $product );
}
public function get_all_variations() {
$variation_ids = $this->get_children();
$available_variations = array();
if ( is_callable( '_prime_post_caches' ) ) {
_prime_post_caches( $variation_ids );
}
foreach ( $variation_ids as $variation_id ) {
$variation = wc_get_product( $variation_id );
// Hide out of stock variations if 'Hide out of stock items from the catalog' is checked.
if ( ! $variation || ! $variation->exists() || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {
continue;
}
$available_variations[] = $this->get_all_variation( $variation );
}
$available_variations = array_values( array_filter( $available_variations ) );
return $available_variations;
}
}
}