I am trying to dynamically echo out the ID of the default attribute for variable WooCommerce products.
Using $default_variation = $product->get_default_attributes();
so far I am only able to echo out an array of attributes for each variable product.
Is there a WooCommerce method available, as opposed to get_default_attributes
, that will simply echo out the ID of the one default attribute?
Alternatively is there a method I can use in my function, included below, to cycle through the array and echo out only the default attribute ID?
function add_new_add_to_cart_button() {
global $product;
if( $product->is_type( 'variable' ) ){
$product_id = $product->get_id();
$default_variation = $product->get_default_attributes();
echo '<form class="cart">
<input type="hidden" name="product_id" value="'.$product_id.'">
<input type="hidden" name="variation_id" value="'.$default_variation.'">
<input type="hidden" name="qty" value="1">
<button class="button product_type_variable add_to_cart_button" style="float:none;" type="submit" name="add-to-cart" value="'.$product_id.'">Buy Now</button>
</form>';
}
}
add_action( 'woocommerce_after_shop_loop_item', 'add_new_add_to_cart_button', 15 );