Home > Back-end >  WooCommerce - get count of variation attributes
WooCommerce - get count of variation attributes

Time:11-23

I am trying to get/count the amount of variation attributes for at WooCommerce product. If I var_dump the product I see the amount here: https://take.ms/1kJXg but I can't get the value as a variable so I can use it for an if condition.

I have tried a lot of combinations from other questions but without success.

I have come up with this, but missing the code to get the actual code for the var $variationattributes?

add_action( 'woocommerce_before_single_product_summary', 'if_multiple_attributes' );
function if_multiple_attributes() {
    global $product;

    if ( $product->get_type() == 'variable' ) {

        $variations = $product->get_available_variations();

        var_dump($variations);

        // I wish to be able to do something like this

        $varitionattributes = ?????;

        if ($varitionattributes > 1){
            // do something
        }
      
    }
}

CodePudding user response:

You need to apply foreach if you multiple entries and if not then using associate array method or using object based array method you can retrieve the data.

Eg for Associate array value fetch, with your variable:

count($variations['attributes']) --- This will result the count as you want

Eg for Object array value fetch, with your variable:

count($variations->attributes) --- This will result the count as you want

Please try the process once.

  • Related